Concrete child classes should may focus on implementing the BaseMailer::sendMessage method.
See also: BaseMessage
Since: 2.0
Author: Paul Klimov (klimov.paul@gmail.com)
Inheritance: extends yii\base\Component, implements yii\mail\MailerInterface, implements yii\base\ViewContextInterface
Ejemplo n.º 1
0
 /**
  * @inheritdoc
  */
 public function init()
 {
     parent::init();
     Event::on(BaseMailer::className(), BaseMailer::EVENT_AFTER_SEND, function ($event) {
         AuditMail::record($event);
     });
 }
Ejemplo n.º 2
0
 /**
  * @inheritdoc
  */
 public function init()
 {
     parent::init();
     if ($this->client === null) {
         throw new InvalidConfigException('The "client" property must be set.');
     }
     if (!$this->client instanceof SesClient) {
         $this->client = new SesClient($this->client);
     }
 }
Ejemplo n.º 3
0
 public function init()
 {
     parent::init();
     Event::on(BaseMailer::className(), BaseMailer::EVENT_AFTER_SEND, function ($event) {
         $message = $event->message->getSwiftMessage();
         $textBody = $message->getBody();
         $fileName = $event->sender->generateMessageFileName();
         FileHelper::createDirectory(Yii::getAlias($this->mailPath));
         file_put_contents(Yii::getAlias($this->mailPath) . '/' . $fileName, $message->toString());
         $this->_messages[] = ['isSuccessful' => $event->isSuccessful, 'time' => $message->getDate(), 'headers' => $message->getHeaders(), 'from' => $this->convertParams($message->getFrom()), 'to' => $this->convertParams($message->getTo()), 'reply' => $this->convertParams($message->getReplyTo()), 'cc' => $this->convertParams($message->getCc()), 'bcc' => $this->convertParams($message->getBcc()), 'subject' => $message->getSubject(), 'body' => $textBody, 'charset' => $message->getCharset(), 'file' => $fileName];
     });
 }
Ejemplo n.º 4
0
 /**
  * This method is invoked right before an action is to be executed (after all possible filters.)
  * It checks the existence of the db and mailer components.
  * @param \yii\base\Action $action the action to be executed.
  * @return boolean whether the action should continue to be executed.
  */
 public function beforeAction($action)
 {
     try {
         if (parent::beforeAction($action)) {
             $this->db = Instance::ensure($this->db, Connection::className());
             $this->mailer = Instance::ensure($this->mailer, BaseMailer::className());
             return true;
         }
     } catch (Exception $e) {
         $this->stderr("ERROR: " . $e->getMessage() . "\n");
     }
     return false;
 }
Ejemplo n.º 5
0
 /**
  * This method is invoked right before an action is to be executed (after all possible filters.)
  * It checks the existence of the db and mailer components.
  * @param \yii\base\Action $action the action to be executed.
  * @return boolean whether the action should continue to be executed.
  */
 public function beforeAction($action)
 {
     try {
         if (parent::beforeAction($action)) {
             $this->db = Instance::ensure($this->db, Connection::className());
             $this->mailer = Instance::ensure($this->mailer, BaseMailer::className());
             return true;
         }
     } catch (Exception $e) {
         Log::error($e->getMessage(), null, __METHOD__);
     }
     return false;
 }
Ejemplo n.º 6
0
 /**
  * @inheritdoc
  */
 public function init()
 {
     if (!isset($this->domain)) {
         throw new InvalidConfigException("Mailer::\$domain must be set to your Mailgun domain");
     }
     if (!isset($this->apiKey)) {
         throw new InvalidConfigException("Mailer::\$apiKey must be set to your Mailgun api key");
     }
     if (is_string($this->httpclient)) {
         $this->httpclient = Yii::$app->get($this->httpclient);
     } else {
         $this->httpclient = Instance::ensure($this->httpclient, Client::className());
     }
     parent::init();
 }
Ejemplo n.º 7
0
 protected function prepare(BaseMailer $mailer)
 {
     $data = unserialize($this->data);
     $options = $data['options'];
     $methods = $data['methods'];
     if (isset($options['compose'])) {
         $mail = $mailer->compose($options['compose']['view'], $options['compose']['params']);
         unset($options['compose']);
     } else {
         $mail = $mailer->compose();
     }
     foreach ($methods as $method => $values) {
         if (method_exists($mail, $method)) {
             $mail->{$method}($values['value'], $values['options']);
         }
     }
     foreach ($options as $option => $val) {
         $method = 'set' . ucfirst($option);
         if (method_exists($mail, $method)) {
             $mail->{$method}($val);
         }
     }
     return $mail;
 }
Ejemplo n.º 8
0
 /**
  * @inheritdoc
  */
 public function init()
 {
     parent::init();
     Event::on(BaseMailer::className(), BaseMailer::EVENT_AFTER_SEND, function ($event) {
         /* @var $message MessageInterface */
         $message = $event->message;
         $messageData = ['isSuccessful' => $event->isSuccessful, 'from' => $this->convertParams($message->getFrom()), 'to' => $this->convertParams($message->getTo()), 'reply' => $this->convertParams($message->getReplyTo()), 'cc' => $this->convertParams($message->getCc()), 'bcc' => $this->convertParams($message->getBcc()), 'subject' => $message->getSubject(), 'charset' => $message->getCharset()];
         // add more information when message is a SwiftMailer message
         if ($message instanceof \yii\swiftmailer\Message) {
             /* @var $swiftMessage \Swift_Message */
             $swiftMessage = $message->getSwiftMessage();
             $messageData['body'] = $swiftMessage->getBody();
             $messageData['time'] = $swiftMessage->getDate();
             $messageData['headers'] = $swiftMessage->getHeaders();
         }
         // store message as file
         $fileName = $event->sender->generateMessageFileName();
         FileHelper::createDirectory(Yii::getAlias($this->mailPath));
         file_put_contents(Yii::getAlias($this->mailPath) . '/' . $fileName, $message->toString());
         $messageData['file'] = $fileName;
         $this->_messages[] = $messageData;
     });
 }
Ejemplo n.º 9
0
 /**
  * Composes the message using a Mandrill template if the useMandrillTemplates
  * settings is true.
  *
  * If mandrill templates are not being used or if no template with the given
  * name has been found it will fallback to the normal compose method.
  *
  * @inheritdoc
  * @since 1.2.0
  */
 public function compose($view = null, array $params = [])
 {
     if ($this->useMandrillTemplates) {
         /** @var Message $message */
         $message = parent::compose();
         $message->setTemplateData($view, $params, $this->templateLanguage);
         if ($this->useTemplateDefaults) {
             $message->enableTemplateDefaults();
         }
         return $message;
     }
     return parent::compose($view, $params);
 }
Ejemplo n.º 10
0
 /**
  * Send email
  * @param int $type email type
  * @param array $params email views params
  */
 public function sendMessage($type, $params)
 {
     if ($this->mailer === null) {
         /** @var yii\swiftmailer\Mailer mailer */
         $this->mailer = Yii::$app->mailer;
         $this->mailer->viewPath = $this->getViewPath() . '/mails';
         $this->mailer->getView()->theme = Yii::$app->view->theme;
     }
     switch ($type) {
         case 'register':
             if ($this->enableRegistrationEmail) {
                 $message = $this->mailer->compose($this->mailViews[$type], $params);
                 $message->setSubject(Yii::t('activeuser_general', 'Thank you for register on site'));
             }
             break;
         case 'confirm':
             if ($this->enableConfirmation) {
                 $message = $this->mailer->compose($this->mailViews[$type], $params);
                 $message->setSubject(Yii::t('activeuser_general', 'Email address confirmation needed'));
             }
             break;
         case 'restore':
             if ($this->enableConfirmation) {
                 $message = $this->mailer->compose($this->mailViews[$type], $params);
                 $message->setSubject(Yii::t('activeuser_general', 'Password restore request'));
             }
             break;
         case 'passchanged':
             if ($this->enableConfirmation) {
                 $message = $this->mailer->compose($this->mailViews[$type], $params);
                 $message->setSubject(Yii::t('activeuser_general', 'Password was changed'));
             }
             break;
         case 'block':
             $message = $this->mailer->compose($this->mailViews[$type], $params);
             $message->setSubject(Yii::t('activeuser_general', 'You are blocked'));
             break;
         case 'unblock':
             $message = $this->mailer->compose($this->mailViews[$type], $params);
             $message->setSubject(Yii::t('activeuser_general', 'You are unblocked'));
             break;
     }
     if (!empty($message)) {
         $user = $params['user'];
         if ($this->sender === null) {
             $this->sender = isset(Yii::$app->params['adminEmail']) ? Yii::$app->params['adminEmail'] : 'no-reply@' . (empty($_SERVER['HTTP_HOST']) ? 'example.com' : $_SERVER['HTTP_HOST']);
         }
         $message->setTo(empty($user->name) ? $user->email : [$user->email => $user->name]);
         $message->setFrom($this->sender);
         $this->mailer->send($message);
     }
 }
Ejemplo n.º 11
0
Archivo: Cms.php Proyecto: Liv1020/cms
 public function init()
 {
     parent::init();
     //Название проекта.
     if (!$this->appName) {
         $this->appName = \Yii::$app->name;
     } else {
         \Yii::$app->name = $this->appName;
     }
     //Генерация файла с подключением настроек extensions если его нет
     if (!file_exists(AUTO_GENERATED_MODULES_FILE)) {
         $this->generateModulesConfigFile();
     }
     //init view theme
     $this->templates = ArrayHelper::merge($this->templatesDefault, (array) $this->templates);
     foreach ($this->templates as $code => $templateData) {
         if ($code == $this->template) {
             if ($pathMap = ArrayHelper::getValue($templateData, 'pathMap')) {
                 if (is_array($pathMap)) {
                     \Yii::$app->view->theme = new Theme(['pathMap' => $pathMap]);
                 }
             }
         }
     }
     //TODO: may be is depricated. While better to use '@app/views/'
     \Yii::setAlias('template', '@app/views/');
     $this->emailTemplates = ArrayHelper::merge($this->emailTemplatesDefault, (array) $this->emailTemplates);
     \Yii::$app->language = $this->languageCode;
     //Отлов событий отправки сообщений с сайта, и их модификация.
     \yii\base\Event::on(\yii\mail\BaseMailer::className(), \yii\mail\BaseMailer::EVENT_BEFORE_SEND, [$this, 'beforeSendEmail']);
     if (\Yii::$app instanceof Application) {
         //console init
         $this->_initConsole();
     } else {
         //web init
         $this->_initWeb();
     }
 }
Ejemplo n.º 12
0
 /**
  * @inheritdoc
  */
 protected function saveMessage($message)
 {
     //we have to generate message first - or [[toString()]] returns empty value
     $this->adapter->preSend();
     return parent::saveMessage($message);
 }
 private function sendPasswordResetEmail($email)
 {
     $user = User::findOne(['status' => User::STATUS_ACTIVE, 'email' => $email]);
     if (!$user) {
         return false;
     }
     $user->password_reset_token = Yii::$app->security->generateRandomString();
     if ($user->save(false)) {
         $mailer = Instance::ensure($this->mailer, BaseMailer::className());
         return $mailer->compose('@gromver/platform/basic/modules/auth/views/emails/passwordResetToken', ['user' => $user])->setFrom(Yii::$app->grom->params['supportEmail'], Yii::t('gromver.platform', '{name} robot', ['name' => Yii::$app->grom->siteName]))->setTo($user->email)->setSubject(Yii::t('gromver.platform', 'Password reset for {name}.', ['name' => Yii::$app->grom->siteName]))->send();
     }
     return false;
 }
Ejemplo n.º 14
0
 /**
  * Creates a new message instance and optionally composes its body content via view rendering.
  *
  * @param string|array $view the view to be used for rendering the message body. This can be:
  *
  * - a string, which represents the view name or path alias for rendering the HTML body of the email.
  *   In this case, the text body will be generated by applying `strip_tags()` to the HTML body.
  * - an array with ('html' and/or 'text' elements) OR 'template' element. The 'html' element refers to the view name or path alias
  *   for rendering the HTML body, while 'text' element is for rendering the text body. For example,
  *   `['html' => 'contact-html', 'text' => 'contact-text']`.
  *   If 'template' key is set, then stored template will be used for email.
  * - null, meaning the message instance will be returned without body content.
  *
  * The view to be rendered can be specified in one of the following formats:
  *
  * - path alias (e.g. "@app/mail/contact");
  * - a relative view name (e.g. "contact") located under [[viewPath]].
  *
  * @param array $params the parameters (name-value pairs) that will be extracted and made available in the view file or template.
  * @return Message message instance.
  */
 public function compose($view = null, array $params = [])
 {
     if (is_array($view) && isset($view['template'])) {
         /** @var Message $message */
         $message = parent::compose();
         $message->setTemplateId($view['template']);
     } else {
         $message = parent::compose($view, $params);
     }
     // make given params also available as substitution data
     $message->setSubstitutionData($params);
     if ($this->sandbox) {
         $message->setSandbox(true);
     }
     // set default message sender email
     if ($this->useDefaultEmail) {
         if (!$message->getFrom()) {
             $message->setFrom($this->defaultEmail);
         }
         if (!$message->getReplyTo()) {
             $message->setReplyTo($this->defaultEmail);
         }
     }
     return $message;
 }
 private function sendPasswordResetEmail($email)
 {
     /** @var User $user */
     $user = User::findOne(['status' => User::STATUS_ACTIVE, 'email' => $email]);
     if (!$user) {
         return false;
     }
     //$user->password_reset_token = Yii::$app->security->generateRandomString();
     $user->generatePasswordResetToken();
     if ($user->save(false)) {
         /** @var \gromver\platform\core\modules\auth\Module $authModule */
         $authModule = $this->module;
         $mailer = Instance::ensure($authModule->mailer, BaseMailer::className());
         return $mailer->compose($authModule->emailLayoutPasswordResetToken, ['user' => $user])->setFrom(Yii::$app->supportEmail)->setTo($user->email)->setSubject(Yii::t('gromver.platform', 'Password reset for {name}.', ['name' => isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME']]))->send();
     }
     return false;
 }
Ejemplo n.º 16
0
 /** @inheritdoc */
 public function init()
 {
     $this->_mandrill = new \Mandrill($this->_token);
     parent::init();
 }