Пример #1
0
 static function sendNewsletterMail($newsletter, $sendPreview = false, $previewFormat = false, $receiverLimit = null)
 {
     $sendMailSettings = eZINI::instance('ezsendmailsettings.ini');
     $replaceMsgIDHost = $sendMailSettings->variable('SendNewsletter', 'ReplaceMessageIDHost');
     $newSendHost = $sendMailSettings->variable('SendNewsletter', 'Host');
     $hostSettings['replace'] = $replaceMsgIDHost;
     $hostSettings['host'] = $newSendHost;
     $mail = new eZNewsletterMail();
     $sys = eZSys::instance();
     $newsletterMailData = array();
     // Check that the newsletter type exists, if not, process next newsletter
     if (!$newsletter->attribute('newsletter_type')) {
         return;
     }
     $newsletterMailData[eZNewsletter::OutputFormatText] = $newsletter->generateNewsletter(eZNewsletter::OutputFormatText, $sendPreview);
     $newsletterMailData[eZNewsletter::OutputFormatHTML] = $newsletter->generateNewsletter(eZNewsletter::OutputFormatHTML, $sendPreview);
     $newsletterMailData[eZNewsletter::OutputFormatExternalHTML] = $newsletter->generateNewsletter(eZNewsletter::OutputFormatExternalHTML, $sendPreview);
     $newsletterMailData[eZNewsletter::OutputFormatSMS] = $newsletter->generateNewsletter(eZNewsletter::OutputFormatSMS, $sendPreview);
     $newsletterOutputFormatList = $newsletter->attribute('output_format_list');
     $noMimeMessage = "This message is in MIME format. Since your mail reader does not understand\nthis format, some or all of this message may not be legible.";
     $lineBreak = "\r\n";
     $partCounter = 0;
     $boundary = date("YmdGHjs") . ':' . getmypid() . ':' . $partCounter++;
     $charset = eZTextCodec::internalCharset();
     $contentTypeHtmlPart = "Content-Type: text/html; charset={$charset}";
     foreach (array(eZNewsletter::OutputFormatHTML, eZNewsletter::OutputFormatExternalHTML) as $key) {
         $htmlOutput =& $newsletterMailData[$key];
         if ($htmlOutput['imageNameMap']) {
             $data = $noMimeMessage . $lineBreak;
             $data .= $lineBreak . '--' . $boundary . $lineBreak;
             $data .= $contentTypeHtmlPart . $lineBreak;
             $data .= "Content-Transfer-Encoding: 8bit" . $lineBreak . $lineBreak;
             $data .= $htmlOutput['body'] . $lineBreak;
             foreach ($htmlOutput['imageNameMap'] as $id => $filename) {
                 $filename = trim($filename);
                 if (is_readable($filename)) {
                     $mime = eZMimeType::findByURL($filename);
                     $encodedFileContent = chunk_split(base64_encode(file_get_contents($filename)), 76, $lineBreak);
                     $data .= $lineBreak . '--' . $boundary . $lineBreak;
                     $data .= "Content-Type: " . $mime['name'] . ';' . $lineBreak . ' name="' . basename($filename) . '"' . $lineBreak;
                     $data .= "Content-ID: <" . $id . ">" . $lineBreak;
                     $data .= "Content-Transfer-Encoding: base64" . $lineBreak;
                     $original_filename = basename($filename);
                     if ($htmlOutput['imageNameMapName'][$id]) {
                         $original_filename = $htmlOutput['imageNameMapName'][$id];
                     }
                     $data .= 'Content-Disposition: INLINE;' . $lineBreak . ' filename="' . $original_filename . '"' . $lineBreak . $lineBreak;
                     $data .= $encodedFileContent;
                 }
             }
             $data .= $lineBreak . '--' . $boundary . '--';
             $htmlOutput['body'] = $data;
         } else {
             $data = $noMimeMessage . $lineBreak;
             $data .= $lineBreak . '--' . $boundary . $lineBreak;
             $data .= $contentTypeHtmlPart . $lineBreak;
             $data .= "Content-Transfer-Encoding: 8bit" . $lineBreak . $lineBreak;
             $data .= $htmlOutput['body'] . $lineBreak;
             $data .= $lineBreak . '--' . $boundary . '--';
             $htmlOutput['body'] = $data;
         }
     }
     // 4. Go through revceivers, and send emails.
     if (!$sendPreview) {
         $mail->setSender($newsletterMailData[eZNewsletter::OutputFormatText]['emailSender'], $newsletterMailData[eZNewsletter::OutputFormatText]['emailSenderName']);
         $idcounter = 0;
         $sendCount = 0;
         $skipCount = 0;
         // HACK!
         $receiverList = eZSendNewsletterItem::fetchByNewsletterID($newsletter->attribute('id'), 0, $receiverLimit);
         foreach ($receiverList as $receiver) {
             $msgid = eZNewsletter::generateMessageId($newsletterMailData[eZNewsletter::OutputFormatText]['emailSender'], $receiver->attribute('id'), $idcounter++, $hostSettings);
             $mail->setMessageID($msgid);
             $userData = $receiver->attribute('user_data');
             if (!$userData) {
                 //When no userdata is found, it is usually the result of a deleted subscription,
                 //we mark the mail as being sent, without sending it.
                 $receiver->setAttribute('send_status', eZSendNewsletterItem::SendStatusSent);
                 $receiver->setAttribute('send_ts', time());
                 $receiver->sync();
                 continue;
             }
             // #TODO# IDs expected
             $userOutputFormatList = explode(',', $userData['output_format']);
             // #TODO#
             #echo " ### userOutputFormatList\n";  ok
             #var_dump( $userOutputFormatList );
             #echo " ### newsletterOutputFormatList\n"; ok
             #var_dump( $newsletterOutputFormatList );
             $outputFormat = false;
             //special case for SMS sending
             if (in_array(eZNewsletter::OutputFormatSMS, $userOutputFormatList) && in_array(eZNewsletter::OutputFormatSMS, $newsletterOutputFormatList)) {
                 $mail->setContentType("sms", false, false, false, $boundary);
                 $outputFormat = eZNewsletter::OutputFormatSMS;
                 //$mail->setSubject( $userMailData['subject'] );                                        ### $userMailData is undefined
                 # echo " ### userMailData\n";
                 # var_dump( $userMailData );
                 # $mail->setSubject( $userMailData['subject'] );
                 # $mail->setReceiver( $userData['email'] );
                 # $mail->setMobile( $userData['mobile'] );
                 //$mail->setBody( $userMailData['body'] );                                              ### $userMailData is undefined
                 # $mail->setBody( $userMailData['body'] );
                 # $mail->setDateTimestamp( $newsletter->attribute( 'send_date') );
                 $mailResult = eZNewsletterMailTransport::send($mail, false);
             }
             //send regular emails
             if (in_array(eZNewsletter::OutputFormatHTML, $userOutputFormatList) && in_array(eZNewsletter::OutputFormatHTML, $newsletterOutputFormatList)) {
                 $mail->setContentType("multipart/related", false, false, false, $boundary);
                 $outputFormat = eZNewsletter::OutputFormatHTML;
             }
             if (in_array(eZNewsletter::OutputFormatExternalHTML, $userOutputFormatList) && in_array(eZNewsletter::OutputFormatExternalHTML, $newsletterOutputFormatList)) {
                 $mail->setContentType("multipart/related", false, false, false, $boundary);
                 $outputFormat = eZNewsletter::OutputFormatExternalHTML;
             }
             // ...
             if ($outputFormat === false) {
                 $outputIntersect = array_intersect($userOutputFormatList, $newsletterOutputFormatList);
                 if (count($outputIntersect) > 0) {
                     $outputFormat = $outputIntersect[0];
                 }
             }
             if ($outputFormat !== false) {
                 //personalize if set in type
                 $newsletter_type = eZNewsletterType::fetch($newsletter->attribute('newslettertype_id'));
                 if ($newsletter_type->attribute('personalise') === '1') {
                     $userMailData = eZNewsletter::personalize($newsletterMailData[$outputFormat], $userData, true);
                 } else {
                     $userMailData = eZNewsletter::personalize($newsletterMailData[$outputFormat], $userData, false);
                 }
                 $mail->setSubject($userMailData['subject']);
                 $mail->setReceiver($userData['email']);
                 $mail->setMobile($userData['mobile']);
                 $mail->setBody($userMailData['body']);
                 $mail->setDateTimestamp($newsletter->attribute('send_date'));
                 //if only SMS was selected, don't send email
                 if (!(in_array(eZNewsletter::OutputFormatSMS, $userOutputFormatList) && count($userOutputFormatList) == 1)) {
                     $mailResult = eZNewsletterMailTransport::send($mail, false);
                 }
                 $sendCount++;
             } else {
                 // User doesnt want any format we defined - skipped
                 $skipCount++;
             }
             $receiver->setAttribute('send_status', eZSendNewsletterItem::SendStatusSent);
             $receiver->setAttribute('send_ts', time());
             $receiver->sync();
         }
         //send SMS messages
         $instance = eZSMS::instance();
         if ($instance->countNumbers() > 0) {
             echo "Preparing to send " . $instance->countNumbers() . " SMS messages..." . "\n";
             $instance->setContent($newsletterMailData[eZNewsletter::OutputFormatSMS]['body']);
             foreach ($instance->getNumbers() as $number) {
                 echo "Recipient is: " . $number . "\n";
             }
             $reply = $instance->sendMessages();
             if ($reply != "") {
                 echo "SMS Reply:" . "\n";
                 echo $reply;
             }
         }
         //}
         return array('sendCount' => $sendCount, 'skipCount' => $skipCount);
     } else {
         //send preview
         $msgid = eZNewsletter::generateMessageId($newsletterMailData[eZNewsletter::OutputFormatText]['emailSender'], 0, 0, $hostSettings);
         $mail->setMessageID($msgid);
         $userOutputFormatList = $previewFormat;
         $outputFormat = false;
         //special case for SMS sending
         if (in_array(eZNewsletter::OutputFormatSMS, $userOutputFormatList)) {
             $mail->setContentType("sms", false, false, false, $boundary);
             $outputFormat = eZNewsletter::OutputFormatSMS;
             $newsletterMail = $newsletterMailData[eZNewsletter::OutputFormatSMS];
             $mail->setSender($newsletterMail['emailSender'], $newsletterMail['emailSenderName']);
             $mail->setReceiver($newsletter->attribute('preview_email'));
             $mail->setMobile($newsletter->attribute('preview_mobile'));
             $mail->setBody($newsletterMail['body']);
             $mail->setSubject($newsletterMail['subject']);
             $mail->setDateTimestamp($newsletter->attribute('send_date'));
             $mailResult = eZNewsletterMailTransport::send($mail, true);
         }
         //send regular emails
         if (in_array(eZNewsletter::OutputFormatHTML, $userOutputFormatList)) {
             $mail->setContentType("multipart/related", false, false, false, $boundary);
             $outputFormat = eZNewsletter::OutputFormatHTML;
         }
         if (in_array(eZNewsletter::OutputFormatExternalHTML, $userOutputFormatList)) {
             $mail->setContentType("multipart/related", false, false, false, $boundary);
             $outputFormat = eZNewsletter::OutputFormatExternalHTML;
         }
         if ($outputFormat === false) {
             $outputIntersect = array_intersect($userOutputFormatList, $newsletterOutputFormatList);
             if (count($outputIntersect) > 0) {
                 $outputFormat = $outputIntersect[0];
             }
         }
         if ($outputFormat === false) {
             $outputFormat = $newsletterOutputFormatList[0];
             if ($outputFormat == eZNewsletter::OutputFormatHTML || $outputFormat == eZNewsletter::OutputFormatExternalHTML) {
                 $mail->setContentType("multipart/related", false, false, false, $boundary);
             }
         }
         $user = eZUser::currentUser();
         $userObject = $user->attribute('contentobject');
         //personalize if set in type
         $newsletter_type = eZNewsletterType::fetch($newsletter->attribute('newslettertype_id'));
         if ($newsletter_type->attribute('personalise') === '1') {
             $newsletterMail = $newsletter->personalize($newsletterMailData[$outputFormat], array('name' => $userObject->attribute('name')), true);
         } else {
             $newsletterMail = $newsletter->personalize($newsletterMailData[$outputFormat], array('name' => $userObject->attribute('name')), false);
         }
         $mail->setSender($newsletterMail['emailSender']);
         $mail->setReceiver($newsletter->attribute('preview_email'));
         $mail->setMobile($newsletter->attribute('preview_mobile'));
         $mail->setBody($newsletterMail['body']);
         $mail->setSubject($newsletterMail['subject']);
         $mail->setDateTimestamp($newsletter->attribute('send_date'));
         //if only SMS was selected, don't send email
         if (!(in_array(eZNewsletter::OutputFormatSMS, $userOutputFormatList) && count($userOutputFormatList) == 1)) {
             $mailResult = eZNewsletterMailTransport::send($mail, true);
         }
         //send SMS messages
         $instance = eZSMS::instance();
         if ($instance->countNumbers() > 0) {
             //echo "Preparing to send ".$instance->countNumbers()." SMS messages..."."\n";
             $instance->setContent($newsletterMailData[eZNewsletter::OutputFormatSMS]['body']);
             $reply = $instance->sendMessages();
             if ($reply != "") {
                 echo "SMS Reply:" . "\n";
                 echo $reply;
             }
         }
     }
 }