/** * Return all markers and their values as associative array * @param Email $email * @return string[] */ private function getMarkers(Email $email) { $markers = $email->getRecipientData(); // Add predefined markers $markers['newsletter_view_url'] = $email->getViewUrl(); $markers['newsletter_unsubscribe_url'] = $email->getUnsubscribeUrl(); return $markers; }
public function setUp() { parent::setUp(); $this->mockNewsletter = $this->getMock('Ecodev\\Newsletter\\Domain\\Model\\Newsletter', array('getDomain', 'getSenderName', 'getSenderEmail', 'getValidatedContent', 'getInjectOpenSpy', 'getInjectLinksSpy'), array(), '', false); $this->mockNewsletter->method('getDomain')->will($this->returnValue('example.com')); $this->mockNewsletter->method('getSenderName')->will($this->returnValue('John Connor')); $this->mockNewsletter->method('getSenderEmail')->will($this->returnValue('*****@*****.**')); $this->mockEmail = $this->getMock('Ecodev\\Newsletter\\Domain\\Model\\Email', array('s'), array(), '', false); $this->mockEmail->setRecipientData(array('email' => '*****@*****.**', 'my_custom_field' => 'my custom value', 'boolean_false' => false, 'boolean_true' => true, 'integer_false' => 0, 'integer_true' => 1, 'string_false' => '', 'string_true' => 'foo')); $GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['newsletter'] = serialize(array('attach_images' => true)); }
/** * @dataProvider dataProviderTestMailer */ public function testMailer($pid, $injectOpenSpy, $injectLinksSpy, $inputFile, $expectedEmailFile) { $input = file_get_contents($inputFile); $expectedEmail = file_get_contents($expectedEmailFile); $this->mockNewsletter->method('getValidatedContent')->will($this->returnValue(['content' => $input, 'errors' => [], 'warnings' => [], 'infos' => []])); $this->mockNewsletter->method('getInjectOpenSpy')->will($this->returnValue($injectOpenSpy)); $this->mockNewsletter->method('getInjectLinksSpy')->will($this->returnValue($injectLinksSpy)); $this->mockNewsletter->method('getPid')->will($this->returnValue($pid)); $this->mockEmail->method('getPid')->will($this->returnValue($pid)); $mailer = $this->objectManager->get(\Ecodev\Newsletter\Mailer::class); $mailer->setNewsletter($this->mockNewsletter); $mailer->prepare($this->mockEmail); $message = $mailer->createMessage($this->mockEmail); $actualEmail = $message->toString(); $this->assertSame($this->unrandomizeEmail($expectedEmail), $this->unrandomizeEmail($actualEmail)); if ($injectLinksSpy) { $this->assertLinkWasCreated('http://www.example.com'); $this->assertLinkWasCreated('http://###my_custom_field###'); $this->assertLinkWasCreated('http://www.example.com?param=###my_custom_field###'); } }
/** * Dispatch actions to take according to current bounce level */ public function dispatch() { $this->findEmail(); // If couldn't find the original email we cannot do anything if (!$this->email) { Tools::getLogger(__CLASS__)->warning('Bounced email found but cannot find corresponding record in database. Skipped.'); return; } if ($this->bounceLevel != self::NEWSLETTER_NOT_A_BOUNCE) { if ($this->recipientList) { $this->recipientList->registerBounce($this->email->getRecipientAddress(), $this->bounceLevel); } $this->email->setBounceTime(new DateTime()); $emailRepository = $this->objectManager->get('Ecodev\\Newsletter\\Domain\\Repository\\EmailRepository'); $emailRepository->updateNow($this->email); } Tools::getLogger(__CLASS__)->info('Bounced email found with bounce level ' . $this->bounceLevel); }
/** * Raw send method. This does not replace markers, or reset the mail afterwards. * * @param array Record with receivers information as name => value pairs. * @param array Array with extra headers to apply to mails as name => value pairs. * @return void */ private function raw_send(Email $email) { $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Mail\\MailMessage'); $message->setTo($email->getRecipientAddress())->setFrom(array($this->senderEmail => $this->senderName))->setSubject($this->title); if ($this->bounceAddress) { $message->setReturnPath($this->bounceAddress); } foreach ($this->attachments as $attachment) { $message->attach($attachment); } // Specify message-id for bounce identification $msgId = $message->getHeaders()->get('Message-ID'); $msgId->setId($email->getAuthCode() . '@' . $this->newsletter->getDomain()); // Build plaintext $plain = $this->getPlain(); $recipientData = $email->getRecipientData(); if ($recipientData['plain_only']) { $message->setBody($plain, 'text/plain'); } else { // Attach inline files and replace markers used for URL foreach ($this->attachmentsEmbedded as $marker => $attachment) { $embeddedSrc = $message->embed($attachment); $plain = str_replace($marker, $embeddedSrc, $plain); $this->html = str_replace($marker, $embeddedSrc, $this->html); } $message->setBody($this->html, 'text/html'); $message->addPart($plain, 'text/plain'); } $message->send(); }
/** * Return all markers and their values as associative array * @param Email $email * @return string[] */ private function getMarkers(Email $email) { $markers = $email->getRecipientData(); // Add predefined markers $authCode = $email->getAuthCode(); $markers['newsletter_view_url'] = Tools::buildFrontendUri('show', array('c' => $authCode), 'Email'); $markers['newsletter_unsubscribe_url'] = Tools::buildFrontendUri('unsubscribe', array('c' => $authCode), 'Email'); return $markers; }
/** * Creates the Message object from our current state and returns it * * @param Email $email * @return \TYPO3\CMS\Core\Mail\MailMessage */ public function createMessage(Email $email) { /* @var $message \TYPO3\CMS\Core\Mail\MailMessage */ $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Mail\MailMessage::class); $message->setTo($email->getRecipientAddress())->setFrom([$this->senderEmail => $this->senderName])->setSubject($this->title); if ($this->replytoEmail) { $message->addReplyTo($this->replytoEmail, $this->replytoName); } $unsubscribeUrls = ['<' . $email->getUnsubscribeUrl() . '>']; if ($this->bounceAddress) { $message->setReturnPath($this->bounceAddress); array_unshift($unsubscribeUrls, '<mailto:' . $this->bounceAddress . '?subject=unsubscribe-' . $email->getAuthCode() . '>'); } // Add header for easy unsubscribe, either by email, or standard URL $message->getHeaders()->addTextHeader('List-Unsubscribe', implode(', ', $unsubscribeUrls)); $message->getHeaders()->addTextHeader('Precedence', 'bulk'); foreach ($this->attachments as $attachment) { $message->attach($attachment); } // Specify message-id for bounce identification $msgId = $message->getHeaders()->get('Message-ID'); $msgId->setId($email->getAuthCode() . '@' . $this->newsletter->getDomain()); // Build plaintext $plain = $this->getPlain(); $recipientData = $email->getRecipientData(); if ($recipientData['plain_only']) { $message->setBody($plain, 'text/plain'); } else { // Attach inline files and replace markers used for URL foreach ($this->attachmentsEmbedded as $marker => $attachment) { $embeddedSrc = $message->embed($attachment); $plain = str_replace($marker, $embeddedSrc, $plain); $this->html = str_replace($marker, $embeddedSrc, $this->html); } $message->setBody($this->html, 'text/html'); $message->addPart($plain, 'text/plain'); } return $message; }
/** * Sends an email to the address configured in extension settings when a recipient unsubscribe * @param \Ecodev\Newsletter\Domain\Model\Newsletter $newsletter * @param \Ecodev\Newsletter\Domain\Model\RecipientList $recipientList * @param \Ecodev\Newsletter\Domain\Model\Email $email * @return void */ protected function notifyUnsubscribe($newsletter, $recipientList, Email $email) { $notificationEmail = Tools::confParam('notification_email'); // Use the page-owner as user if ($notificationEmail == 'user') { $rs = $GLOBALS['TYPO3_DB']->sql_query("SELECT email\n\t\t\tFROM be_users\n\t\t\tLEFT JOIN pages ON be_users.uid = pages.perms_userid\n\t\t\tWHERE pages.uid = " . $newsletter->getPid()); list($notificationEmail) = $GLOBALS['TYPO3_DB']->sql_fetch_row($rs); } // If cannot find valid email, don't send any notification if (!\TYPO3\CMS\Core\Utility\GeneralUtility::validEmail($notificationEmail)) { return; } // Build email texts $baseUrl = 'http://' . $newsletter->getDomain(); $urlRecipient = $baseUrl . '/typo3/alt_doc.php?&edit[tx_newsletter_domain_model_email][' . $email->getUid() . ']=edit'; $urlRecipientList = $baseUrl . '/typo3/alt_doc.php?&edit[tx_newsletter_domain_model_recipientlist][' . $recipientList->getUid() . ']=edit'; $urlNewsletter = $baseUrl . '/typo3/alt_doc.php?&edit[tx_newsletter_domain_model_newsletter][' . $newsletter->getUid() . ']=edit'; $subject = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('unsubscribe_notification_subject', 'newsletter'); $body = \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate('unsubscribe_notification_body', 'newsletter', array($email->getRecipientAddress(), $urlRecipient, $recipientList->getTitle(), $urlRecipientList, $newsletter->getTitle(), $urlNewsletter)); // Actually sends email $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Mail\\MailMessage'); $message->setTo($notificationEmail)->setFrom(array($newsletter->getSenderEmail() => $newsletter->getSenderName()))->setSubject($subject)->setBody($body, 'text/html'); $message->send(); }
/** * @test */ public function isBounced() { $this->assertFalse($this->subject->isBounced()); $this->subject->setBounceTime(new \DateTime()); $this->assertTrue($this->subject->isBounced()); }