/** * @group ZF-8988 */ public function testReturnPathIsUsedAsMailFrom() { $connectionMock = $this->getMock('Zend_Mail_Protocol_Smtp'); $connectionMock->expects($this->once())->method('mail')->with('*****@*****.**'); $transport = new Zend_Mail_Transport_Smtp($this->_params['host'], $this->_params); $transport->setConnection($connectionMock); $mail = new Zend_Mail(); $mail->setBodyText('This is a test.')->setFrom('*****@*****.**', 'from user')->setReturnPath('*****@*****.**'); $mail->send($transport); }
/** * Sends mass mailing. * Prepares data and tests the recipients list before sendig emails. * * @param int $releaseID The release of the the newsletter to send. * * @return array Contains data corresponding to the send result. */ public function sendMassMailingAction($releaseID = null) { $this->disableView(); if (!$releaseID) { $releaseID = $_REQUEST['releaseID']; } // $releaseID = $this->view->params['releaseID']; // 1- Get all newsletter to send $dateTimeNow = date('Y-m-d H:i:s'); $newsletterSelect = new NewsletterReleases(); $select = $newsletterSelect->select()->setIntegrityCheck(false); $select->from('Newsletter_Releases')->join('Languages', 'L_ID = NR_LanguageID')->join('CategoriesIndex', 'CI_CategoryID = NR_CategoryID')->join('Newsletter_Models_Index', 'NMI_NewsletterModelID = NR_ModelID')->join('Newsletter_Models', 'NM_ID = NMI_NewsletterModelID')->where('CI_LanguageID = NR_LanguageID')->where('NMI_LanguageID = NR_LanguageID')->where('NR_Status <> 1')->where('NR_ID = ?', $releaseID); $newsletterData = $newsletterSelect->fetchAll($select); foreach ($newsletterData as $release) { $listSent = array(); $listDest = array(); $listIds = array(); $mailLog = array(); $date = new Zend_Date($release['NR_Date'], null, Zend_Registry::get('languageSuffix') == 'fr' ? 'fr_CA' : 'en_CA'); $date_string = Cible_FunctionsGeneral::dateToString($date, Cible_FunctionsGeneral::DATE_LONG_NO_DAY, '.'); $date_string_url = Cible_FunctionsGeneral::dateToString($date, Cible_FunctionsGeneral::DATE_SQL, '-'); $releaseLanguage = $release['NR_LanguageID']; $this->view->assign('languageRelease', $releaseLanguage); $filteredData = $this->_countFilterMembers($release['NR_CollectionFiltersID'], $releaseLanguage); $members = $filteredData['members']; $selection = $filteredData['selection']; $dateTimeStart = date('Y-m-d H:i:s'); $member_count = 0; $stats = array('action' => 'set', 'sentTo' => 0, 'targetedTotal' => 0); if ($release['NR_Status'] == 0 || $release['NR_Status'] == 3) { //Send to all recipient even if they have already received it $member_count = count($members); } elseif ($release['NR_Status'] == 2) { $member_count = count($members); $stats['action'] = 'increment'; //Send to recipient who have not already received it $alreadyMembersRecievedSelect = new NewsletterReleasesMembers(); $select = $alreadyMembersRecievedSelect->select()->where('NRM_ReleaseID = ?', $release['NR_ID']); $alreadyMembersRecievedData = $alreadyMembersRecievedSelect->fetchAll($select); $already_received_count = count($alreadyMembersRecievedData); $membersTmp = array(); for ($i = 0; $i < $member_count; $i++) { $received = "false"; for ($j = 0; $j < $already_received_count; $j++) { if ($members[$i]['GP_MemberID'] == $alreadyMembersRecievedData[$j]['NRM_MemberID']) { $received = "true"; } } if ($received == "false") { array_push($membersTmp, $members[$i]); } } $members = $membersTmp; $member_count = count($members); } $stats['targetedTotal'] = $member_count; if (!empty($members) && $member_count > 0) { $newsletterArticlesSelect = new NewsletterArticles(); $select = $newsletterArticlesSelect->select(); $select->where('NA_ReleaseID = ?', $release['NR_ID'])->order('NA_ZoneID')->order('NA_PositionID'); $newsletterArticlesData = $newsletterArticlesSelect->fetchAll($select); $this->view->articles = $newsletterArticlesData->toArray(); $registry = Zend_Registry::getInstance()->set('format', 'email'); $config = Zend_Registry::get('config')->toArray(); $nbMax = $config['massMailing']['packof']; $sleep = $config['massMailing']['sleep']; $server = $config['massMailing']['server']; $i = 0; set_time_limit(0); $emailValidator = new Zend_Validate_EmailAddress(); $sentToCount = 0; $failedEmailAddress = array(); for ($k = 0; $k < $member_count; $k++) { try { if ($i == $nbMax) { $protocol->quit(); $protocol->disconnect(); sleep($sleep); $i = 0; } if ($i == 0) { $transport = new Zend_Mail_Transport_Smtp(); $protocol = new Zend_Mail_Protocol_Smtp($server); $protocol->connect(); $protocol->helo($server); $transport->setConnection($protocol); } $protocol->rset(); if ($emailValidator->isValid($members[$k]['GP_Email'])) { $date = new Zend_Date($release['NR_Date'], null, Zend_Registry::get('languageSuffix') == 'fr' ? 'fr_CA' : 'en_CA'); $date_string = Cible_FunctionsGeneral::dateToString($date, Cible_FunctionsGeneral::DATE_LONG_NO_DAY, '.'); $date_string_url = Cible_FunctionsGeneral::dateToString($date, Cible_FunctionsGeneral::DATE_SQL, '-'); $newsletterCategoryID = $release['NR_CategoryID']; $this->view->assign('unsubscribeLink', "/" . Cible_FunctionsCategories::getPagePerCategoryView($newsletterCategoryID, 'unsubscribe', 8)); $this->view->assign('subscribeLink', "/" . Cible_FunctionsCategories::getPagePerCategoryView($newsletterCategoryID, 'subscribe', 8, $releaseLanguage)); $this->view->assign('archiveLink', "/" . Cible_FunctionsCategories::getPagePerCategoryView($newsletterCategoryID, 'list_archives', 8, $releaseLanguage)); //$this->view->assign('details_release', "/" . Cible_FunctionsCategories::getPagePerCategoryView($newsletterCategoryID, 'details_release', 8) . "/ID/" . $release['NR_ID']); $this->view->assign('details_release', "/" . Cible_FunctionsCategories::getPagePerCategoryView($newsletterCategoryID, 'details_release', 8, $releaseLanguage) . "/" . $date_string_url . "/" . $release['NR_Title']); $this->view->assign('details_page', Cible_FunctionsCategories::getPagePerCategoryView($newsletterCategoryID, 'details_article', 8)); $this->view->assign('isOnline', $release['NR_Online']); $this->view->assign('newsletterID', $release['NR_ID']); $this->view->assign('memberId', $members[$k]['GP_MemberID']); $this->view->assign('moduleId', $this->_moduleID); $this->view->assign('dateString', $date_string); $this->view->assign('parutionDate', $date_string_url); $bodyText = $this->view->render($release['NM_DirectoryEmail']); $salutationsSelect = new Salutations(); $select = $salutationsSelect->select()->setIntegrityCheck(false); $salutationId = $members[$k]['GP_Salutation']; if (is_null($salutationId)) { $salutationId = 0; } $select->from('Salutations')->join('Static_Texts', 'ST_Identifier = S_StaticTitle')->where('ST_LangID = ?', Zend_Registry::get("languageID"))->where('S_ID = ?', $salutationId); $salutationsData = $salutationsSelect->fetchRow($select); $bodyText = str_replace('#prenom#', $members[$k]['GP_FirstName'], $bodyText); $bodyText = str_replace('#nom#', $members[$k]['GP_LastName'], $bodyText); $bodyText = str_replace('#courtoisie#', $salutationsData['ST_Value'], $bodyText); //$newsletterData['NR_AfficherTitre'] = $form->getValue('NR_AfficherTitre'); $newsletterAfficherTitre = $release['NR_AfficherTitre']; $newsletterTextIntro = $release['NR_TextIntro']; $newsletterTextIntro = str_replace('##prenom##', $members[$k]['GP_FirstName'], $newsletterTextIntro); $newsletterTextIntro = str_replace('##nom##', $members[$k]['GP_LastName'], $newsletterTextIntro); $newsletterTextIntro = str_replace('##salutation##', $salutationsData['ST_Value'], $newsletterTextIntro); $this->view->intro = $newsletterTextIntro; $this->view->newsletterAfficherTitre = $newsletterAfficherTitre; $mail = new Zend_Mail(); $mail->setBodyHtml($bodyText); $mail->setFrom($release['NM_FromEmail'], $release['NMI_FromName']); $mail->addTo($members[$k]['GP_Email']); $mail->setSubject($release['NR_Title']); if (!$mail->send()) { array_push($failedEmailAddress, array('fname' => $members[$k]['GP_FirstName'], 'lname' => $members[$k]['GP_LastName'], 'email' => $members[$k]['GP_Email'])); } $sentToCount++; $releaseMember = new NewsletterReleasesMembers(); $releaseMemberData = $releaseMember->createRow(); $releaseMemberData['NRM_ReleaseID'] = $release['NR_ID']; $releaseMemberData['NRM_MemberID'] = $members[$k]['GP_MemberID']; $releaseMemberData['NRM_DateTimeReceived'] = date('Y-m-d H:i:s'); $insert = $releaseMemberData->save(); $i++; } else { array_push($failedEmailAddress, array('fname' => $members[$k]['GP_FirstName'], 'lname' => $members[$k]['GP_LastName'], 'email' => $members[$k]['GP_Email'])); } } catch (Exception $exc) { $index = $releaseID . '-'; $index .= $k + 1 . '-'; $index .= $members[$k]['GP_MemberID']; $mailLog[$index]['message'] = $exc->getCode() . '-' . $exc->getFile() . '-' . $exc->getLine(); $mailLog[$index]['log'] = $protocol->getResponse(); $columnsMap = array('NEL_IdIndex' => 'NEL_IdIndex', 'NEL_CodeFileLine' => 'NEL_CodeFileLine', 'NEL_Response' => 'NEL_Response', 'NEL_Timestamp' => 'timestamp'); $writer = new Zend_Log_Writer_Db($this->_db, 'Newsletter_ErrorsLog', $columnsMap); $oZLog = new Zend_Log($writer); $oZLog->setEventItem('NEL_IdIndex', $index); $oZLog->setEventItem('NEL_CodeFileLine', $mailLog[$index]['message']); $oZLog->setEventItem('NEL_Response', $mailLog[$index]['log'][0]); $oZLog->log('errors', 4); } } $protocol->quit(); $protocol->disconnect(); } else { echo Zend_Json::encode(array('sentTo' => '0', 'targetedTotal' => '0', 'failedEmail' => array(), 'select' => $selection)); break; } $stats['sentTo'] = $sentToCount; $dateTimeEnd = date('Y-m-d H:i:s'); $release['NR_MailingDateTimeStart'] = $dateTimeStart; $release['NR_MailingDateTimeEnd'] = $dateTimeEnd; $release['NR_SendTo'] = $stats['action'] == 'set' ? $stats['sentTo'] : $release['NR_SendTo'] + $stats['sentTo']; $release['NR_TargetedTotal'] = $stats['action'] == 'set' ? $stats['targetedTotal'] : $release['NR_TargetedTotal'] + $stats['targetedTotal']; $release['NR_Status'] = 1; $release->save(); if (count($failedEmailAddress) > 0) { $this->_recordEmails($failedEmailAddress, $releaseID); } $this->_stats = array('invalidEmails' => count($failedEmailAddress), 'errors' => count($mailLog), 'totalToSend' => $stats['targetedTotal'], 'totalSent' => $stats['sentTo']); if ($this->_isXmlHttpRequest) { array_push($listSent, $release['NR_Title']); array_push($listIds, $release['NR_ID']); array_push($listDest, $release['NR_AdminEmail']); $data = array('list' => $listSent, 'dest' => $listDest, 'ids' => $listIds); $this->_adminNotification($data); $this->_logSending($data); } echo Zend_Json::encode(array('sentTo' => $stats['sentTo'], 'targetedTotal' => $stats['targetedTotal'], 'failedEmail' => $failedEmailAddress, 'select' => $selection)); exit; } // If all the newsletter have a status = 1, we don't pass through the foreach // So redirect the action to the newsletter // echo(Zend_Json::encode(array('sentTo' => '0', 'targetedTotal' => '0', 'failedEmail' => array(), 'select' => ''))); // exit; }