Ejemplo n.º 1
0
 public function testBodySwap()
 {
     $message1 = new Swift_Message('Test');
     $html = Swift_MimePart::newInstance('<html></html>', 'text/html');
     $html->getHeaders()->addTextHeader('X-Test-Remove', 'Test-Value');
     $html->getHeaders()->addTextHeader('X-Test-Alter', 'Test-Value');
     $message1->attach($html);
     $source = $message1->toString();
     $message2 = clone $message1;
     $message2->setSubject('Message2');
     foreach ($message2->getChildren() as $child) {
         $child->setBody('Test');
         $child->getHeaders()->removeAll('X-Test-Remove');
         $child->getHeaders()->get('X-Test-Alter')->setValue('Altered');
     }
     $final = $message1->toString();
     if ($source != $final) {
         $this->fail("Difference although object cloned \n [" . $source . "]\n[" . $final . "]\n");
     }
     $final = $message2->toString();
     if ($final == $source) {
         $this->fail('Two body matches although they should differ' . "\n [" . $source . "]\n[" . $final . "]\n");
     }
     $id_1 = $message1->getId();
     $id_2 = $message2->getId();
     $this->assertEquals($id_1, $id_2, 'Message Ids differ');
     $id_2 = $message2->generateId();
     $this->assertNotEquals($id_1, $id_2, 'Message Ids are the same');
 }
Ejemplo n.º 2
0
 public function testWritingMessageToByteStreamTwiceUsingAFileAttachment()
 {
     $message = new Swift_Message();
     $message->setSubject('test subject');
     $message->setTo('*****@*****.**');
     $message->setCc('*****@*****.**');
     $message->setFrom('*****@*****.**');
     $attachment = Swift_Attachment::fromPath($this->_attFile);
     $message->attach($attachment);
     $message->setBody('HTML part', 'text/html');
     $id = $message->getId();
     $date = preg_quote(date('r', $message->getDate()), '~');
     $boundary = $message->getBoundary();
     $streamA = new Swift_ByteStream_ArrayByteStream();
     $streamB = new Swift_ByteStream_ArrayByteStream();
     $pattern = '~^' . 'Message-ID: <' . $id . '>' . "\r\n" . 'Date: ' . $date . "\r\n" . 'Subject: test subject' . "\r\n" . 'From: user@domain.tld' . "\r\n" . 'To: user@domain.tld' . "\r\n" . 'Cc: other@domain.tld' . "\r\n" . 'MIME-Version: 1.0' . "\r\n" . 'Content-Type: multipart/mixed;' . "\r\n" . ' boundary="' . $boundary . '"' . "\r\n" . "\r\n\r\n" . '--' . $boundary . "\r\n" . 'Content-Type: text/html; charset=utf-8' . "\r\n" . 'Content-Transfer-Encoding: quoted-printable' . "\r\n" . "\r\n" . 'HTML part' . "\r\n\r\n" . '--' . $boundary . "\r\n" . 'Content-Type: ' . $this->_attFileType . '; name=' . $this->_attFileName . "\r\n" . 'Content-Transfer-Encoding: base64' . "\r\n" . 'Content-Disposition: attachment; filename=' . $this->_attFileName . "\r\n" . "\r\n" . preg_quote(base64_encode(file_get_contents($this->_attFile)), '~') . "\r\n\r\n" . '--' . $boundary . '--' . "\r\n" . '$~D';
     $message->toByteStream($streamA);
     $message->toByteStream($streamB);
     $this->assertPatternInStream($pattern, $streamA);
     $this->assertPatternInStream($pattern, $streamB);
 }
Ejemplo n.º 3
0
 /**
  * Send a message to any number of recipients
  * @param Swift_Message The message to send.  This does not need to (and shouldn't really) have any of the recipient headers set.
  * @param mixed The recipients to send to.  Can be a string, Swift_Address or Swift_RecipientList. Note that all addresses apart from Bcc recipients will appear in the message headers
  * @param mixed The address to send the message from.  Can either be a string or an instance of Swift_Address.
  * @return int The number of successful recipients
  * @throws Swift_ConnectionException If sending fails for any reason.
  */
 public function send(Swift_Message $message, $recipients, $from)
 {
     Swift_ClassLoader::load("Swift_Message_Encoder");
     if (is_string($recipients) && preg_match("/^" . Swift_Message_Encoder::CHEAP_ADDRESS_RE . "\$/", $recipients)) {
         $recipients = new Swift_Address($recipients);
     } elseif (!$recipients instanceof Swift_AddressContainer) {
         throw new Exception("The recipients parameter must either be a valid string email address, " . "an instance of Swift_RecipientList or an instance of Swift_Address.");
     }
     if (is_string($from) && preg_match("/^" . Swift_Message_Encoder::CHEAP_ADDRESS_RE . "\$/", $from)) {
         $from = new Swift_Address($from);
     } elseif (!$from instanceof Swift_Address) {
         throw new Exception("The sender parameter must either be a valid string email address or " . "an instance of Swift_Address.");
     }
     $log = Swift_LogContainer::getLog();
     if (!$message->getEncoding() && !$this->connection->hasExtension("8BITMIME")) {
         $message->setEncoding("QP", true, true);
     }
     $list = $recipients;
     if ($recipients instanceof Swift_Address) {
         $list = new Swift_RecipientList();
         $list->addTo($recipients);
     }
     Swift_ClassLoader::load("Swift_Events_SendEvent");
     $send_event = new Swift_Events_SendEvent($message, $list, $from, 0);
     $this->notifyListeners($send_event, "BeforeSendListener");
     $to = $cc = array();
     if (!($has_from = $message->getFrom())) {
         $message->setFrom($from);
     }
     if (!($has_return_path = $message->getReturnPath())) {
         $message->setReturnPath($from->build(true));
     }
     if (!($has_reply_to = $message->getReplyTo())) {
         $message->setReplyTo($from);
     }
     if (!$has_reply_to[0]) {
         $message->setReplyTo($from->getAddress());
     }
     if (!($has_message_id = $message->getId())) {
         $message->generateId();
     }
     $this->command("MAIL FROM: " . $message->getReturnPath(true), 250);
     $failed = 0;
     $sent = 0;
     $tmp_sent = 0;
     $it = $list->getIterator("to");
     while ($it->hasNext()) {
         $it->next();
         $address = $it->getValue();
         $to[] = $address->build();
         try {
             $this->command("RCPT TO: " . $address->build(true), 250);
             $tmp_sent++;
         } catch (Swift_BadResponseException $e) {
             $failed++;
             $send_event->addFailedRecipient($address->getAddress());
             if ($log->hasLevel(Swift_Log::LOG_FAILURES)) {
                 $log->addfailedRecipient($address->getAddress());
             }
         }
     }
     $it = $list->getIterator("cc");
     while ($it->hasNext()) {
         $it->next();
         $address = $it->getValue();
         $cc[] = $address->build();
         try {
             $this->command("RCPT TO: " . $address->build(true), 250);
             $tmp_sent++;
         } catch (Swift_BadResponseException $e) {
             $failed++;
             $send_event->addFailedRecipient($address->getAddress());
             if ($log->hasLevel(Swift_Log::LOG_FAILURES)) {
                 $log->addfailedRecipient($address->getAddress());
             }
         }
     }
     if ($failed == count($to) + count($cc)) {
         $this->reset();
         $this->notifyListeners($send_event, "SendListener");
         return 0;
     }
     if (!($has_to = $message->getTo()) && !empty($to)) {
         $message->setTo($to);
     }
     if (!($has_cc = $message->getCc()) && !empty($cc)) {
         $message->setCc($cc);
     }
     $this->command("DATA", 354);
     $data = $message->build();
     while (false !== ($bytes = $data->read())) {
         $this->command($bytes, -1);
     }
     if ($log->hasLevel(Swift_Log::LOG_NETWORK)) {
         $log->add("<MESSAGE DATA>", Swift_Log::COMMAND);
     }
     try {
         $this->command("\r\n.", 250);
         $sent += $tmp_sent;
     } catch (Swift_BadResponseException $e) {
         $failed += $tmp_sent;
     }
     $tmp_sent = 0;
     $has_bcc = $message->getBcc();
     $it = $list->getIterator("bcc");
     while ($it->hasNext()) {
         $it->next();
         $address = $it->getValue();
         if (!$has_bcc) {
             $message->setBcc($address->build());
         }
         try {
             $this->command("MAIL FROM: " . $message->getReturnPath(true), 250);
             $this->command("RCPT TO: " . $address->build(true), 250);
             $this->command("DATA", 354);
             $data = $message->build();
             while (false !== ($bytes = $data->read())) {
                 $this->command($bytes, -1);
             }
             if ($log->hasLevel(Swift_Log::LOG_NETWORK)) {
                 $log->add("<MESSAGE DATA>", Swift_Log::COMMAND);
             }
             $this->command("\r\n.", 250);
             $sent++;
         } catch (Swift_BadResponseException $e) {
             $failed++;
             $send_event->addFailedRecipient($address->getAddress());
             if ($log->hasLevel(Swift_Log::LOG_FAILURES)) {
                 $log->addfailedRecipient($address->getAddress());
             }
             $this->reset();
         }
     }
     $total = count($to) + count($cc) + count($list->getBcc());
     $send_event->setNumSent($sent);
     $this->notifyListeners($send_event, "SendListener");
     if (!$has_return_path) {
         $message->setReturnPath("");
     }
     if (!$has_from) {
         $message->setFrom("");
     }
     if (!$has_to) {
         $message->setTo("");
     }
     if (!$has_reply_to) {
         $message->setReplyTo(null);
     }
     if (!$has_cc) {
         $message->setCc(null);
     }
     if (!$has_bcc) {
         $message->setBcc(null);
     }
     if (!$has_message_id) {
         $message->setId(null);
     }
     if ($log->hasLevel(Swift_Log::LOG_NETWORK)) {
         $log->add("Message sent to " . $sent . "/" . $total . " recipients", Swift_Log::NORMAL);
     }
     return $sent;
 }
Ejemplo n.º 4
0
 /**
  * PHPWS_Email has a built-in simple logging function.  This replicates
  * the functionality of that function for SwiftMail.
  */
 public static function logSwiftmailMessage(Swift_Message $message)
 {
     $id = 'id:' . $message->getId();
     $from = 'from:' . $message->getSender();
     $to = 'to:' . implode(',', array_keys($message->getTo()));
     // Optional fields, If the message has them, implode the arrays to simple strings.
     $cc = $message->getCc() != null ? 'cc:' . implode(',', array_keys($message->getCc())) : '';
     $bcc = $message->getBcc() != null ? 'bcc:' . implode(',', array_keys($message->getBcc())) : '';
     $replyto = $message->getReplyTo() != null ? 'reply-to:' . implode(',', array_keys($message->getReplyTo())) : '';
     $subject = 'subject:' . $message->getSubject();
     $module = 'module:' . PHPWS_Core::getCurrentModule();
     $user = '******' . (Current_User::isLogged() ? Current_User::getUsername() : '');
     PHPWS_Core::log("{$id} {$module} {$user} {$subject} {$from} {$to} {$cc} {$bcc} {$replyto}", 'phpws-mail.log', 'mail');
 }
Ejemplo n.º 5
0
 /**
  * {@inheritdoc}
  */
 public function getId()
 {
     return $this->message->getId();
 }
Ejemplo n.º 6
-1
 public function testEmbeddedFilesWithMultipartDataCreateMultipartRelatedContentAsAnAlternative()
 {
     $message = new Swift_Message();
     $message->setCharset('utf-8');
     $message->setSubject('test subject');
     $message->addPart('plain part', 'text/plain');
     $image = new Swift_Image('<image data>', 'image.gif', 'image/gif');
     $cid = $message->embed($image);
     $message->setBody('<img src="' . $cid . '" />', 'text/html');
     $message->setTo(array('*****@*****.**' => 'User'));
     $message->setFrom(array('*****@*****.**' => 'Other'));
     $message->setSender(array('*****@*****.**' => 'Other'));
     $id = $message->getId();
     $date = preg_quote(date('r', $message->getDate()), '~');
     $boundary = $message->getBoundary();
     $cidVal = $image->getId();
     $this->assertRegExp('~^' . 'Sender: Other <*****@*****.**>' . "\r\n" . 'Message-ID: <' . $id . '>' . "\r\n" . 'Date: ' . $date . "\r\n" . 'Subject: test subject' . "\r\n" . 'From: Other <*****@*****.**>' . "\r\n" . 'To: User <*****@*****.**>' . "\r\n" . 'MIME-Version: 1.0' . "\r\n" . 'Content-Type: multipart/alternative;' . "\r\n" . ' boundary="' . $boundary . '"' . "\r\n" . "\r\n\r\n" . '--' . $boundary . "\r\n" . 'Content-Type: text/plain; charset=utf-8' . "\r\n" . 'Content-Transfer-Encoding: quoted-printable' . "\r\n" . "\r\n" . 'plain part' . "\r\n\r\n" . '--' . $boundary . "\r\n" . 'Content-Type: multipart/related;' . "\r\n" . ' boundary="(.*?)"' . "\r\n" . "\r\n\r\n" . '--\\1' . "\r\n" . 'Content-Type: text/html; charset=utf-8' . "\r\n" . 'Content-Transfer-Encoding: quoted-printable' . "\r\n" . "\r\n" . '<img.*?/>' . "\r\n\r\n" . '--\\1' . "\r\n" . 'Content-Type: image/gif; name=image.gif' . "\r\n" . 'Content-Transfer-Encoding: base64' . "\r\n" . 'Content-Disposition: inline; filename=image.gif' . "\r\n" . 'Content-ID: <' . $cidVal . '>' . "\r\n" . "\r\n" . preg_quote(base64_encode('<image data>'), '~') . "\r\n\r\n" . '--\\1--' . "\r\n" . "\r\n\r\n" . '--' . $boundary . '--' . "\r\n" . '$~D', $message->toString());
 }
Ejemplo n.º 7
-2
 public function testHTMLPartAppearsLastEvenWhenAttachmentsAdded()
 {
     $message = new Swift_Message();
     $message->setCharset('utf-8');
     $message->setSubject('test subject');
     $message->addPart('plain part', 'text/plain');
     $attachment = new Swift_Attachment('<data>', 'image.gif', 'image/gif');
     $message->attach($attachment);
     $message->setBody('HTML part', 'text/html');
     $message->setTo(array('*****@*****.**' => 'User'));
     $message->setFrom(array('*****@*****.**' => 'Other'));
     $message->setSender(array('*****@*****.**' => 'Other'));
     $id = $message->getId();
     $date = preg_quote($message->getDate()->format('r'), '~');
     $boundary = $message->getBoundary();
     $this->assertRegExp('~^' . 'Sender: Other <*****@*****.**>' . "\r\n" . 'Message-ID: <' . $id . '>' . "\r\n" . 'Date: ' . $date . "\r\n" . 'Subject: test subject' . "\r\n" . 'From: Other <*****@*****.**>' . "\r\n" . 'To: User <*****@*****.**>' . "\r\n" . 'MIME-Version: 1.0' . "\r\n" . 'Content-Type: multipart/mixed;' . "\r\n" . ' boundary="' . $boundary . '"' . "\r\n" . "\r\n\r\n" . '--' . $boundary . "\r\n" . 'Content-Type: multipart/alternative;' . "\r\n" . ' boundary="(.*?)"' . "\r\n" . "\r\n\r\n" . '--\\1' . "\r\n" . 'Content-Type: text/plain; charset=utf-8' . "\r\n" . 'Content-Transfer-Encoding: quoted-printable' . "\r\n" . "\r\n" . 'plain part' . "\r\n\r\n" . '--\\1' . "\r\n" . 'Content-Type: text/html; charset=utf-8' . "\r\n" . 'Content-Transfer-Encoding: quoted-printable' . "\r\n" . "\r\n" . 'HTML part' . "\r\n\r\n" . '--\\1--' . "\r\n" . "\r\n\r\n" . '--' . $boundary . "\r\n" . 'Content-Type: image/gif; name=image.gif' . "\r\n" . 'Content-Transfer-Encoding: base64' . "\r\n" . 'Content-Disposition: attachment; filename=image.gif' . "\r\n" . "\r\n" . preg_quote(base64_encode('<data>'), '~') . "\r\n\r\n" . '--' . $boundary . '--' . "\r\n" . '$~D', $message->toString());
 }