Yii2 Email How to set sender name

泪湿孤枕 提交于 2019-12-22 09:47:49

问题


i using Mailer to send email, so i have problem about sender name This is my config

    'mailer' => [
        'class' => 'yii\swiftmailer\Mailer',
        'useFileTransport' => false,
        'messageConfig' => [
            'charset' => 'UTF-8',
            'from' => ['admin@app.com' => 'App Sender Name'],
        ],
        'transport' => [
            'class' => 'Swift_MailTransport',
        ],
    ],

So it's not work. I goto inbox and only email showed.

And i need show as Example:


回答1:


it's work when i update setFrom() method. Ex: $mailer->setFrom(['email@app.com' => 'App Name']). And here is config Yii2 for send by PHP mail and with sender name

'mailer' => [
        'class' => 'yii\swiftmailer\Mailer',
        'useFileTransport' => false,
        'messageConfig' => [
            'charset' => 'UTF-8',
            'from' => ['admin@app.com' => 'App Sender Name'],
        ],
        'transport' => [
            'class' => 'Swift_MailTransport',
        ],
    ],

and send email

   Yii::$app->mailer->compose()
    ->setTo('client@email.com')
    ->setFrom(['admin@app.com' => 'App Name'])
    ....



回答2:


Every component and sub-component in yii2 is configurable by dependency injection, this case is same, eg:

        'transport' => [
            'class' => 'Swift_SmtpTransport',
            'host' => 'smtp.gmail.com',
            'username' => 'noreply.yourcompany@gmail.com',
            'password' => '123456',
            'port' => '465',
            'encryption' => 'ssl',
        ],


来源:https://stackoverflow.com/questions/39218286/yii2-email-how-to-set-sender-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!