简体   繁体   English

尝试使用SwiftMailer和Sendgrid发送电子邮件时出现致命错误

[英]Fatal error when trying to send email using SwiftMailer and Sendgrid

I'm getting this error when trying to send an email using swiftmailer and the sendgrid smtp 尝试使用swiftmailer和sendgrid smtp发送电子邮件时出现此错误

Fatal error: *Uncaught exception 'Swift_TransportException' with message 'Expected response code 250 but got code "", with message ""'* 致命错误: *未捕获异常'Swift_TransportException',消息'预期响应代码250但得到代码“”,消息“”'*

Here's my code : 这是我的代码:

$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);
}

An idea ? 一个主意 ?

Thanks 谢谢

There could be a number of things causing this. 可能会有很多事情导致这种情况发生。 The most likely one is that your server has connecting to external hosts turned off. 最可能的一个是您的服务器已连接到外部主机关闭。 Other possibilities are that you're using an old version of PHP that has an openSSL error or that you're being rate limited by something. 其他可能性是您正在使用具有openSSL错误的旧版本的PHP,或者您正受到速率限制。

You should take a look at this question for details on the external host issue: send mails via sendgrid 您应该查看此问题以获取有关外部主机问题的详细信息: 通过sendgrid发送邮件


On a separate note, you should use the SendGrid PHP library if you want to send emails using SendGrid. 另外,如果要使用SendGrid发送电子邮件,则应使用SendGrid PHP库。 It addresses a whole bunch of subtle nuances with Swift and sending in general. 它通过Swift和一般发送解决了一大堆微妙的细微差别。 Also, it gives you access to the HTTP API in case you can't use SMTP for whatever reason. 此外,它还允许您访问HTTP API,以防您出于任何原因无法使用SMTP。

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

Full Disclosure : I work as a developer evangelist for SendGrid and work on the PHP library from time to time. 完全披露 :我是SendGrid的开发人员传播者,并不时在PHP库上工作。

for sendgrid might be two resons: 对于sendgrid可能是两个resons:

  1. you may have the authentification data wrong 您可能有错误的身份验证数据

  2. your account might still be provisioned, so you must wait a bit for it to be fully activated 您的帐户可能仍处于配置状态,因此您必须等待一段时间才能完全激活它

so 所以

Your account is still being provisioned, you may not be able to send emails. 您的帐户仍在配置中,您可能无法发送电子邮件。

You will also receive this error if your account is frozen for billing reasons (eg lapsed credit card expiry date). 如果您的帐户因结算原因而被冻结(例如信用卡失效日期),您也会收到此错误。

Note: SendGrid doesn't drop the message(s), they hold them until your billing issue is resolved then process them even when this error is returned. 注意: SendGrid不会丢弃消息,它们会保留消息,直到您的结算问题得到解决,然后即使返回此错误也会处理它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试使用 Swiftmailer 发送电子邮件 - Trying to send an email using Swiftmailer 尝试使用SwiftMailer通过Gmail发送电子邮件时出错 - Error trying to send email through Gmail using SwiftMailer Symfony2:尝试使用Swiftmailer发送电子邮件时出错 - Symfony2: error trying to send an email with Swiftmailer 使用swiftmailer发送批量电子邮件 - using swiftmailer to send bulk email 无法使用SwiftMailer发送电子邮件 - Cannot send email using SwiftMailer 错误:当我尝试使用 PHP + Swiftmailer 发送电子邮件时出现“501 5.5.4 Invalid domain name” - Error : “501 5.5.4 Invalid domain name” when I attempt to send an email using PHP + Swiftmailer 获取错误使用swiftmailer在yii2中发送电子邮件时,代码1的SSL操作失败 - Get error SSL operation failed with code 1 when using swiftmailer to send an email in yii2 尝试使用带有 office 365 oauth2 accesstoken 的 swiftmailer 发送邮件时,在 smtp 上无法验证错误 - Getting Failed to authenticate error on smtp when trying to send mail using swiftmailer with office 365 oauth2 accesstoken SendGrid API 405 尝试发送 email 时方法不允许 - SendGrid API 405 Method Not Allowed when trying to send an email 尝试在Azure的VM ubuntu中使用SendGrid发送电子邮件时出错 - Error trying to send email with SendGrid in a VM ubuntu in Azure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM