[英]Fatal error when trying to send email using SwiftMailer and Sendgrid
嘗試使用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);
}
一個主意 ?
謝謝
可能會有很多事情導致這種情況發生。 最可能的一個是您的服務器已連接到外部主機關閉。 其他可能性是您正在使用具有openSSL錯誤的舊版本的PHP,或者您正受到速率限制。
您應該查看此問題以獲取有關外部主機問題的詳細信息: 通過sendgrid發送郵件
另外,如果要使用SendGrid發送電子郵件,則應使用SendGrid PHP庫。 它通過Swift和一般發送解決了一大堆微妙的細微差別。 此外,它還允許您訪問HTTP API,以防您出於任何原因無法使用SMTP。
https://github.com/sendgrid/sendgrid-php
完全披露 :我是SendGrid的開發人員傳播者,並不時在PHP庫上工作。
對於sendgrid可能是兩個resons:
您可能有錯誤的身份驗證數據
您的帳戶可能仍處於配置狀態,因此您必須等待一段時間才能完全激活它
所以
檢查來自app / config / mail.php的所有autentification數據,以匹配來自https://sendgrid.com/docs/Integrate/index.html的數據
請檢查您的sendgrid帳戶上是否有熱門消息/通知,例如:
您的帳戶仍在配置中,您可能無法發送電子郵件。
如果您的帳戶因結算原因而被凍結(例如信用卡失效日期),您也會收到此錯誤。
注意: SendGrid不會丟棄消息,它們會保留消息,直到您的結算問題得到解決,然后即使返回此錯誤也會處理它們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.