mailer" to obtain the mailer object; finally set the email information. Can."/> mailer" to obtain the mailer object; finally set the email information. Can.">

Home  >  Article  >  Backend Development  >  How to send emails in Yii framework?

How to send emails in Yii framework?

Guanhui
GuanhuiOriginal
2020-07-24 14:16:292176browse

How to send emails in Yii framework?

How to send emails using Yii framework?

First use Composer to install the "yiisoft/yii2-swiftmailer" extension;

php composer require --prefer-dist yiisoft/yii2-swiftmailer

Then configure the parameters in the mailer;

return [ 
    //.... 
    'components' => [ 
        'mailer' => [ 
            'class' => 'yii\swiftmailer\Mailer', 
            'viewPath' => '@common/mail', 
            // send all mails to a file by default. You have to set 
            // 'useFileTransport' to false and configure a transport 
            // for the mailer to send real emails. 
            'useFileTransport' => false, 
            'transport' => [ 
                'class' => 'Swift_SmtpTransport', 
                'host' => 'smtp.163.com', 
                'username' => '***@163.com', 
                'password' => '******', 
                'port' => '25', 
                'encryption' => 'tls', 
            ], 
            'messageConfig'=>[ 
                'charset'=>'UTF-8', 
                'from'=>['***@163.com'=>'白狼栈'] 
            ], 
        ], 
    ], 
];

Then call the code "Yii:: $app->mailer" to obtain the mailer object;

$mail= Yii::$app->mailer->compose();

Finally set the email information.

$mail= Yii::$app->mailer->compose(); 
$mail->setTo('***@qq.com'); //要发送给那个人的邮箱 
$mail->setSubject("邮件主题"); //邮件主题 
$mail->setTextBody('测试text'); //发布纯文字文本 
$mail->setHtmlBody("测试html text"); //发送的消息内容 
var_dump($mail->send());

Recommended tutorial: "PHP"

The above is the detailed content of How to send emails in Yii framework?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn