尝试使用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);
}
一个想法?
由于
答案 0 :(得分:3)
可能会有很多事情导致这种情况发生。最可能的一个是您的服务器已连接到外部主机关闭。其他可能性是你使用的旧版本的PHP有一个openSSL错误,或者你受到某些事情的限制。
您应该查看此问题,详细了解外部主机问题:send mails via sendgrid
另外,如果要使用SendGrid发送电子邮件,则应使用SendGrid PHP库。它通过Swift和一般发送解决了一大堆微妙的细微差别。此外,它还允许您访问HTTP API,以防您因任何原因无法使用SMTP。
https://github.com/sendgrid/sendgrid-php
完全披露 :我作为SendGrid的开发人员传播者工作,并不时处理PHP库。
答案 1 :(得分:3)
可能是两个resons:
您的身份验证数据可能错误
您的帐户可能仍在配置中,因此您必须等待一段时间才能完全激活
所以
检查app / config / mail.php中的所有身份验证数据,以匹配https://sendgrid.com/docs/Integrate/index.html
请检查您的sendgrid帐户上是否有热门消息/通知,例如:
您的帐户仍在配置中,您可能无法发送 电子邮件。
答案 2 :(得分:0)
如果您的帐户因结算原因(例如已失效的信用卡到期日期)被冻结,您也会收到此错误。
注意: SendGrid不会删除邮件,他们会保留邮件,直到您的结算问题得到解决,然后即使返回此错误也会处理它们。