1.在qq郵箱的設置中開啓POP3/SMTP服務
保存下授權碼
2.1.在Yii2配置文件中(common/config/main.php中components)添加郵箱組件
'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***[email protected]',
//授權碼, 什麼是授權碼, 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']
],
],
3.調用郵箱接口發送郵件
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";
}
}