/** * 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(); }
/** * Fix relative URL to absolute URL */ private function infoRelativeToAbsolute() { // Find out the absolute domain. If specified in HTML source, use it as is. if (preg_match('|<base[^>]*href="([^"]*)"[^>]*/>|i', $this->content, $match)) { $absoluteDomain = $match[1]; } else { $absoluteDomain = $this->newsletter->getBaseUrl() . '/'; } $urlPatterns = ['hyperlinks' => '/<a [^>]*href="(.*)"/Ui', 'stylesheets' => '/<link [^>]*href="(.*)"/Ui', 'images' => '/ src="(.*)"/Ui', 'background images' => '/ background="(.*)"/Ui']; foreach ($urlPatterns as $type => $urlPattern) { preg_match_all($urlPattern, $this->content, $urls); $replacementCount = 0; foreach ($urls[1] as $i => $url) { // If this is already an absolute link, dont replace it $decodedUrl = html_entity_decode($url); if (!Uri::isAbsolute($decodedUrl)) { $replace_url = str_replace($decodedUrl, $absoluteDomain . ltrim($decodedUrl, '/'), $urls[0][$i]); $this->content = str_replace($urls[0][$i], $replace_url, $this->content); ++$replacementCount; } } if ($replacementCount) { $this->infos[] = sprintf($this->lang->getLL('validation_mail_converted_relative_url'), $type); } } }
/** * @dataProvider dataProviderTestMailer */ public function testMailer($injectOpenSpy, $injectLinksSpy, $inputFile, $expectedHtmlFile, $expectedPlainFile) { $input = file_get_contents($inputFile); $expectedHtml = file_get_contents($expectedHtmlFile); $expectedPlain = file_get_contents($expectedPlainFile); $this->mockNewsletter->method('getValidatedContent')->will($this->returnValue(array('content' => $input, 'errors' => array(), 'warnings' => array(), 'infos' => array()))); $this->mockNewsletter->method('getInjectOpenSpy')->will($this->returnValue($injectOpenSpy)); $this->mockNewsletter->method('getInjectLinksSpy')->will($this->returnValue($injectLinksSpy)); $mailer = $this->objectManager->get('Ecodev\\Newsletter\\Mailer'); $mailer->setNewsletter($this->mockNewsletter); $mailer->prepare($this->mockEmail, true); $actualHtml = $mailer->getHtml(); $actualPlain = $mailer->getPlain(); $this->assertEquals($expectedHtml, $actualHtml); $this->assertEquals($expectedPlain, $actualPlain); }
/** * @test */ public function getUidRecipientList() { $this->assertNull($this->subject->getUidRecipientList()); $recipientList = $this->getMock('Ecodev\\Newsletter\\Domain\\Model\\RecipientList\\BeUsers', array('getUid'), array(), '', false); $recipientList->expects($this->once())->method('getUid')->will($this->returnValue(123)); $this->subject->setRecipientList($recipientList); $this->assertEquals(123, $this->subject->getUidRecipientList()); }
/** * @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###'); } }
/** * @dataProvider dataProviderTestMailer */ public function testMailer($injectOpenSpy, $injectLinksSpy, $inputFile, $expectedHtmlFile, $expectedPlainFile) { $input = file_get_contents($inputFile); $expectedHtml = file_get_contents($expectedHtmlFile); $expectedPlain = file_get_contents($expectedPlainFile); $this->mockNewsletter->method('getValidatedContent')->will($this->returnValue(array('content' => $input, 'errors' => array(), 'warnings' => array(), 'infos' => array()))); $this->mockNewsletter->method('getInjectOpenSpy')->will($this->returnValue($injectOpenSpy)); $this->mockNewsletter->method('getInjectLinksSpy')->will($this->returnValue($injectLinksSpy)); $mailer = $this->objectManager->get('Ecodev\\Newsletter\\Mailer'); $mailer->setNewsletter($this->mockNewsletter); $mailer->prepare($this->mockEmail); $actualHtml = $mailer->getHtml(); $actualPlain = $mailer->getPlain(); $this->assertSame($expectedHtml, $actualHtml); $this->assertSame($expectedPlain, $actualPlain); if ($injectLinksSpy) { $this->assertLinkWasCreated('http://www.example.com'); $this->assertLinkWasCreated('http://###my_custom_field###'); $this->assertLinkWasCreated('http://www.example.com?param=###my_custom_field###'); } }
/** * 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; }
/** * Creates a new Newsletter and forwards to the list action. * * @param \Ecodev\Newsletter\Domain\Model\Newsletter $newNewsletter a fresh Newsletter object which has not yet been added to the repository * @dontverifyrequesthash * @dontvalidate $newNewsletter * @ignorevalidation $newNewsletter */ public function createAction(Newsletter $newNewsletter = null) { $limitTestRecipientCount = 10; // This is a low limit, technically, but it does not make sense to test a newsletter for more people than that anyway $recipientList = $newNewsletter->getRecipientList(); $recipientList->init(); $count = $recipientList->getCount(); $validatedContent = $newNewsletter->getValidatedContent($language); // If we attempt to create a newsletter as a test but it has too many recipient, reject it (we cannot safely send several emails wihtout slowing down respoonse and/or timeout issues) if ($newNewsletter->getIsTest() && $count > $limitTestRecipientCount) { $this->addFlashMessage($this->translate('flashmessage_test_maximum_recipients', [$count, $limitTestRecipientCount]), $this->translate('flashmessage_test_maximum_recipients_title'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR); $this->view->assign('success', false); } elseif (count($validatedContent['errors'])) { $this->addFlashMessage('The newsletter HTML content does not validate. See tab "Newsletter > Status" for details.', $this->translate('flashmessage_newsletter_invalid'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR); $this->view->assign('success', false); } else { // If it's a test newsletter, it's planned to be sent right now if ($newNewsletter->getIsTest()) { $newNewsletter->setPlannedTime(new DateTime()); } // Save the new newsletter $this->newsletterRepository->add($newNewsletter); $this->persistenceManager->persistAll(); $this->view->assign('success', true); // If it is test newsletter, send it immediately if ($newNewsletter->getIsTest()) { try { // Fill the spool and run the queue Tools::createSpool($newNewsletter); Tools::runSpool($newNewsletter); $this->addFlashMessage($this->translate('flashmessage_test_newsletter_sent'), $this->translate('flashmessage_test_newsletter_sent_title'), \TYPO3\CMS\Core\Messaging\FlashMessage::OK); } catch (\Exception $exception) { $this->addFlashMessage($exception->getMessage(), $this->translate('flashmessage_test_newsletter_error'), \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR); } } else { $this->addFlashMessage($this->translate('flashmessage_newsletter_queued'), $this->translate('flashmessage_newsletter_queued_title'), \TYPO3\CMS\Core\Messaging\FlashMessage::OK); } } $this->view->setVariablesToRender(['data', 'success', 'flashMessages']); $this->view->setConfiguration(['data' => self::resolveJsonViewConfiguration()]); $this->view->assign('data', $newNewsletter); $this->flushFlashMessages(); }
/** * Spool a newsletter page out to the real receivers. * * @global \TYPO3\CMS\Core\Database\DatabaseConnection $TYPO3_DB * @param Newsletter $newsletter */ public static function createSpool(Newsletter $newsletter) { // If newsletter is locked because spooling now, or already spooled, then skip if ($newsletter->getBeginTime()) { return; } $newsletterRepository = self::getNewsletterRepository(); // Lock the newsletter by setting its begin_time $beginTime = new DateTime(); $newsletter->setBeginTime($beginTime); $newsletterRepository->updateNow($newsletter); $emailSpooledCount = 0; $recipientList = $newsletter->getRecipientList(); $recipientList->init(); $databaseConnection = self::getDatabaseConnection(); while ($receiver = $recipientList->getRecipient()) { // Register the recipient if (GeneralUtility::validEmail($receiver['email'])) { $databaseConnection->exec_INSERTquery('tx_newsletter_domain_model_email', ['pid' => $newsletter->getPid(), 'recipient_address' => $receiver['email'], 'recipient_data' => serialize($receiver), 'newsletter' => $newsletter->getUid()]); $databaseConnection->exec_UPDATEquery('tx_newsletter_domain_model_email', 'uid = ' . intval($databaseConnection->sql_insert_id()), ['auth_code' => 'MD5(CONCAT(uid, recipient_address))'], ['auth_code']); ++$emailSpooledCount; } } self::getLogger(__CLASS__)->info("Queued {$emailSpooledCount} emails to be sent for newsletter " . $newsletter->getUid()); // Schedule repeated newsletter if any $newsletter->scheduleNextNewsletter(); // Unlock the newsletter by setting its end_time $newsletter->setEndTime(new DateTime()); $newsletterRepository->updateNow($newsletter); }
/** * Returns the content of the newsletter with validation messages. The content * is also "fixed" automatically when possible. * @param Newsletter $newsletter * @param string $language language of the content of the newsletter (the 'L' parameter in TYPO3 URL) * @return array ('content' => $content, 'errors' => $errors, 'warnings' => $warnings, 'infos' => $infos); */ public function validate(Newsletter $newsletter, $language = null) { $this->initializeLang(); // We need to catch the exception if domain was not found/configured properly try { $url = $newsletter->getContentUrl($language); } catch (Exception $e) { return array('content' => '', 'errors' => array($e->getMessage()), 'warnings' => array(), 'infos' => array()); } $content = $this->getURL($url); $errors = array(); $warnings = array(); $infos = array(sprintf($this->lang->getLL('validation_content_url'), '<a target="_blank" href="' . $url . '">' . $url . '</a>')); // Content should be more that just a few characters. Apache error propably occured if (strlen($content) < 200) { $errors[] = $this->lang->getLL('validation_mail_too_short'); } // Content should not contain PHP-Warnings if (substr($content, 0, 22) == "<br />\n<b>Warning</b>:") { $errors[] = $this->lang->getLL('validation_mail_contains_php_warnings'); } // Content should not contain PHP-Warnings if (substr($content, 0, 26) == "<br />\n<b>Fatal error</b>:") { $errors[] = $this->lang->getLL('validation_mail_contains_php_errors'); } // If the page contains a "Pages is being generared" text... this is bad too if (strpos($content, 'Page is being generated.') && strpos($content, 'If this message does not disappear within')) { $errors[] = $this->lang->getLL('validation_mail_being_generated'); } // Find out the absolute domain. If specified in HTML source, use it as is. if (preg_match('|<base[^>]*href="([^"]*)"[^>]*/>|i', $content, $match)) { $absoluteDomain = $match[1]; } else { $absoluteDomain = $newsletter->getBaseUrl() . '/'; } // Fix relative URL to absolute URL $urlPatterns = array('hyperlinks' => '/<a [^>]*href="(.*)"/Ui', 'stylesheets' => '/<link [^>]*href="(.*)"/Ui', 'images' => '/ src="(.*)"/Ui', 'background images' => '/ background="(.*)"/Ui'); foreach ($urlPatterns as $type => $urlPattern) { preg_match_all($urlPattern, $content, $urls); $replacementCount = 0; foreach ($urls[1] as $i => $url) { // If this is already an absolute link, dont replace it $decodedUrl = html_entity_decode($url); if (!Uri::isAbsolute($decodedUrl)) { $replace_url = str_replace($decodedUrl, $absoluteDomain . ltrim($decodedUrl, '/'), $urls[0][$i]); $content = str_replace($urls[0][$i], $replace_url, $content); ++$replacementCount; } } if ($replacementCount) { $infos[] = sprintf($this->lang->getLL('validation_mail_converted_relative_url'), $type); } } // Find linked css and convert into a style-tag preg_match_all('|<link rel="stylesheet" type="text/css" href="([^"]+)"[^>]+>|Ui', $content, $urls); foreach ($urls[1] as $i => $url) { $content = str_replace($urls[0][$i], "<!-- fetched URL: {$url} -->\n<style type=\"text/css\">\n<!--\n" . $this->getURL($url) . "\n-->\n</style>", $content); } if (count($urls[1])) { $infos[] = $this->lang->getLL('validation_mail_contains_linked_styles'); } // We cant very well have attached javascript in a newsmail ... removing $content = preg_replace('|<script[^>]*type="text/javascript"[^>]*>[^<]*</script>|i', '', $content, -1, $count); if ($count) { $warnings[] = $this->lang->getLL('validation_mail_contains_javascript'); } // Images in CSS if (preg_match('|background-image: url\\([^\\)]+\\)|', $content) || preg_match('|list-style-image: url\\([^\\)]+\\)|', $content)) { $errors[] = $this->lang->getLL('validation_mail_contains_css_images'); } // CSS-classes if (preg_match('|<[a-z]+ [^>]*class="[^"]+"[^>]*>|', $content)) { $warnings[] = $this->lang->getLL('validation_mail_contains_css_classes'); } // Positioning & element sizes in CSS $forbiddenCssProperties = array('width' => '((min|max)+-)?width', 'height' => '((min|max)+-)?height', 'margin' => 'margin(-(bottom|left|right|top)+)?', 'padding' => 'padding(-(bottom|left|right|top)+)?', 'position' => 'position'); $forbiddenCssPropertiesWarnings = array(); if (preg_match_all('|<[a-z]+[^>]+style="([^"]*)"|', $content, $matches)) { foreach ($matches[1] as $stylepart) { foreach ($forbiddenCssProperties as $property => $regex) { if (preg_match('/(^|[^\\w-])' . $regex . '[^\\w-]/', $stylepart)) { $forbiddenCssPropertiesWarnings[$property] = $property; } } } foreach ($forbiddenCssPropertiesWarnings as $property) { $warnings[] = sprintf($this->lang->getLL('validation_mail_contains_css_some_property'), $property); } } return array('content' => $content, 'errors' => $errors, 'warnings' => $warnings, 'infos' => $infos); }
/** * Find all pairs of newsletter-email UIDs that are should be sent * * @global \TYPO3\CMS\Core\Database\DatabaseConnection $TYPO3_DB * @param Newsletter $newsletter * @return array [[newsletter => 12, email => 5], ...] */ public static function findAllNewsletterAndEmailUidToSend(Newsletter $newsletter = null) { global $TYPO3_DB; // Apply limit of emails per round $mails_per_round = (int) \Ecodev\Newsletter\Tools::confParam('mails_per_round'); if ($mails_per_round) { $limit = ' LIMIT ' . $mails_per_round; } else { $limit = ''; } // Apply newsletter restriction if any if ($newsletter) { $newsletterUid = 'AND tx_newsletter_domain_model_newsletter.uid = ' . $newsletter->getUid(); } else { $newsletterUid = ''; } // Find the uid of emails and newsletters that need to be sent $rs = $TYPO3_DB->sql_query('SELECT tx_newsletter_domain_model_newsletter.uid AS newsletter, tx_newsletter_domain_model_email.uid AS email FROM tx_newsletter_domain_model_email INNER JOIN tx_newsletter_domain_model_newsletter ON (tx_newsletter_domain_model_email.newsletter = tx_newsletter_domain_model_newsletter.uid) WHERE tx_newsletter_domain_model_email.begin_time = 0 ' . $newsletterUid . ' ORDER BY tx_newsletter_domain_model_email.newsletter ' . $limit); $result = array(); while ($record = $TYPO3_DB->sql_fetch_assoc($rs)) { $result[] = $record; } return $result; }
/** * @test */ public function getReplytoEmail() { $this->assertSame('*****@*****.**', $this->subject->getReplytoEmail(), 'sould return globally configured default value'); $this->subject->setReplytoEmail('*****@*****.**'); $this->assertSame('*****@*****.**', $this->subject->getReplytoEmail(), 'sould return locally set value'); }
/** * 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(); }
/** * Spool a newsletter page out to the real receivers. * * @global \TYPO3\CMS\Core\Database\DatabaseConnection $TYPO3_DB * @param array Newsletter record. * @param integer Actual begin time. * @return void */ public static function createSpool(Newsletter $newsletter) { global $TYPO3_DB; // If newsletter is locked because spooling now, or already spooled, then skip if ($newsletter->getBeginTime()) { return; } $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager'); $newsletterRepository = $objectManager->get('Ecodev\\Newsletter\\Domain\\Repository\\NewsletterRepository'); // Lock the newsletter by setting its begin_time $begintime = new DateTime(); $newsletter->setBeginTime($begintime); $newsletterRepository->updateNow($newsletter); $emailSpooledCount = 0; $recipientList = $newsletter->getRecipientList(); $recipientList->init(); while ($receiver = $recipientList->getRecipient()) { // Register the receiver if (\TYPO3\CMS\Core\Utility\GeneralUtility::validEmail($receiver['email'])) { $TYPO3_DB->exec_INSERTquery('tx_newsletter_domain_model_email', array('pid' => $newsletter->getPid(), 'recipient_address' => $receiver['email'], 'recipient_data' => serialize($receiver), 'pid' => $newsletter->getPid(), 'newsletter' => $newsletter->getUid())); $emailSpooledCount++; } } self::log("Queued {$emailSpooledCount} emails to be sent for newsletter " . $newsletter->getUid()); // Schedule repeated newsletter if any $newsletter->scheduleNextNewsletter(); // Unlock the newsletter by setting its end_time $newsletter->setEndTime(new DateTime()); $newsletterRepository->updateNow($newsletter); }