发布日期:2018-03-26
通过PHPMailer使用Gmail SMTP服务器发送邮件+ 查看更多
通过PHPMailer使用Gmail SMTP服务器发送邮件
+ 查看更多
发布日期:2018-03-10 14:57
分类:PHP
浏览次数:431
如下:
我想通过PHPMailer 使用Gmail SMTP(Gmail邮件)服务器发送邮件
这是我的代码:
我想通过PHPMailer 使用Gmail SMTP(Gmail邮件)服务器发送邮件
这是我的代码:
IsSMTP(); $mail->CharSet="UTF-8"; $mail->SMTPSecure = 'tls'; $mail->Host = 'smtp.gmail.com'; $mail->Port = 587; $mail->Username = 'MyUsername@gmail.com'; $mail->Password = 'valid password'; $mail->SMTPAuth = true; $mail->From = 'MyUsername@gmail.com'; $mail->FromName = 'Mohammad Masoudian'; $mail->AddAddress('anotherValidGmail@gmail.com'); $mail->AddReplyTo('phoenixd110@gmail.com', 'Information'); $mail->IsHTML(true); $mail->Subject = "PHPMailer Test Subject via Sendmail, basic"; $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; $mail->Body = "Hello"; if(!$mail->Send()){ echo "Mailer Error: " . $mail->ErrorInfo;}else{ echo "Message sent!";}?>
但是有一些错误:
Mailer Error: SMTP Error: The following recipients failed: anotherValidGmail@gmail.com SMTP server error: SMTP AUTH is required for message submission on port 587
我的域名是vatandesign.ir
回答:
$mail = new PHPMailer(); // create a new object $mail->IsSMTP(); // enable SMTP $mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only $mail->SMTPAuth = true; // authentication enabled $mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail $mail->Host = "smtp.gmail.com"; $mail->Port = 465; // or 587 $mail->IsHTML(true); $mail->Username = "email@gmail.com"; $mail->Password = "password"; $mail->SetFrom("example@gmail.com"); $mail->Subject = "Test"; $mail->Body = "hello"; $mail->AddAddress("email@gmail.com"); if(!$mail->Send()) { echo "Mailer Error: " . $mail->ErrorInfo; } else { echo "Message has been sent"; }
上面的代码经过测试是可行的。
你可能需要使用$mail->SMTPSecure = 'ssl';
确认你的账户不需要二次验证,因为这可能导致问题。(所谓二次验证,就是输入了密码之后还需要用短信之类的方式进行第二次验证。)
更新:
可以将$mail->SMTP改变为:
你可能需要使用$mail->SMTPSecure = 'ssl';
确认你的账户不需要二次验证,因为这可能导致问题。(所谓二次验证,就是输入了密码之后还需要用短信之类的方式进行第二次验证。)
更新:
可以将$mail->SMTP改变为:
$mail->SMTPSecure = 'tls';有些SMTP服务块连接需要注意。一些SMTP服务不支持SSL(或者TLS)连接。