2012-06-22 59 views
3

我得到尝试使用swiftmailer和sendgrid SMTP致命错误尝试发送使用SwiftMailer和Sendgrid

致命错误发送电子邮件时,这个错误电子邮件时: *未捕获的异常“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 
// [email protected], [email protected] 
$from = array('[email protected]' => 'Mupiz'); 
$to = array('[email protected]'=>'AN',"[email protected]"=>"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); 
} 

的想法?

谢谢

回答

3

有可能是一些事情造成这种情况。最有可能的是您的服务器已连接到关闭的外部主机。其他的可能性是,你使用的是旧版本的PHP,它有openSSL错误,或者你被某些东西限制了速度。

你应该在这个问题上的外部主机问题的详细信息来看一看:send mails via sendgrid


在一个单独的说明,如果你想使用SendGrid发送电子邮件,你应该使用SendGrid PHP库。它通过Swift和一般发送来解决一系列微妙的细微差别。此外,它允许您访问HTTP API,以防因任何原因无法使用SMTP。

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

充分披露:我作为一个开发者传播者SendGrid和工作的PHP库不时工作。

+0

什么是细微的细微差别? – Shackrock

3

为sendgrid可能有两个resons:

  1. 你可能有认证数据错误

  2. 您的帐户可能仍然被提供,所以你必须稍等一下,它完全激活

所以

Your account is still being provisioned, you may not be able to send emails.

+0

哈哈thnx,今天改了我的邮件密码>。<我无法找出问题^ .- – Szenis

0

如果您的帐户由于结算原因被冻结(例如,您也会收到此错误信用卡失效日期)。

注意: SendGrid不会删除邮件,它们会保留它们直到您的结算问题得到解决,然后在返回此错误时处理它们。

相关问题