compose() публичный Метод

Creates a new message instance and optionally composes its body content via view rendering.
public compose ( string | array | null $view = null, array $params = [] ) : yii\mail\MessageInterface
$view string | array | null 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. 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']`. - 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]].
$params array the parameters (name-value pairs) that will be extracted and made available in the view file.
Результат yii\mail\MessageInterface message instance.
Пример #1
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;
 }
Пример #2
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);
 }
Пример #3
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);
     }
 }
Пример #4
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;
 }