Beispiel #1
0
 /**
  * The main send of one letter to one or mode recipients.
  * The mail content generates for each user
  *
  * TODO: Need to refactor it all to:
  * sendNewsletterToSubscriber($nid, $sid, $options)
  * sentNewsletterToEmail($nid, array $emailAndName, $options)
  * sentLetterToEmail(array $letterData, array $emailAndName, $options)
  * 
  * @param  array $params newsletter_id, subscriber(object), type ('html'|'plain'), tracking(bool)
  *
  * @return object
  * @since  1.0
  */
 public function send($params = null)
 {
     // load letter to send....
     if (empty($params['newsletter_id'])) {
         $msg = 'Newsletter id is absent. There is nothing to send.';
         $this->setError($msg);
         throw new Exception($msg);
     }
     if (empty($params['subscriber'])) {
         $msg = 'Subscriber is absent. There is no one to send.';
         $this->setError($msg);
         throw new Exception($msg);
     }
     if (empty($params['tracking'])) {
         $params['tracking'] = false;
     }
     // Load newsletter...
     $letter = JModel::getInstance('Newsletter', 'NewsletterModelEntity');
     if (!$letter->load($params['newsletter_id'])) {
         $msg = 'Loading letter error or newsletter_id is not defined. Id:' . $params['newsletter_id'];
         $this->setError($msg);
         throw new Exception($msg);
     }
     // Load newsletter's SMTP profile...
     $smtpProfile = JModel::getInstance('Smtpprofile', 'NewsletterModelEntity');
     if (!$smtpProfile->load($letter->smtp_profile_id)) {
         $msg = 'Cant load SMTP profile with id: ' . $letter->smtp_profile_id;
         $this->setError($msg);
         throw new Exception($msg);
     }
     // Load mailbox profile bound to loaded SMTP profile...
     $mailboxProfile = JModel::getInstance('Mailboxprofile', 'NewsletterModelEntity');
     if (!$mailboxProfile->load($smtpProfile->mailbox_profile_id)) {
         LogHelper::addWarning('COM_NEWSLETTER_CANT_LOAD_MAILBOX_CANT_SET_SOME_HEADERS', LogHelper::CAT_MAILER, array('Mailbox profile id' => $smtpProfile->mailbox_profile_id, 'SMTP profile' => $smtpProfile->smtp_profile_name));
     }
     // Now we have newsletter, subscriber, SMTP profile and, probably, Mailbox profile.
     // So we can start to send...
     // Use the phpMailer exceptions
     $sender = new MigurMailerSender(array('exceptions' => true));
     $subscriber = $params['subscriber'];
     $type = MailHelper::filterType(!empty($params['type']) ? $params['type'] : null);
     if (!$type) {
         $msg = 'The type "' . $type . '" is not supported';
         $this->setError($msg);
         throw new Exception($msg);
     }
     // emulate user environment
     SubscriberHelper::saveRealUser();
     if (!SubscriberHelper::emulateUser(array('email' => $subscriber->email))) {
         $msg = 'The user "' . $subscriber->email . '" is absent';
         $this->setError($msg);
         throw new Exception($msg);
     }
     PlaceholderHelper::setPlaceholder('newsletter id', $letter->newsletter_id);
     // render the content of letter for each user
     $letter->content = $this->render(array('type' => $type, 'newsletter_id' => $letter->newsletter_id, 'tracking' => true));
     $letter->subject = $this->renderSubject($letter->subject);
     $letter->encoding = $letter->params->encoding;
     SubscriberHelper::restoreRealUser();
     // Result object
     $res = new StdClass();
     $res->state = false;
     $res->errors = array();
     $res->content = $letter->content;
     if ($letter->content === false) {
         return $res;
     }
     // Add custom headers
     // Set the email to bounce
     if (!empty($mailboxProfile->username)) {
         $sender->AddCustomHeader('Return-Path:' . $mailboxProfile->username);
         $sender->AddCustomHeader('Return-Receipt-To:' . $mailboxProfile->username);
         $sender->AddCustomHeader('Errors-To:' . $mailboxProfile->username);
     }
     // Add info about newsleerter and subscriber
     $sender->AddCustomHeader(MailHelper::APPLICATION_HEADER);
     $sender->AddCustomHeader(MailHelper::EMAIL_NAME_HEADER . ':' . $letter->name);
     $sender->AddCustomHeader(MailHelper::NEWSLETTER_ID_HEADER . ':' . $params['newsletter_id']);
     $sender->AddCustomHeader(MailHelper::SUBSCRIBER_ID_HEADER . ':' . $subscriber->subscriber_id);
     // Get attachments
     $atts = DownloadHelper::getByNewsletterId($params['newsletter_id']);
     if (!$smtpProfile->isJoomlaProfile()) {
         $fromName = $smtpProfile->from_name;
         $fromEmail = $smtpProfile->from_email;
         $toName = $smtpProfile->reply_to_name;
         $toEmail = $smtpProfile->reply_to_email;
     } else {
         $jConfig = new JConfig();
         $fromName = isset($letter->params->from_name) ? $letter->params->from_name : $jConfig->fromname;
         $fromEmail = isset($letter->params->from_email) ? $letter->params->from_email : $jConfig->mailfrom;
         $toName = isset($letter->params->to_name) ? $letter->params->to_name : $jConfig->fromname;
         $toEmail = isset($letter->params->to_email) ? $letter->params->to_email : $jConfig->mailfrom;
     }
     // Check if we dan determine all parameters...
     if (empty($fromName) || empty($fromEmail) || empty($toName) || empty($toEmail)) {
         LogHelper::addWarning('COM_NEWSLETTER_MAILER_CANT_DETERMINE SOME FROMTO', LogHelper::CAT_MAILER, array('From name' => $fromName, 'From email' => $fromEmail, 'Reply to name' => $toName, 'Reply to email' => $toEmail, 'SMTP profile' => $smtpProfile->smtp_profile_name, 'Newsletter' => $letter->name));
     }
     try {
         // send the unique letter to each recipient
         $sendRes = $sender->send(array('letter' => $letter->toObject(), 'attach' => $atts, 'emails' => array($subscriber), 'smtpProfile' => $smtpProfile->toObject(), 'fromName' => $fromName, 'fromEmail' => $fromEmail, 'toName' => $toName, 'toEmail' => $toEmail, 'type' => $type, 'tracking' => $params['tracking']));
         // If sending failed
         if (!$sendRes && !empty($sender->ErrorInfo)) {
             throw new Exception($sender->ErrorInfo);
         }
     } catch (Exception $e) {
         $error = JError::getError('unset');
         if (!empty($error)) {
             $msg = $error->get('message');
             $this->setError($msg);
             $res->errors[] = $msg;
         }
         $res->errors[] = $e->getMessage();
         LogHelper::addError('COM_NEWSLETTER_MAILER_SEND_ERROR', LogHelper::CAT_MAILER, array('Error' => $e->getMessage(), 'Email' => $subscriber->email, 'Mail type' => $type, 'SMTP profile' => $smtpProfile->smtp_profile_name, 'Newsletter' => $letter->name));
         return $res;
     }
     $res->state = true;
     return $res;
 }