2018-04-12 10:22:37 2113次浏览 2条回答 0 悬赏 10 金钱

邮件类 在配置文件设置过了,

控制器中的代码是

public function actionRequestPasswordReset()
{
    $model = new PasswordResetRequestForm();
    if ($model->load(Yii::$app->request->post()) && $model->validate()) {
        if ($model->sendEmail()) {
            Yii::$app->session->setFlash('success', '请检查您的邮箱');

            return $this->goHome();
        } else {
            Yii::$app->session->setFlash('error', 'Sorry, we are unable to reset password for the provided email address.');
        }
    }

    return $this->render('requestPasswordResetToken', [
        'model' => $model,
    ]);
}

运行报错

in D:\newWWW\sinocd.cn\vendor\swiftmailer\swiftmailer\lib\classes\Swift\Mime\SimpleMessage.php at line 496

这个文件是原版文件,没有更改过,请问 哪里出问题呢 是不是配置文件写的不对?该怎么写?
看看下面这个配置文件

'mailer' => [
    'class' => 'yii\swiftmailer\Mailer',  
    'useFileTransport' =>false,
    'transport' => [
        'class' => 'Swift_SmtpTransport',  
        'host' => 'smtp.163.com',
        'username' => '888888@163.com',
        'password' => '888888',
        'port' => '465',
        'encryption' => 'ssl'
    ],
    'messageConfig'=>[
        'charset'=>'UTF-8',
        'from'=>['88888888@163.com'=>'8888']
    ] 
]

敏感信息用8代替了。另外谁能详解下这个组件(swiftmailer\swiftmailer)的配置,这个框架(YII)好多配置

补充于 2018-04-12 14:09

我自己找到问题错误了,两个问号是php7才出的新特性,我的本地php版本才5.4,升级下PHP版本试试看
其实两个问题??是php7新推出的表达式,

c = a ?? b;

表示如果a非空,则c = a,

如果a为空,则 c = b;

补充于 2018-04-12 17:23

经过验证,就是php版本的问题,升级到PHP7.0.以后版本,发送邮件没有任何问题。

D:\newWWW\sinocd.cn\vendor\swiftmailer\swiftmailer\lib\classes\Swift\Mime\SimpleMessage.php at line 496

其实两个问题??是php7新推出的表达式,

c = a ?? b;

表示如果a非空,则c = a,

如果a为空,则 c = b;

补充于 2018-04-12 21:08

并且发送邮件的方法可以参考frontend\models\PasswordResetRequestForm模型中的sendEmail方法,
定义viewpath可以容易的给邮件定义模板,容易群发和按需要发送
邮件正确的配置是这样的:

'mailer' => [
    'class' => 'yii\swiftmailer\Mailer',
    'viewPath' => '@common/mail',
    'useFileTransport' =>false,
    'transport' => [
        'class' => 'Swift_SmtpTransport',  
        'host' => 'smtp.163.com',
        'username' => '888888@163.com',
        'password' => '88888',
        'port' => '465',
        'encryption' => 'ssl'
    ],
    'messageConfig'=>[
        'charset'=>'UTF-8',
        'from'=>['88888@163.com'=>'8888']
    ]          
],
  • 回答于 2018-04-12 10:59 举报
    /**
     * 发送邮件
     *
     * @param     $email   邮箱地址
     * @param     $subject 邮件主题
     * @param     $content 邮件内容
     * @param int $isHtml  邮件内容是否是html, 1-html, 0-text
     *
     * @return bool
     * @throws \Exception
     */
    public static function sendMail ( $email, $subject, $content, $isHtml = 1 ) {
        $mail = Yii::$app->mailer->compose ();
        $mail->setTo ( $email );
        $mail->setSubject ( $subject );
        if ( $isHtml == 1 ) {
            $mail->setHtmlBody ( $content );
        } else {
            $mail->setTextBody ( $content );
        }
        try {
            if ( $mail->send () ) {
                return true;
            } else {
                return false;
            }
        }catch (\Exception $e) {
            throw new \Exception($e->getMessage ());
        }
    }
    
  • 回答于 2018-04-12 10:54 举报
    'mailer' => [
    		'class'            => 'yii\swiftmailer\Mailer',
    		'useFileTransport' => false,
    		'transport'        => [
    			'class'      => 'Swift_SmtpTransport',
    			'host'       => 'smtp.exmail.qq.com',
    			'username'   => '@',
    			'password'   => '',
    			'port'       => '465',
    			'encryption' => 'SSL',
    		],
    		'messageConfig'    => [
    			'charset' => 'UTF-8',
    			'from'    => [ '@' => '发送人名称' ]
    		],
    	],
    
    1 条回复
    回复于 2018-04-12 10:56 回复

    我报的那个错误 你估计什么问题?配置你和我写的基本一样

您需要登录后才可以回答。登录 | 立即注册
sinobill
见习主管

sinobill

注册时间:2017-10-26
最后登录:2020-04-03
在线时长:15小时35分
  • 粉丝2
  • 金钱115
  • 威望0
  • 积分265

热门问题