【发布时间】:2017-10-21 14:43:21
【问题描述】:
我是 php 新手,我正在尝试使用 phpmailer 使用 gmail 发送电子邮件
这是我写的代码
$mail = new PHPMailer(true); try { $mail->SMTPDebug = 2; $mail->isSMTP(); $mail->Host = 'smtp.gmail.com'; $mail->SMTPAuth = true; $mail->Username = 'sendermail@gmail.com'; $mail->Password = '<some password>'; $mail->SMTPSecure = 'tls'; $mail->Port = 587; $mail->setFrom('anadresse@gmail.com', 'GestionStock'); $mail->addAddress('anadresse@gmail.com', 'hamza'); $mail->isHTML(true); $mail->Subject = 'Here is the subject'; $mail->Body = 'This is the HTML message body <b>in bold!</b>'; $mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; $mail->send(); echo 'Message has been sent'; } catch (Exception $e) { echo 'Message could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; } ?>
我得到的结果是 SMTP 错误:无法连接到 SMTP 主机。
SMTP 错误:无法连接到 SMTP 主机。 无法发送邮件。Mailer 错误:SMTP 错误:无法连接到 SMTP 主机。
谁能告诉我我做错了什么!!
【问题讨论】:
-
您可以手动连接到端口
587上的smtp.google.com吗?你身边的防火墙能阻止这种情况吗? -
好吧,那么提供的 SMTP 配置显然有问题。顺便说一句,我希望这不是您的真实密码。否则你应该立即改变它!尝试改成
$mail->SMTPSecure = 'ssl';和$mail->Port = 465; -
@GillesGouaillardet,我该如何手动完成??
-
@ Connum ,我尝试了你所说的,我得到了 2017-10-21 14:49:06 SMTP 错误:无法连接到服务器:(0) SMTP connect() 失败。 github.com/PHPMailer/PHPMailer/wiki/Troubleshooting 无法发送邮件。邮件程序错误:SMTP 连接()失败。 github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
-
我认为您必须在 gmail 帐户中激活使用 SMTP 的选项,否则将不起作用。例如Gmail Webmail - Remote SMTP Server - Setup Guide(使用谷歌快速搜索)