3

尝试使用 swiftmailer 和 sendgrid smtp 发送电子邮件时出现此错误

致命错误: *未捕获异常“Swift_TransportException”,消息“预期响应代码 250,但得到代码“”,消息“”'*

这是我的代码:

$hdr = new SmtpApiHeader();


// Set all of the above variables
$hdr->addTo($toList);
$hdr->addSubVal('-name-', $nameList);
$hdr->addSubVal('-time-', $timeList);

// Specify that this is an initial contact message
$hdr->setCategory("initial");

// The subject of your email
$subject = 'Example SendGrid Email';

// Where is this message coming from. For example, this message can be from 
// support@yourcompany.com, info@yourcompany.com
$from = array('no-reply@mupiz.com' => 'Mupiz');
$to = array('antonin@noos.fr'=>'AN',"antonin@mupiz.com"=>"AN2s");

$text="Hello -name-
       Thank you for your interest in our products. We have set up an appointment
             to call you at -time- EST to discuss your needs in more detail.

                Regards,

                Fred, How are you?
      ";
$html = "
<html>
  <head></head>
  <body>
    <p>Hello -name-,<br>
       Thank you for your interest in our products. We have set up an appointment
             to call you at -time- EST to discuss your needs in more detail.

                Regards,

                Fred, How are you?<br>
    </p>
  </body>
</html>
";

// Your SendGrid account credentials
$username = 'XXXX';
$password = 'XXXX';

// Create new swift connection and authenticate
$transport = Swift_SmtpTransport::newInstance('smtp.sendgrid.net', 25);
$transport ->setUsername($username);
$transport ->setPassword($password);
$swift = Swift_Mailer::newInstance($transport);

// Create a message (subject)
$message = new Swift_Message($subject);

// add SMTPAPI header to the message
// *****IMPORTANT NOTE*****
// SendGrid's asJSON function escapes characters. If you are using Swift Mailer's
// PHP Mailer functions, the getTextHeader function will also escape characters.
// This can cause the filter to be dropped.
$headers = $message->getHeaders();
$headers->addTextHeader('X-SMTPAPI', $hdr->asJSON());

// attach the body of the email
$message->setFrom($from);
$message->setBody($html, 'text/html');
$message->setTo($to);
$message->addPart($text, 'text/plain');

// send message
if ($recipients = $swift->send($message, $failures))
{
// This will let us know how many users received this message
// If we specify the names in the X-SMTPAPI header, then this will always be 1.
echo 'Message sent out to '.$recipients.' users';
}
// something went wrong =(
else
{
echo "Something went wrong - ";
print_r($failures);
}

一个想法 ?

谢谢

4

3 回答 3

3

造成这种情况的原因可能有很多。最有可能的是您的服务器已关闭与外部主机的连接。其他可能性是您使用的是具有 openSSL 错误的旧版本 PHP,或者您受到某些东西的速率限制。

您应该查看这个问题以了解有关外部主机问题的详细信息:通过 sendgrid 发送邮件


另外,如果您想使用 SendGrid 发送电子邮件,您应该使用 SendGrid PHP 库。它解决了 Swift 和一般发送的一大堆细微差别。此外,它还允许您访问 HTTP API,以防您因任何原因无法使用 SMTP。

https://github.com/sendgrid/sendgrid-php

完全披露:我作为 SendGrid 的开发布道者工作,不时在 PHP 库上工作。

于 2012-06-22T18:20:11.023 回答
3

对于 sendgrid 可能有两个原因:

  1. 你可能有错误的身份验证数据

  2. 您的帐户可能仍处于预置状态,因此您必须稍等片刻才能完全激活

所以

您的帐户仍在配置中,您可能无法发送电子邮件。

于 2014-11-28T10:20:45.143 回答
0

如果您的帐户因计费原因被冻结(例如信用卡过期日期),您也会收到此错误消息。

注意: SendGrid 不会丢弃消息,它们会保留它们直到您的计费问题得到解决,然后即使返回此错误也会处理它们。

于 2016-09-08T15:45:32.863 回答