Exemplo n.º 1
0
 /**
  * Get the Transport object for this message (Based on the account)
  * 
  * @return mixed Transport/\GO\Email\Transport
  */
 public function getTransport()
 {
     if (!$this->hasAccount()) {
         return Transport::newGoInstance();
     } else {
         return \GO\Email\Transport::newGoInstance($this->_account);
     }
 }
Exemplo n.º 2
0
 /**
  *
  * @todo Save to sent items should be implemented as a Swift outputstream for better memory management
  * @param type $params
  * @return boolean
  */
 protected function actionSend($params)
 {
     //HARDCODE FIX FOR RV ERROR
     $params['forward_mailbox'] = "";
     $params['forward_uid'] = 0;
     GO::session()->closeWriting();
     $response['success'] = true;
     $response['feedback'] = '';
     $alias = \GO\Email\Model\Alias::model()->findByPk($params['alias_id']);
     $account = Account::model()->findByPk($alias->account_id);
     $message = new \GO\Base\Mail\SmimeMessage();
     $tag = $this->_createAutoLinkTagFromParams($params, $account);
     if (!empty($tag)) {
         if ($params['content_type'] == 'html') {
             $params['htmlbody'] .= '<div style="display:none">' . $tag . '</div>';
         } else {
             $params['plainbody'] .= "\n\n" . $tag . "\n\n";
         }
     }
     $message->handleEmailFormInput($params);
     $recipientCount = $message->countRecipients();
     if (!$recipientCount) {
         throw new \Exception(GO::t('feedbackNoReciepent', 'email'));
     }
     $message->setFrom($alias->email, $alias->name);
     $mailer = \GO\Base\Mail\Mailer::newGoInstance(\GO\Email\Transport::newGoInstance($account));
     $logger = new \Swift_Plugins_Loggers_ArrayLogger();
     $mailer->registerPlugin(new \Swift_Plugins_LoggerPlugin($logger));
     $this->fireEvent('beforesend', array(&$this, &$response, &$message, &$mailer, $account, $alias, $params));
     $failedRecipients = array();
     $success = $mailer->send($message, $failedRecipients);
     // Update "last mailed" time of the emailed contacts.
     if ($success && GO::modules()->addressbook) {
         $toAddresses = $message->getTo();
         if (empty($toAddresses)) {
             $toAddresses = array();
         }
         $ccAddresses = $message->getCc();
         if (empty($ccAddresses)) {
             $ccAddresses = array();
         }
         $bccAddresses = $message->getBcc();
         if (empty($bccAddresses)) {
             $bccAddresses = array();
         }
         $emailAddresses = array_merge($toAddresses, $ccAddresses);
         $emailAddresses = array_merge($emailAddresses, $bccAddresses);
         foreach ($emailAddresses as $emailAddress => $fullName) {
             $findCriteria = \GO\Base\Db\FindCriteria::newInstance()->addCondition('email', $emailAddress, '=', 't', false)->addCondition('email2', $emailAddress, '=', 't', false)->addCondition('email3', $emailAddress, '=', 't', false);
             $findParams = \GO\Base\Db\FindParams::newInstance()->criteria($findCriteria);
             $contactsStmt = \GO\Addressbook\Model\Contact::model()->find($findParams);
             if ($contactsStmt) {
                 foreach ($contactsStmt as $contactModel) {
                     if ($contactModel->name == $fullName) {
                         $contactLastMailTimeModel = \GO\Email\Model\ContactMailTime::model()->findSingleByAttributes(array('contact_id' => $contactModel->id, 'user_id' => GO::user()->id));
                         if (!$contactLastMailTimeModel) {
                             $contactLastMailTimeModel = new \GO\Email\Model\ContactMailTime();
                             $contactLastMailTimeModel->contact_id = $contactModel->id;
                             $contactLastMailTimeModel->user_id = GO::user()->id;
                         }
                         $contactLastMailTimeModel->last_mail_time = time();
                         $contactLastMailTimeModel->save();
                     }
                 }
             }
         }
     }
     if (!empty($params['reply_uid'])) {
         //set \Answered flag on IMAP message
         GO::debug("Reply");
         $account2 = Account::model()->findByPk($params['reply_account_id']);
         $imap = $account2->openImapConnection($params['reply_mailbox']);
         $imap->set_message_flag(array($params['reply_uid']), "\\Answered");
     }
     if (!empty($params['forward_uid'])) {
         //set forwarded flag on IMAP message
         $account2 = Account::model()->findByPk($params['forward_account_id']);
         $imap = $account2->openImapConnection($params['forward_mailbox']);
         $imap->set_message_flag(array($params['forward_uid']), "\$Forwarded");
     }
     /**
      * if you want ignore default sent folder message will be store in
      * folder wherefrom user sent it
      */
     if ($account->ignore_sent_folder && !empty($params['reply_mailbox'])) {
         $account->sent = $params['reply_mailbox'];
     }
     if ($account->sent && $recipientCount > count($failedRecipients)) {
         GO::debug("Sent");
         //if a sent items folder is set in the account then save it to the imap folder
         $imap = $account->openImapConnection($account->sent);
         if (!$imap->append_message($account->sent, $message, "\\Seen")) {
             $response['success'] = false;
             $response['feedback'] .= 'Failed to save send item to ' . $account->sent;
         }
     }
     if (!empty($params['draft_uid'])) {
         //remove drafts on send
         $imap = $account->openImapConnection($account->drafts);
         $imap->delete(array($params['draft_uid']));
     }
     if (count($failedRecipients)) {
         $msg = GO::t('failedRecipients', 'email') . ': ' . implode(', ', $failedRecipients) . '<br /><br />';
         $logStr = $logger->dump();
         preg_match('/<< 55[0-9] .*>>/s', $logStr, $matches);
         if (isset($matches[0])) {
             $logStr = trim(substr($matches[0], 2, -2));
         }
         throw new \Exception($msg . nl2br($logStr));
     }
     //if there's an autolink tag in the message we want to link outgoing messages too.
     $tags = $this->_findAutoLinkTags($params['content_type'] == 'html' ? $params['htmlbody'] : $params['plainbody'], $account->id);
     $this->_link($params, $message, false, $tags);
     $response['unknown_recipients'] = $this->_findUnknownRecipients($params);
     return $response;
 }
Exemplo n.º 3
0
 protected function actionBatchSend($params)
 {
     $this->requireCli();
     $this->_sentEmails = array();
     \GO::$disableModelCache = true;
     $mailing = \GO\Addressbook\Model\SentMailing::model()->findByPk($params['mailing_id']);
     if (!$mailing) {
         throw new \Exception("Mailing not found!\n");
     }
     \GO::session()->runAs($mailing->user_id);
     echo 'Status: ' . $mailing->status . "\n";
     if (empty($mailing->status)) {
         echo "Starting mailing at " . \GO\Base\Util\Date::get_timestamp(time()) . "\n";
         $mailing->reset();
     } elseif (!empty($params['restart'])) {
         echo "Restarting mailing at " . \GO\Base\Util\Date::get_timestamp(time()) . "\n";
         $mailing->reset();
     } elseif ($mailing->status == \GO\Addressbook\Model\SentMailing::STATUS_PAUSED) {
         echo "Resuming mailing at " . \GO\Base\Util\Date::get_timestamp(time()) . "\n";
         $mailing->status = \GO\Addressbook\Model\SentMailing::STATUS_RUNNING;
         $mailing->save();
     }
     $htmlToText = new \GO\Base\Util\Html2Text();
     //$addresslist = \GO\Addressbook\Model\Addresslist::model()->findByPk($mailing->addresslist_id);
     $mimeData = file_get_contents(\GO::config()->file_storage_path . $mailing->message_path);
     $message = \GO\Base\Mail\Message::newInstance()->loadMimeMessage($mimeData);
     $joinCriteria = \GO\Base\Db\FindCriteria::newInstance()->addRawCondition('t.id', 'a.account_id');
     $findParams = \GO\Base\Db\FindParams::newInstance()->single()->join(\GO\Email\Model\Alias::model()->tableName(), $joinCriteria, 'a')->criteria(\GO\Base\Db\FindCriteria::newInstance()->addCondition('id', $mailing->alias_id, '=', 'a'));
     if ($mailing->campaign_id > 0 && \GO::modules()->isAvailable('campaigns')) {
         $account = new \GO\Email\Model\Account();
         $account->username = \GO::config()->campaigns_imap_user;
         $account->password = \GO::config()->campaigns_imap_pass;
         $account->host = \GO::config()->campaigns_imap_server;
         $account->port = \GO::config()->campaigns_imap_port;
         $account->smtp_username = \GO::config()->campaigns_smtp_user;
         $account->smtp_password = \GO::config()->campaigns_smtp_pass;
         $account->smtp_host = \GO::config()->campaigns_smtp_server;
         $account->smtp_port = \GO::config()->campaigns_smtp_port;
         $message->setFrom(\GO::config()->campaigns_from);
     } else {
         $account = \GO\Email\Model\Account::model()->find($findParams);
         if (!$account->store_password && !empty($mailing->temp_pass)) {
             $account->smtp_password = $mailing->temp_pass;
         }
     }
     $mailer = \GO\Base\Mail\Mailer::newGoInstance(\GO\Email\Transport::newGoInstance($account));
     echo "Will send emails from " . $account->username . ".\n";
     if (empty(\GO::config()->mailing_messages_per_minute)) {
         \GO::config()->mailing_messages_per_minute = 30;
     }
     //Rate limit to 100 emails per-minute
     $mailer->registerPlugin(new \Swift_Plugins_ThrottlerPlugin(\GO::config()->mailing_messages_per_minute, \Swift_Plugins_ThrottlerPlugin::MESSAGES_PER_MINUTE));
     // Use AntiFlood to re-connect after 50 emails
     $mailer->registerPlugin(new \Swift_Plugins_AntiFloodPlugin(\GO::config()->mailing_messages_per_minute));
     echo 'Sending a maximum of ' . \GO::config()->mailing_messages_per_minute . ' messages per minute' . "\n";
     $failedRecipients = array();
     $bodyWithTags = $message->getBody();
     foreach ($mailing->contacts as $contact) {
         $sentMailingContactModel = \GO\Addressbook\Model\SentMailingContact::model()->findSingleByAttributes(array('sent_mailing_id' => $mailing->id, 'contact_id' => $contact->id));
         if (!$sentMailingContactModel->sent) {
             $errors = 1;
             $unsubscribeHref = \GO::url('addressbook/sentMailing/unsubscribe', array('addresslist_id' => $mailing->addresslist_id, 'contact_id' => $contact->id, 'token' => md5($contact->ctime . $contact->addressbook_id . $contact->firstEmail)), false, true);
             $body = str_replace('%unsubscribe_href%', $unsubscribeHref, $bodyWithTags);
             //curly brackets don't work inside links in browser wysiwyg editors.
             $templateModel = \GO\Addressbook\Model\Template::model();
             $templateModel->htmlSpecialChars = false;
             $body = $templateModel->replaceCustomTags($body, array('unsubscribe_link' => '<a href="' . $unsubscribeHref . '" target="_blank">' . \GO::t("unsubscription", "addressbook") . '</a>'), true);
             $templateModel->htmlSpecialChars = true;
             try {
                 if (!$contact->email_allowed) {
                     echo "Skipping contact " . $contact->firstEmail . " because newsletter sending is disabled in the addresslists tab.\n\n";
                 } elseif (empty($contact->firstEmail)) {
                     echo "Skipping contact " . $contact->name . " no e-mail address was set.\n\n";
                 } else {
                     $body = \GO\Addressbook\Model\Template::model()->replaceContactTags($body, $contact);
                     $message->setTo($contact->firstEmail, $contact->name);
                     $message->setBody($body);
                     $plainTextPart = $message->findPlainTextBody();
                     if ($plainTextPart) {
                         $htmlToText->set_html($body);
                         $plainTextPart->setBody($htmlToText->get_text());
                     }
                     // Check mail limit
                     $nSentMails = \GO::config()->get_setting('campaigns_number_sent_mails', 0);
                     if ($mailing->campaign_id > 0 && $nSentMails >= \GO::config()->campaigns_max_mails_per_period) {
                         $this->_pauseMailing($mailing->id);
                         echo "Error for " . $contact->firstEmail . ": \n";
                         echo str_replace('%maxMails', \GO::config()->campaigns_max_mails_per_period, \GO::t('sentMailLimitReached', 'campaigns'));
                         exit;
                     }
                     $this->_sendmail($message, $contact, $mailer, $mailing);
                     \GO::config()->save_setting('campaigns_number_sent_mails', $nSentMails + 1, 0);
                     $errors = 0;
                 }
             } catch (\Exception $e) {
                 echo "Error for " . $contact->firstEmail . ": " . $e->getMessage() . "\n";
             }
             if ($errors) {
                 $mailing->errors++;
                 $mailing->save();
             }
         }
     }
     foreach ($mailing->companies as $company) {
         $sentMailingCompanyModel = \GO\Addressbook\Model\SentMailingCompany::model()->findSingleByAttributes(array('sent_mailing_id' => $mailing->id, 'company_id' => $company->id));
         if (!$sentMailingCompanyModel->sent) {
             $errors = 1;
             $unsubscribeHref = \GO::url('addressbook/sentMailing/unsubscribe', array('addresslist_id' => $mailing->addresslist_id, 'company_id' => $company->id, 'token' => md5($company->ctime . $company->addressbook_id . $company->email)), true, true);
             $body = str_replace('%unsubscribe_href%', $unsubscribeHref, $bodyWithTags);
             //curly brackets don't work inside links in browser wysiwyg editors.
             $body = \GO\Addressbook\Model\Template::model()->replaceCustomTags($body, array('unsubscribe_link' => '<a href="' . $unsubscribeHref . '">' . \GO::t("unsubscription", "addressbook") . '</a>'), true);
             try {
                 if (!$company->email_allowed) {
                     echo "Skipping company " . $company->email . " because newsletter sending is disabled in the addresslists tab.\n\n";
                 } elseif (empty($company->email)) {
                     echo "Skipping company " . $company->name . " no e-mail address was set.\n\n";
                 } else {
                     $body = \GO\Addressbook\Model\Template::model()->replaceModelTags($body, $company);
                     $message->setTo($company->email, $company->name);
                     $message->setBody($body);
                     $plainTextPart = $message->findPlainTextBody();
                     if ($plainTextPart) {
                         $htmlToText->set_html($body);
                         $plainTextPart->setBody($htmlToText->get_text());
                     }
                     // Check mail limit
                     $nSentMails = \GO::config()->get_setting('campaigns_number_sent_mails', 0);
                     if ($mailing->campaign_id > 0 && $nSentMails >= \GO::config()->campaigns_max_mails_per_period) {
                         $this->_pauseMailing($mailing->id);
                         echo "Error for " . $contact->firstEmail . ": \n";
                         echo str_replace('%maxMails', \GO::config()->campaigns_max_mails_per_period, \GO::t('sentMailLimitReached', 'campaigns'));
                         exit;
                     }
                     $this->_sendmail($message, $company, $mailer, $mailing);
                     \GO::config()->save_setting('campaigns_number_sent_mails', $nSentMails + 1, 0);
                     $errors = 0;
                 }
             } catch (\Exception $e) {
                 echo "Error for " . $company->email . ": " . $e->getMessage() . "\n";
             }
             if ($errors) {
                 $mailing->errors++;
                 $mailing->save();
             }
         }
     }
     $mailing->status = \GO\Addressbook\Model\SentMailing::STATUS_FINISHED;
     // Unset the temp_pass
     if (!empty($mailing->temp_pass)) {
         $mailing->temp_pass = "";
     }
     $mailing->save();
     echo "Mailing finished at " . \GO\Base\Util\Date::get_timestamp(time()) . "\n";
 }