解决yii2发送邮件无法接收的问题

yii2发送邮件无法接收的解决办法:首先找到并打开配置文件代码;然后将代码“'useFileTransport' => true”改成“'useFileTransport' => false,”;最后保存修改即可。

成都创新互联公司服务项目包括泉港网站建设、泉港网站制作、泉港网页制作以及泉港网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,泉港网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到泉港省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!

解决Yii2邮件发送问题(结果返回成功,但接收不到邮件)

刚刚用了一下yii邮件发送功能,虽然结果返回成功,但接收不到邮件。配置文件代码如下:

推荐:《yii教程》

'components' => [
    'db' => [
        'class' => 'yii\\db\\Connection',
        'dsn' => 'mysql:host=localhost;dbname=yiidemo',
        'username' => 'root',
        'password' => 'root',
        'charset' => 'utf8',
    ],
    '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' => true,
        'transport' => [
            'class' => 'Swift_SmtpTransport',
            'host' => 'smtp.qq.com',
            'username' => '********@qq.com',
            'password' => '********',
            'port' => '465',
            'encryption' => 'ssl',
        ],
    ],
],

控制器代码:

public $modelClass = 'common\\models\\User';
 
public function actions()
{
    $actions = parent::actions();
 
    // 禁用"create" 操作后可用自己在控制器中写的方法
    unset($actions['update'],$actions['create'],$actions['delete']);
   //$actions['index']['prepareDataProvider'] = [$this, 'prepareDataProvider'];
    return $actions;
}
 
 
 
public function actionCreate(){
 
    $request = Yii::$app->request;
    $params = $request->post();
    if($params){
           $fromName = $request->getBodyParam('fromName');
        $fromBady = $request->getBodyParam('fromBady');
        $toName = $request->getBodyParam('toName');
        $body = $request->getBodyParam('body');
        return $this->send($fromName,$fromBady,$toName,$body);
       }
   return false;
 
}
 
/*
 * Email Send function
 * @param1 $fromName
 * @param1 $toName
 * @param1 $body
 * $return boo1ean
 *
 */
 
public function send($fromName,$fromBady,$toName,$body = ''){
   $mail = \\Yii::$app->mailer->compose()
        ->setFrom([$fromName=>$fromBady])
        ->setTo($toName)
        ->setSubject('邮件发送配置')
        ->setTextBody($body)   //发布纯文字文本
        ->send();
    if($mail){
        return [
            'name' => [
                'fromName' => $fromName,
                'fromBady' => $fromBady,
                'toName' => $toName,
                'body' => $body,
            ],
            'message' => '发生到['.$toName.']的邮件成功!',
            'code' => 0,
            'status' => 200,
        ];
    }else{
        return [
            'name' => 'Error',
            'message' => '发生到'.$toName.'的邮件失败!',
            'code' => 0,
            'status' => 402,
        ];
    }
}

但是你会发现数据返回是成功的但是你却没收到邮件

这是你应该把

'useFileTransport' => true 改成 'useFileTransport' => false,

并且你的邮箱密码是qq授权码(到你邮箱中的设置->账户 查看)

通过以上努力你会成功接收邮件!
网页标题:解决yii2发送邮件无法接收的问题
当前链接:http://scyanting.com/article/cjchhs.html