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.
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.
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:
referred the Mailing Section of the Definitive Guide.
recreated "Working with Forms" under the Getting Started section of the Definitive Guide
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();
}
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.