2014-03-12 39 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);