Sendgrid Unauthenticated发件人不允许错误php,zf2

时间:2014-03-12 06:49:36

标签: zend-framework2 sendmail sendgrid

我使用STMP API为我的Zend Framework 2应用程序集成了Sendgrid,我使用了Zend Transport,但是我收到了错误

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

 $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('jainudeenf007@gmail.com')
                ->addFrom('fawazj@digitalglare.com.au')
                ->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;
        }
    }

3 个答案:

答案 0 :(得分:1)

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

我不确定如何准确配置SMTP选项,但之前我们使用的是Google 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

答案 1 :(得分:0)

除了使用SimMail选项之外,您还可以尝试使用Zend的邮件模块,如我们的文档中所述:http://sendgrid.com/docs/Integrate/Frameworks/zend.html

另一个选项是我们的Web API,我们在这里有一个PHP库:https://github.com/sendgrid/sendgrid-php

答案 2 :(得分:0)

试试这个

connection_class plain应该可以工作

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);