一、申請(qǐng)163郵箱,開(kāi)通SMTP服務(wù),獲取授權(quán)碼。
圖片1.png
二、在php.ini開(kāi)啟php_openssl.dll和php_sockets.dll。
image.png
image.png
三、配置yii-mail。把yii-mail包放在extensions文件夾下,在配置文件main.php中配置如下代碼:
'import'=>array(
'application.extensions.yii-mail.*',
),
'components'=>array(
'mail'=>array(
'class' => 'application.extensions.yii-mail.YiiMail',
'viewPath' => 'application.views.mail',
'logging' => true,
'dryRun' => false,
'transportType'=>'smtp', // case sensitive!
'transportOptions'=>array(
'host'=>'smtp.163.com', // smtp服務(wù)器
'username'=>'chenlingeek@163.com', // 驗(yàn)證用戶
'password'=>'Aa156588103', // 驗(yàn)證密碼
'port'=>'25', // 端口號(hào)
//'encryption'=>'ssl',
),
),
),
四、實(shí)現(xiàn)發(fā)送郵件代碼。
$message = new YiiMailMessage();
$message->setFrom('送信人');
$message->setTo('收信人');
$message->setSubject('標(biāo)題');
$message->setBody('正文','text/html');
$swiftAttachment = Swift_Attachment::fromPath($file_path); //附件
$message->attach($swiftAttachment);
$sendmail = Yii::app()->mail->send($message) ;