一、申請163郵箱,開通SMTP服務,獲取授權碼。
圖片1.png
二、在php.ini開啟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服務器
'username'=>'chenlingeek@163.com', // 驗證用戶
'password'=>'Aa156588103', // 驗證密碼
'port'=>'25', // 端口號
//'encryption'=>'ssl',
),
),
),
四、實現發送郵件代碼。
$message = new YiiMailMessage();
$message->setFrom('送信人');
$message->setTo('收信人');
$message->setSubject('標題');
$message->setBody('正文','text/html');
$swiftAttachment = Swift_Attachment::fromPath($file_path); //附件
$message->attach($swiftAttachment);
$sendmail = Yii::app()->mail->send($message) ;