无法通过SendGrid SMTP服务器将电子邮件发送到我自己的域

时间:2018-08-09 13:55:08

标签: php html forms phpmailer sendgrid

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

我已将PHPMailer安装到Web服务器中并以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

1 个答案:

答案 0 :(得分:0)

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