Ejemplo n.º 1
0
 /**
  * @param UserModel  $user
  * @param EmailModel $emailModel
  * @param array      $variables
  *
  * @throws Exception
  * @return bool
  */
 private function _sendEmail(UserModel $user, EmailModel $emailModel, $variables = array())
 {
     // Get the saved email settings.
     $emailSettings = $this->getSettings();
     if (!isset($emailSettings['protocol'])) {
         throw new Exception(Craft::t('Could not determine how to send the email.  Check your email settings.'));
     }
     // Fire an 'onBeforeSendEmail' event
     $event = new Event($this, array('user' => $user, 'emailModel' => $emailModel, 'variables' => $variables));
     $this->onBeforeSendEmail($event);
     // Is the event giving us the go-ahead?
     if ($event->performAction) {
         // In case a plugin changed any variables in onBeforeSendEmail
         $variables = $event->params['variables'];
         $email = new \PHPMailer(true);
         // Default the charset to UTF-8
         $email->CharSet = 'UTF-8';
         // Add a reply to (if any).  Make sure it’s set before setting From, because email is dumb.
         if (!empty($emailModel->replyTo)) {
             $email->addReplyTo($emailModel->replyTo);
         }
         // Set the "from" information.
         $email->setFrom($emailModel->fromEmail, $emailModel->fromName);
         // Check which protocol we need to use.
         switch ($emailSettings['protocol']) {
             case EmailerType::Gmail:
             case EmailerType::Smtp:
                 $this->_setSmtpSettings($email, $emailSettings);
                 break;
             case EmailerType::Pop:
                 $pop = new \Pop3();
                 if (!isset($emailSettings['host']) || !isset($emailSettings['port']) || !isset($emailSettings['username']) || !isset($emailSettings['password']) || StringHelper::isNullOrEmpty($emailSettings['host']) || StringHelper::isNullOrEmpty($emailSettings['port']) || StringHelper::isNullOrEmpty($emailSettings['username']) || StringHelper::isNullOrEmpty($emailSettings['password'])) {
                     throw new Exception(Craft::t('Host, port, username and password must be configured under your email settings.'));
                 }
                 if (!isset($emailSettings['timeout'])) {
                     $emailSettings['timeout'] = $this->_defaultEmailTimeout;
                 }
                 $pop->authorize($emailSettings['host'], $emailSettings['port'], $emailSettings['timeout'], $emailSettings['username'], $emailSettings['password'], craft()->config->get('devMode') ? 1 : 0);
                 $this->_setSmtpSettings($email, $emailSettings);
                 break;
             case EmailerType::Sendmail:
                 $email->isSendmail();
                 break;
             case EmailerType::Php:
                 $email->isMail();
                 break;
             default:
                 $email->isMail();
         }
         if (!$this->_processTestToEmail($email, 'Address')) {
             $email->addAddress($user->email, $user->getFullName());
         }
         // Add any custom headers
         if (!empty($emailModel->customHeaders)) {
             foreach ($emailModel->customHeaders as $headerName => $headerValue) {
                 $email->addCustomHeader($headerName, $headerValue);
             }
         }
         // Add any BCC's
         if (!empty($emailModel->bcc)) {
             if (!$this->_processTestToEmail($email, 'BCC')) {
                 foreach ($emailModel->bcc as $bcc) {
                     if (!empty($bcc['email'])) {
                         $bccEmail = $bcc['email'];
                         $bccName = !empty($bcc['name']) ? $bcc['name'] : '';
                         $email->addBCC($bccEmail, $bccName);
                     }
                 }
             }
         }
         // Add any CC's
         if (!empty($emailModel->cc)) {
             if (!$this->_processTestToEmail($email, 'CC')) {
                 foreach ($emailModel->cc as $cc) {
                     if (!empty($cc['email'])) {
                         $ccEmail = $cc['email'];
                         $ccName = !empty($cc['name']) ? $cc['name'] : '';
                         $email->addCC($ccEmail, $ccName);
                     }
                 }
             }
         }
         // Add a sender header (if any)
         if (!empty($emailModel->sender)) {
             $email->Sender = $emailModel->sender;
         }
         // Add any string attachments
         if (!empty($emailModel->stringAttachments)) {
             foreach ($emailModel->stringAttachments as $stringAttachment) {
                 $email->addStringAttachment($stringAttachment['string'], $stringAttachment['fileName'], $stringAttachment['encoding'], $stringAttachment['type']);
             }
         }
         // Add any normal disc attachments
         if (!empty($emailModel->attachments)) {
             foreach ($emailModel->attachments as $attachment) {
                 $email->addAttachment($attachment['path'], $attachment['name'], $attachment['encoding'], $attachment['type']);
             }
         }
         $variables['user'] = $user;
         $oldLanguage = craft()->getLanguage();
         // If they have a preferredLocale, use that.
         if ($user->preferredLocale) {
             craft()->setLanguage($user->preferredLocale);
         }
         $email->Subject = craft()->templates->renderString($emailModel->subject, $variables);
         // If they populated an htmlBody, use it.
         if ($emailModel->htmlBody) {
             $renderedHtmlBody = craft()->templates->renderString($emailModel->htmlBody, $variables);
             $email->msgHTML($renderedHtmlBody);
             $email->AltBody = craft()->templates->renderString($emailModel->body, $variables);
         } else {
             // They didn't provide an htmlBody, so markdown the body.
             $renderedHtmlBody = craft()->templates->renderString(StringHelper::parseMarkdown($emailModel->body), $variables);
             $email->msgHTML($renderedHtmlBody);
             $email->AltBody = craft()->templates->renderString($emailModel->body, $variables);
         }
         craft()->setLanguage($oldLanguage);
         if (!$email->Send()) {
             // Fire an 'onSendEmailError' event
             $this->onSendEmailError(new Event($this, array('user' => $user, 'emailModel' => $emailModel, 'variables' => $variables, 'error' => $email->ErrorInfo)));
             throw new Exception(Craft::t('Email error: {error}', array('error' => $email->ErrorInfo)));
         }
         Craft::log('Successfully sent email with subject: ' . $email->Subject, LogLevel::Info);
         // Fire an 'onSendEmail' event
         $this->onSendEmail(new Event($this, array('user' => $user, 'emailModel' => $emailModel, 'variables' => $variables)));
         return true;
     }
     return false;
 }
Ejemplo n.º 2
0
 /**
  * Returns Mail POP3
  *
  * @param string
  * @param string
  * @param string
  * @param int|null
  * @param bool
  * @param bool
  *
  * @return Eden\Mail\Pop3
  */
 public function pop3($host, $user, $pass, $port = null, $ssl = false, $tls = false)
 {
     Argument::i()->test(1, 'string')->test(2, 'string')->test(3, 'string')->test(4, 'int', 'null')->test(5, 'bool')->test(6, 'bool');
     return Pop3::i($host, $user, $pass, $port, $ssl, $tls);
 }
Ejemplo n.º 3
0
    exit;
}
// END CLI KEEPOUT CHECKING
// mailcheck scheduler block
// Checks for new incoming mail.
global $conf;
if (empty($conf['Messaging']['email_server'])) {
    return;
}
loader_import('saf.Mail.Pop3');
loader_import('pear.Mail.mimeDecode');
loader_import('cms.Workspace.Message');
//loader_import ('cms.Workspace.Task');
$wmsg = new WorkspaceMessage();
//$wtsk = new WorkspaceTask ();
$pop3 = new Pop3($conf['Messaging']['email_server'], $conf['Messaging']['email_port']);
if (!$pop3->connect()) {
    echo $pop3->error . "\n";
    return;
}
if (!$pop3->authenticate($conf['Messaging']['email_username'], $conf['Messaging']['email_password'])) {
    echo $pop3->error . "\n";
    return;
}
$messages = $pop3->listMessages();
foreach ($messages as $number => $message) {
    set_time_limit(30);
    $messages[$number]['message'] = $pop3->getMessage($number);
    if ($messages[$number]['message'] === false) {
        echo $pop3->error . "\n";
    }
Ejemplo n.º 4
0
 /**
  * @param UserModel  $user
  * @param EmailModel $emailModel
  * @param array      $variables
  * @throws Exception
  * @return bool
  */
 private function _sendEmail(UserModel $user, EmailModel $emailModel, $variables = array())
 {
     // Get the saved email settings.
     $emailSettings = $this->getSettings();
     if (!isset($emailSettings['protocol'])) {
         throw new Exception(Craft::t('Could not determine how to send the email.  Check your email settings.'));
     }
     $email = new \PhpMailer(true);
     // Default the charset to UTF-8
     $email->CharSet = 'UTF-8';
     // Add a reply to (if any).  Make sure it’s set before setting From, because email is dumb.
     if (!empty($emailModel->replyTo)) {
         $email->AddReplyTo($emailModel->replyTo);
     }
     // Set the "from" information.
     $email->SetFrom($emailModel->fromEmail, $emailModel->fromName);
     // Check which protocol we need to use.
     switch ($emailSettings['protocol']) {
         case EmailerType::Gmail:
         case EmailerType::Smtp:
             $this->_setSmtpSettings($email, $emailSettings);
             break;
         case EmailerType::Pop:
             $pop = new \Pop3();
             if (!isset($emailSettings['host']) || !isset($emailSettings['port']) || !isset($emailSettings['username']) || !isset($emailSettings['password']) || StringHelper::isNullOrEmpty($emailSettings['host']) || StringHelper::isNullOrEmpty($emailSettings['port']) || StringHelper::isNullOrEmpty($emailSettings['username']) || StringHelper::isNullOrEmpty($emailSettings['password'])) {
                 throw new Exception(Craft::t('Host, port, username and password must be configured under your email settings.'));
             }
             if (!isset($emailSettings['timeout'])) {
                 $emailSettings['timeout'] = $this->_defaultEmailTimeout;
             }
             $pop->authorize($emailSettings['host'], $emailSettings['port'], $emailSettings['timeout'], $emailSettings['username'], $emailSettings['password'], craft()->config->get('devMode') ? 1 : 0);
             $this->_setSmtpSettings($email, $emailSettings);
             break;
         case EmailerType::Sendmail:
             $email->IsSendmail();
             break;
         case EmailerType::Php:
             $email->IsMail();
             break;
         default:
             $email->IsMail();
     }
     // If they have the test email config var set to something, use it instead of the supplied email.
     if (($testToEmail = craft()->config->get('testToEmailAddress')) != '') {
         $email->AddAddress($testToEmail, 'Test Email');
     } else {
         $email->AddAddress($user->email, $user->getFullName());
     }
     // Add any BCC's
     if (!empty($emailModel->bcc)) {
         foreach ($emailModel->bcc as $bcc) {
             if (!empty($bcc['email'])) {
                 $bccEmail = $bcc['email'];
                 $bccName = !empty($bcc['name']) ? $bcc['name'] : '';
                 $email->AddBcc($bccEmail, $bccName);
             }
         }
     }
     // Add any CC's
     if (!empty($emailModel->cc)) {
         foreach ($emailModel->cc as $cc) {
             if (!empty($cc['email'])) {
                 $ccEmail = $cc['email'];
                 $ccName = !empty($cc['name']) ? $cc['name'] : '';
                 $email->AddCc($ccEmail, $ccName);
             }
         }
     }
     // Add a sender header (if any)
     if (!empty($emailModel->sender)) {
         $email->Sender = $emailModel->sender;
     }
     // Add any string attachments
     if (!empty($emailModel->stringAttachments)) {
         foreach ($emailModel->stringAttachments as $stringAttachment) {
             $email->AddStringAttachment($stringAttachment['string'], $stringAttachment['fileName'], $stringAttachment['encoding'], $stringAttachment['type']);
         }
     }
     // Add any normal disc attachments
     if (!empty($emailModel->attachments)) {
         foreach ($emailModel->attachments as $attachment) {
             $email->AddAttachment($attachment['path'], $attachment['name'], $attachment['encoding'], $attachment['type']);
         }
     }
     $variables['user'] = $user;
     $email->Subject = craft()->templates->renderString($emailModel->subject, $variables);
     // If they populated an htmlBody, use it.
     if ($emailModel->htmlBody) {
         $renderedHtmlBody = craft()->templates->renderString($emailModel->htmlBody, $variables);
         $email->MsgHTML($renderedHtmlBody);
         $email->AltBody = craft()->templates->renderString($emailModel->body, $variables);
     } else {
         // They didn't provide an htmlBody, so markdown the body.
         $renderedHtmlBody = craft()->templates->renderString(StringHelper::parseMarkdown($emailModel->body), $variables);
         $email->MsgHTML($renderedHtmlBody);
         $email->AltBody = craft()->templates->renderString($emailModel->body, $variables);
     }
     if (!$email->Send()) {
         throw new Exception(Craft::t('Email error: {error}', array('error' => $email->ErrorInfo)));
     }
     return true;
 }