Home >PHP Framework >YII >How to use yii2 to send files using qq mailbox

How to use yii2 to send files using qq mailbox

王林
王林forward
2021-01-25 10:18:543537browse

How to use yii2 to send files using qq mailbox

The specific method is as follows:

(Learning video sharing: Introduction to Programming)

1. First, log in to the qq mailbox Enable POP3/SMTP service in the settings

How to use yii2 to send files using qq mailbox

Save the authorization code

Add it in the Yii2 configuration file (common/config/main.php components) Mailbox component

'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' => [
                //这里如果你是qq的邮箱,可以参考qq客户端设置后再进行配置 http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256
                'class' => 'Swift_SmtpTransport',
                'host' => 'smtp.qq.com',
                // qq邮箱
                'username' => '114***6@qq.com',
                //授权码, 什么是授权码, http://service.mail.qq.com/cgi-bin/help?subtype=1&&id=28&&no=1001256
                'password' => '***', 
                'port' => '465',
                'encryption' => 'ssl',
             ],
            'messageConfig'=>[
                'charset'=>'UTF-8',
                'from'=>['114***@qq.com'=>'developer']
            ],
        ],

Call the mailbox interface to send emails

public function actionSendmail()
    {
        $mail = \YII::$app->mailer->compose();
        $mail->setTo("***@qq.com");
        $mail->setSubject("邮件测试");
        $mail->setTextBody("textbody 25 ok?");//发布纯文字文本
        //$mail->setHtmlBody("htmlbody");//发布可以带html标签的文本
        if($mail->send()){
            echo "success";
        }else{
            echo "failure";
        }
    }

Related recommendations: yii framework

The above is the detailed content of How to use yii2 to send files using qq mailbox. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete