2014-03-12 36 views
0

我有整合Sendgrid使用STMP API我的Zend Framework 2应用程序,我已经使用Zend的交通运输的,但我得到一个错误Sendgrid未验证发件人不允许错误的PHP,ZF2

“捕获的异常:无法从指定的地址收到:未经身份验证的发件人不允许”

$request = $this->getRequest(); 
    //$form = new Add(); 
    // $product = new Product(); 
    $username = 'XXX'; 
    $password ='XXXX'; 
    if ($request->isXmlHttpRequest()){ // If it's ajax call 
     $email = $request->getPost('email_add'); 
     $message = $request->getPost('message'); 
     try{ 
      $message = new Message(); 
      $message->addTo('[email protected]') 
       ->addFrom('[email protected]') 
       ->setSubject('Greetings and Salutations!') 
       ->setBody("Sorry, I'm going to be late today!"); 
      $transport = new SmtpTransport(); 
      $options = new SmtpOptions(array(
       'name'    => 'sendgrid.com', 
       'host'    => 'smtp.sendgrid.net', 
       'port'    => 587, // Notice port change for TLS is 587 
       'connection_class' => 'smtp', 
       'connection_config' => array(
        'auth' => 'login', 
        'username' => 'XXXXXX', 
        'password' => 'XXXXXX', 
        'ssl'  => 'tls' 
       ), 
      )); 
      $transport->setOptions($options); 
      $transport->send($message); 
      exit; 
     }catch (\Exception $ex){ 
      echo 'Caught exception: ', $ex->getMessage(), "\n"; 
      exit; 
     } 
    } 

回答

1

Sendgrid有一个API,该API由SlmMail实现(声明:我是SlmMail的作者)。使用该API比使用旧的SMTP协议更容易使用。

我不知道如何准确配置SMTP选项,但以前我们与谷歌的SMTP服务器的工作,它需要这样的配置:

'name' => 'gmail.com', 
'host' => 'smtp.gmail.com', 
'port' => 587, 
'connection_class' => 'login', 
'connection_config' => array(
    'ssl'  => 'tls', 
    'username' => $username, 
    'password' => $password, 
), 

这是比你的略有不同(“类”是“登录“,没有”auth“选项)。请检查指定了所有SMTP选项的documentation

0

试试这个

connection_class平原应该工作

use Zend\Mail\Transport\Smtp as SmtpTransport; 
use Zend\Mail\Transport\SmtpOptions; 

      $transport = new SmtpTransport(); 
      $options = new SmtpOptions(array(
       'name'    => $name, 
       'host'    => $host, 
       'port'    => 587, 
       'connection_class' => 'plain', 
       'connection_config' => array(
        'username' => $username, 
        'password' => $password, 
       ), 
      )); 
      $transport->setOptions($options); 
      $transport->send($mail);