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

How to send emails in Yii framework?

Guanhui
Release: 2023-03-03 09:22:01
Original
2230 people have browsed it

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
Copy after login

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'=>'白狼栈'] 
            ], 
        ], 
    ], 
];
Copy after login

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

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

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());
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template