Yii2电子邮件如何设置发件人姓名

时间:2016-08-30 03:22:29

标签: yii2

我使用Mailer发送电子邮件,所以我对发件人姓名有疑问 这是我的配置

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

所以它不起作用。我转到收件箱,只有电子邮件显示。 enter image description here

我需要显示为 例: enter image description here

2 个答案:

答案 0 :(得分:5)

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'])
    ....

答案 1 :(得分:0)

yii2中的每个组件和子组件都可以通过依赖注入进行配置,这种情况是相同的,例如:

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