Ejemplo n.º 1
0
 /**
  * Sends an Tx_Oelib_Mail object (one separate message per recipient).
  *
  * @param Tx_Oelib_Mail $email the Tx_Oelib_Mail object to send
  *
  * @return void
  *
  * @throws InvalidArgumentException
  */
 public function send(Tx_Oelib_Mail $email)
 {
     if (!$email->hasSender()) {
         throw new InvalidArgumentException('$email must have a sender set.', 1331318718);
     }
     $this->validateEmailAddress($email->getSender()->getEmailAddress(), 'From:');
     if ($email->getSubject() === '') {
         throw new InvalidArgumentException('The e-mail subject must not be empty.', 1409410879);
     }
     if (!$email->hasMessage()) {
         throw new InvalidArgumentException('The e-mail message must not be empty.', 1409410886);
     }
     $recipients = $email->getRecipients();
     if (empty($recipients)) {
         throw new InvalidArgumentException('The e-mail must have at least one recipient.', 1409410886);
     }
     foreach ($recipients as $recipient) {
         $this->validateEmailAddress($recipient->getEmailAddress(), 'To:');
     }
     /** @var t3lib_mail_Message $swiftMail */
     $swiftMail = t3lib_div::makeInstance('t3lib_mail_Message');
     $swiftMail->setSubject($email->getSubject());
     $sender = $email->getSender();
     $swiftMail->setFrom(array($sender->getEmailAddress() => $sender->getName()));
     $swiftMail->setCharset('utf-8');
     $returnPath = $email->getReturnPath();
     if ($returnPath !== '') {
         $swiftMail->setReturnPath($returnPath);
     }
     if ($email->hasMessage()) {
         $swiftMail->setBody($this->formatEmailBody($email->getMessage()));
     }
     if ($email->hasHTMLMessage()) {
         $swiftMail->addPart($email->getHTMLMessage(), 'text/html');
     }
     /** @var Tx_Oelib_Attachment $attachment */
     foreach ($email->getAttachments() as $attachment) {
         if ($attachment->getFileName() !== '' && $attachment->getContent() === '') {
             $swiftAttachment = Swift_Attachment::fromPath($attachment->getFileName(), $attachment->getContentType());
         } else {
             $fileName = $attachment->getFileName() !== '' ? $attachment->getFileName() : NULL;
             $swiftAttachment = Swift_Attachment::newInstance($attachment->getContent(), $fileName, $attachment->getContentType());
         }
         $swiftMail->attach($swiftAttachment);
     }
     foreach ($email->getRecipients() as $recipient) {
         $swiftMail->setTo(array($recipient->getEmailAddress() => $recipient->getName()));
         $this->sendSwiftMail($swiftMail);
     }
 }
Ejemplo n.º 2
0
 /**
  * @test
  */
 public function getAttachmentsWithTwoAttachmentsReturnsTwoAttachments()
 {
     $attachment = new Tx_Oelib_Attachment();
     $attachment->setFileName('test.txt');
     $attachment->setContentType('text/plain');
     $attachment->setContent('Test');
     $this->subject->addAttachment($attachment);
     $otherAttachment = new Tx_Oelib_Attachment();
     $otherAttachment->setFileName('second_test.txt');
     $otherAttachment->setContentType('text/plain');
     $otherAttachment->setContent('Second Test');
     $this->subject->addAttachment($otherAttachment);
     self::assertSame(array($attachment, $otherAttachment), $this->subject->getAttachments());
 }