mailer" to obtain the mailer object; finally set the email information. Can."/> mailer" to obtain the mailer object; finally set the email information. Can.">
search
HomeBackend DevelopmentPHP ProblemHow to send emails in Yii framework?

How to send emails in Yii framework?

Jul 24, 2020 pm 02:16 PM
phpyii

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version