php - 尝试使用 SwiftMailer 和 Sendgrid 发送电子邮件时出现 fatal error

标签 php email swiftmailer sendgrid

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

fatal error : *未捕获异常“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);
}

一个想法?

谢谢

最佳答案

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

有关外部主机问题的详细信息,您应该查看此问题:send mails via sendgrid


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

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

全面披露:我是 SendGrid 的开发人员布道者,不时处理 PHP 库。

关于php - 尝试使用 SwiftMailer 和 Sendgrid 发送电子邮件时出现 fatal error ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11158845/

相关文章:

php - 来自另一个键的 YAML 设置值

php表单问题如果图像输入为空则不会插入数据

javascript - 如何通过单击一个复选框将多个复选框值附加到文本区域

php - CodeIgniter 将多维数组作为单独的行插入到数据库表中

python - 使用 Scrapy 通过电子邮件发送项目和日志

java - 从 Android 应用程序阅读电子邮件

symfony - 假脱机邮件并直接发送邮件

javascript - 删除所选选项中的重复元素

email - 在 elisp 中发送电子邮件的最简单、最便携的方式是什么?

php - 如何将 TCPDF 生成的 pdf 作为 Swiftmailer 附件发送