Answer the question
In order to leave comments, you need to log in
How to create a template for a letter from a record in the database?
Good afternoon.
You need to create a letter from a string that is stored in the database.
The line looks like this:
<p><strong>Поступила новая заявка с site.ru.</strong><br />
<br />
<strong>Имя:</strong> {order->name}<br />
<strong>Телефон:</strong> {order->phone}<br />
<strong>Почта:</strong> {order->email}<br />
<strong>Вес:</strong> {order->weight}<br />
<strong>Откуда:</strong> {order->from}<br />
<strong>Куда:</strong> {order->to}<br />
<strong>Дата отправки:</strong> {order->shipping_date}<br />
<strong>Сообщение:</strong> {order->message}</p>
if(Yii::$app->request->isAjax) {
$model = new Proposal();
if($model->load(Yii::$app->request->post()) && $model->validate()){
if($model->save()){
/* получаем шаблон письма из базы данных */
$template = EmailTemplate::findOne(['name' => 'new_order']);
/* в $model записаны данные, которые надо подставить в $template */
Yii::$app->mailer->compose()
->setFrom([Yii::$app->params['supportEmail'] => Yii::$app->name])
->setTo(Yii::$app->siteSettings->get('SITE.EMAIL_ADMIN'))
->setSubject('Новая заявка на сайте' . Yii::$app->name)
/* подставляю полученную строку в письмо */
->setHtmlBody($template->body)
->send();
return 'ok';
}
}
}
return false;
}
Yii::$app->mailer->compose('@app/mails')
Answer the question
In order to leave comments, you need to log in
as the easiest option to use strtr , but before replacing the data from the model must be escaped from html tags
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question