簡單的發送郵件類:
<?php
/**
* Created by PhpStorm.
* User: gyc
* Date: 2020/4/4
* Time: 下午2:42
* 郵件發送
*/
namespace app\plugins;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
class Mail
{
public static function send(string $sendto, string $title, string $content)
{
$config = config('mail');
$mail = new PHPMailer(true);
$mail->SMTPDebug = SMTP::DEBUG_SERVER;
$mail->isSMTP();
$mail->CharSet = "utf8";// 編碼格式為utf8
$mail->Host = $config['host']; // SMTP 服務器地址
$mail->SMTPAuth = $config['smtpAuth']; // 開啟SMTP驗證
$mail->Username = $config['userName']; // SMTP 用戶名
$mail->Password = $config['password']; // SMTP 密碼
$mail->SMTPSecure = 'ssl'; // //使用ssl協議方式
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->Port = $config['port']; // qq郵箱的ssl協議方式端口號是465/587
//信息設置
$mail->setFrom('727306285@qq.com', '自定義'); //發件人信息設置 這里會顯示為Mailer(xxxx@qq.com),Mailer是當做名字顯示
$mail->addAddress($sendto); // 設置收件人信息
//$mail->addReplyTo('', '');// 設置回復人信息,指的是收件人收到郵件后,如果要回復,回復郵件將發送到的郵箱地址
// 郵件內容
$mail->isHTML(true);
$mail->Subject = $title; //郵件標題
$mail->Body = $content;// 郵件正文
if (!$mail->send()){
echo $mail->ErrorInfo;
}else{
echo '郵件發送成功!';
}
}
}
解決方法:
原來的:
$this->smtp_conn =fsockopen(
$host,
$port,
$errno,
$errstr,
$timeout
);
修改為:
$this->smtp_conn = @stream_socket_client(
$host,
$port,
$errno,
$errstr,
$timeout
);
### 另:郵件密碼是在郵箱設置里面的授權碼,而不是登錄密碼
