Composing Mail

Hi all, Previously I posted questions and code in an attempt to produce results based on the 1st two sections of code in the docs under "Composing Mail Content". I now understand this is a straight forward process as now I have sent emails with this method. The challenge is to now include the additional view parameters to the compose() method.

Model


 public function sendEmail($email)

    {

        return Yii::$app->mailer->compose('service-info',[

        'Name' => Yii::$app->$this->name,

        'Address' => Yii::$this->address, ])        

            ->setTo($email)

            ->setFrom([$this->email => $this->name])

            ->setSubject($this->makeModelColor)

            ->send();

    }

   

common>mail>service-info.php


<?php

use yii\helpers\Html;


/* @var $this \yii\web\View view component instance */

/* @var $message \yii\mail\MessageInterface the message being composed */

/* @var $content string main view render result */

?>

<?php $this->beginPage() ?>

<?php $this->beginBody() ?>

<?= $content ?>

<?php $this->endBody() ?>

<?php $this->endPage() ?>



I do not understand why I’m not receiving an email as $content is predefined and I’ve basically copied the compose method from the docs. If someone could set me straight on this, I would really appreciate it.

Thanks

New approach is listed above.

That code is a layout, not a view :)

In the advanced app, the mailer uses common/mail/layouts/html and common/mail/layouts/text for layouts (unsurprisingly), and then the views are put in common/mail.

An example:




<?php

use yii\helpers\Html;


/* @var $this yii\web\View */

/* @var $user common\models\User */


$resetLink = Yii::$app->urlManager->createAbsoluteUrl(['site/reset-password', 'token' => $user->password_reset_token]);

?>

<div class="password-reset">

	<p>Hello <?= Html::encode($user->username) ?>,</p>


	<p>Follow the link below to reset your password:</p>


	<p><?= Html::a(Html::encode($resetLink), $resetLink) ?></p>

</div>



Just like any other view, in fact.

The problem with email is that the styles need to be inlined.

See https://github.com/yiisoft/yii2-app-advanced/tree/master/common/mail

This is the layout for html:




use yii\helpers\Html;


/* @var $this \yii\web\View view component instance */

/* @var $message \yii\mail\MessageInterface the message being composed */

/* @var $content string main view render result */

?>

<?php $this->beginPage() ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

	<meta http-equiv="Content-Type" content="text/html; charset=<?= Yii::$app->charset ?>" />

	<title><?= Html::encode($this->title) ?></title>

	<?php $this->head() ?>

</head>

<body>

	<?php $this->beginBody() ?>

	<?= $content ?>

	<?php $this->endBody() ?>

</body>

</html>

<?php $this->endPage() ?>



Thank you for your quick guidance. I appreciate this.

Is it possible to pass $model->body without using the layout? I really not sure how to properly construct the compose(), however I feel we are very close. What do I need to change so that the view file will take the $model->body?

Thanks

I now have FE>models>ContactForms.php:


  public function sendEmail($email)

    {

        return Yii::$app->mailer->compose('serviceInfo-html', [

        'body' => $model->body,

        ])

            ->setTo($email)

            ->setFrom([$this->email => $this->name])

            ->setSubject($this->subject)            

            ->send();

    }

C>mail>serviceInfo-html.php:


<?php

use yii\helpers\Html;

use yii\helpers\Url;

/* @var $this \yii\web\View view component instance */

/* @var $message \yii\mail\BaseMessage instance of newly created mail message */

?>


<h1>This is a Start.</h1>


<?= Html::encode($model->body) ?>

I am now simply trying to pass $model to the serviceInfo-html.php. I’m expecting to obtain $this->body in the view file as well. I’ve tried the following:

  1. referred the Mailing Section of the Definitive Guide.

  2. visted several threads on mailing, however I feel the following is the best explanation of what I need: http://www.yiiframework.com/forum/index.php/topic/64301-how-to-compose-message-content-via-view-file/page__p__282365__hl__%2Bcompose+%2Bmail__fromsearch__1#entry282365

  3. recreated "Working with Forms" under the Getting Started section of the Definitive Guide

  4. Reread Guidelines For Posting In This Forum - I’m not clear on what I need to post or the direction I should take to gain understanding of what should be a simple building block.

I now have FE/models/ContactForm.php


 public function sendEmail($email)

    {

        return Yii::$app->mailer->compose('serviceInfo-html',

          ['model' => $model ]

       )

            ->setTo($email)

            ->setFrom([$this->email => $this->name])

            ->setSubject($this->subject)            

            ->send();

    }

common/mail/serviceInfo-html.php


$this->title = 'Contact Details';

?>


<h1><?= Html::encode($this->title) ?></h1>


<h1><?= Html::encode($model->body) ?></h1>


 <?php echo '<pre>' . print_r ($model, 1) . '</pre>'; ?>

Any advice on how to pass $model, $this->body, or simply how to properly ask for advice regarding this matter will be greatly appreciated. Thanks

Where did “$model” come from? Shouldn’t it be “$this” instead? Otherwise your code seems to be OK.

something like?


 public function sendEmail($email)

    {

        return Yii::$app->mailer->compose('serviceInfo-html',

          ['body' => $this->body ]

       )

*** change from $this-body to $this->body with the ">"

Yes, now the body of message should be available as "$body" in the template. Does it work?


$this->title = 'Contact Details';


$this->body = 'this';

?>


<h1><?= Html::encode($this->title) ?></h1>




<h1><?= Html::encode($this->body) ?></h1>

<h2><?= Html::encode($body) ?></h2>




 <?php echo '<pre>' . print_r ($body, 1) . '</pre>'; ?>



Yields Setting unknown property: yii\web\View::body

/************************************************************

/************************************************************

/************************************************************


$this->title = 'Contact Details';




?>


<h1><?= Html::encode($this->title) ?></h1>




<h2><?= Html::encode($body) ?></h2>




 <?php echo '<pre>' . print_r ($body, 1) . '</pre>'; ?>



Yields blank email body other than the header.

When I set useFileTransport =>true, shouldn’t I be able to access an actual file. I’ve read the guide on this; however, I don’t understand the location of the appropriate folder.

I don’t understand the difference between \Yii:: & Yii::.


 return \Yii::$app->mailer->compose(['html' => 'passwordResetToken-html', 'text' => 'passwordResetToken-text'], ['user' => $user])

I’ve seen post on here as well as stackoverflow with both methods.