首页 > 解决方案 > 无法通过 SendGrid SMTP 服务器向我自己的域发送电子邮件

问题描述

我正在尝试使用SendGrid SMTP服务器从我网页中的联系表格发送电子邮件。

我已经安装PHPMailer到我的网络服务器中并SMTP在我的 php 表单中配置了凭据,只要接收者("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

标签: phphtmlformsphpmailersendgrid

解决方案


似乎问题不在我的代码或配置中,而是在我的 SMTP 服务器使用的 IP 中。我的 IP 被列入黑名单,这会阻止我的电子邮件。当您使用免费计划(共享 IP)时,可能会发生这种情况:https ://sendgrid.com/docs/Classroom/Deliver/Undeliverable_Email/what_do_i_do_if_im_blacklisted.html


推荐阅读