H
H
hanyll2016-08-19 14:43:41
Yii
hanyll, 2016-08-19 14:43:41

Yii2 Swiftmailer - how to set default sender?

I use Yii2 in the project, in which I set up sending emails through Swiftmailer, which is native to it. You can use setFrom() to specify the sender. But every time when sending letters, doing the same thing, specifying the same sender is inconvenient. Is it possible once, through the config, to specify the sender, which will be substituted by default for each sending of letters?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2016-08-19
@slo_nik

Good afternoon.
Set a parameter in params, where specify the admin's email

// в params.php
return [
    'adminEmail' => '[email protected]',
]
// в контроллере
if($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])){

            Yii::$app->session->setFlash('contactFormSubmitted');

            return $this->refresh();

}

A
at0m1x, 2016-08-23
@at0m1x

The sender can also be specified in the application config \config\web.php

...
'components' => [
    ...
    'mailer' => [
        ...
        'messageConfig' => [
            ...
            'from' => ['[email protected]' => 'Site Name'],
            ...
        ],
    ...
    ],
    ...
]
...

При этом, не нужно будет каждый раз при отправке письма указывать отправителя, он подставиться автоматически, т.е. будет взят из этого конфига.
Подробнее о нюансах работы со Swiftmailer можно почитать тут.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question