S
S
Sergey Pugovkin2016-10-07 20:25:58
Yii
Sergey Pugovkin, 2016-10-07 20:25:58

How to set default sender mail for yii\swiftmailer\Mailer?

I send mail via yandex. In yii2 config:

'mailer' => [
            'class' => 'yii\swiftmailer\Mailer',
            'transport' => [
                'class' => 'Swift_SmtpTransport',
                'host' => 'smtp.yandex.ru',
                'username' => '[email protected]',
                'password' => 'mypassword',
                'port' => '465',
                'encryption' => 'SSL',
            ],
],

Is there a way to specify the sender's email here?
So that you do not have to constantly specify it during sending:
Yii::$app->mailer->compose()
    ->setFrom('[email protected]')
    ->setTo('...')
    ->setSubject('...')
    ->setHtmlBody('...')
    ->send();

but simply:
Yii::$app->mailer->compose()
    ->setTo('...')
    ->setSubject('...')
    ->setHtmlBody('...')
    ->send();

Now, if you do not specify the sender's email, an error pops up:
Swift_TransportException
Cannot send message without a sender address

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2016-10-10
@Driver86

any message settings can be set through the messageConfig property , for example:

'mailer' => [
    'class' => 'yii\swiftmailer\Mailer',
    'transport' => [
        'class' => 'Swift_SmtpTransport',
        'host' => 'smtp.yandex.ru',
        'username' => '[email protected]',
        'password' => 'mypassword',
        'port' => '465',
        'encryption' => 'SSL',
    ],
    'messageConfig' => [
        'from' => ['[email protected]' => 'Site Name'],
    ],
],

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question