【发布时间】:2018-08-09 13:55:07
【问题描述】:
我正在尝试使用SendGrid SMTP 服务器从我网页中的联系表单发送电子邮件。
我已将PHPMailer 安装到我的网络服务器中,并在我的php 表单中配置了SMTP 凭据,只要接收者("to") 是gmail 帐户,它就可以正常工作。 但是如果我将该帐户更改为我自己的域之一,即hello@myowndomain.net,我将永远不会收到该电子邮件。
我不知道是否应该在我的SendGrid 帐户或我的php 代码中配置任何其他内容。
这是我的 php 代码:
<?php
require_once "vendor/autoload.php";
$mail = new PHPMailer;
//Enable SMTP debugging.
$mail->SMTPDebug = 1;
//Set PHPMailer to use SMTP.
$mail->isSMTP();
//Set SMTP host name
$mail->Host = " smtp.sendgrid.net";
//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;
//Provide username and password
$mail->Username = "apikey";
$mail->Password = "mySMTPPassword";
//If SMTP requires TLS encryption then set it
//$mail->SMTPSecure = "tls";
//Set TCP port to connect to
$mail->Port = 587;
$mail->From = "test@gmail.com";
$mail->FromName = "Full Name";
$mail->smtpConnect(
array(
"ssl" => array(
"verify_peer" => false,
"verify_peer_name" => false,
"allow_self_signed" => true
)
)
);
$mail->addAddress("hello@myowndomain.net", "Recepient Name");
$mail->isHTML(true);
$mail->Subject = "Contact form";
$mail->Body = "<i>You have received a new message!</i>";
$mail->AltBody = "This is the plain text";
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent successfully";
}
编辑:
I changed SMTPDebug mode to 2, and it seems everything is OK for me:
2018-08-09 14:08:15 CLIENT -> SERVER: AUTH LOGIN
2018-08-09 14:08:15 SERVER -> CLIENT: 334
2018-08-09 14:08:15 CLIENT -> SERVER: YXBpa2V5
2018-08-09 14:08:15 SERVER -> CLIENT: 334
2018-08-09 14:08:15 CLIENT -> SERVER:
2018-08-09 14:08:16 SERVER -> CLIENT: 235 Authentication successful
2018-08-09 14:08:16 CLIENT -> SERVER: MAIL FROM:<test@gmail.com>
2018-08-09 14:08:16 SERVER -> CLIENT: 250 Sender address accepted
2018-08-09 14:08:16 CLIENT -> SERVER: RCPT TO:<hellomyowndomain.com>
2018-08-09 14:08:16 SERVER -> CLIENT: 250 Recipient address accepted
2018-08-09 14:08:16 CLIENT -> SERVER: DATA
2018-08-09 14:08:16 SERVER -> CLIENT: 354 Continue
2018-08-09 14:08:16 CLIENT -> SERVER: Date: Thu, 9 Aug 2018 07:08:16 -0700
2018-08-09 14:08:16 CLIENT -> SERVER: To: Recepient Name <hellowmyowndomain.com>
2018-08-09 14:08:16 CLIENT -> SERVER: From: Full Name <test@gmail.com>
2018-08-09 14:08:16 CLIENT -> SERVER: Subject: Contact form
2018-08-09 14:08:16 CLIENT -> SERVER: Message-ID: <@localhost>
2018-08-09 14:08:16 CLIENT -> SERVER: X-Mailer: PHPMailer 5.2.26
(https://github.com/PHPMailer/PHPMailer)
2018-08-09 14:08:16 CLIENT -> SERVER: MIME-Version: 1.0
2018-08-09 14:08:16 CLIENT -> SERVER: Content-Type: multipart/alternative;
2018-08-09 14:08:16 CLIENT -> SERVER: Content-Transfer-Encoding: 8bit
2018-08-09 14:08:16 CLIENT -> SERVER:
2018-08-09 14:08:16 CLIENT -> SERVER: This is a multi-part message in MIME
format.
2018-08-09 14:08:16 CLIENT -> SERVER:
2018-08-09 14:08:16 CLIENT -> SERVER: Content-Type: text/plain; charset=us-ascii
2018-08-09 14:08:16 CLIENT -> SERVER:
2018-08-09 14:08:16 CLIENT -> SERVER: This is the plain text
2018-08-09 14:08:16 CLIENT -> SERVER:
2018-08-09 14:08:16 CLIENT -> SERVER: Content-Type: text/html; charset=us-ascii
2018-08-09 14:08:16 CLIENT -> SERVER:
2018-08-09 14:08:16 CLIENT -> SERVER: <i>You have received a new message!</i>
2018-08-09 14:08:16 CLIENT -> SERVER:
2018-08-09 14:08:16 CLIENT -> SERVER:
2018-08-09 14:08:16 CLIENT -> SERVER:
2018-08-09 14:08:16 CLIENT -> SERVER:
2018-08-09 14:08:16 CLIENT -> SERVER: .
2018-08-09 14:08:16 SERVER -> CLIENT: 250 Ok: queued as
2018-08-09 14:08:16 CLIENT -> SERVER: QUIT
2018-08-09 14:08:16 SERVER -> CLIENT: 221 See you later
Message has been sent successfully
【问题讨论】:
-
你不能在 Sendgrids 界面查看邮件发生了什么吗?
-
为什么要禁用证书验证?这真是个坏主意。设置
SMTPDebug = 2,这样你就可以看到SendGrid说了什么,然后在他们自己的工具中检查你提交的消息的状态。 -
您可能还想阅读 this sendgrid documentation 来设置您的域身份验证。
-
我用调试信息编辑过。
-
在
2018-08-09 14:08:16 SERVER -> CLIENT: 250 Ok: queued as行上,您应该获得它提供给您的唯一字符串,并升级到SendGrid Support。他们可以使用它来查看消息发生了什么。
标签: php html forms phpmailer sendgrid