允许使用Swiftmailer

时间:2016-03-15 10:53:29

标签: php symfony email swiftmailer

我使用SwiftmailerSymfony 2.8一起将邮件从gmail帐户发送到Outlook帐户。 我在我的控制器中使用的代码:

$message = \Swift_Message::newInstance()
    ->setSubject('Test Email')
    ->setFrom('xxxxx@xxxxx.com')
    ->setTo('xxxxxx@outlook.com')
    ->setBody(
        $this->renderView(

            'QualifBundle:Mails:Welcome.html.twig'
            ),
        'text/html'
    );
$this->get('mailer')->send($message);

配置文件:

# Swiftmailer Configuration
swiftmailer:
    transport: %mailer_transport%
    encryption: %mailer_encryption%
    auth_mode:  %mailer_auth_mode%
    host:      %mailer_host%
    username:  %mailer_username%
    password:  %mailer_password%
    spool:     { type: memory }

参数yml:

mailer_username: xxxxxxx@gmail.com
mailer_password: xxxxxxxxx
mailer_transport:  smtp
mailer_encryption: ssl
mailer_auth_mode:  login
mailer_host: smtp.gmail.com

我设法解决了SSL错误,现在使用此代码我的应用程序运行没有例外,但邮件未收到(甚至可能没有发送) 我该如何解决或指纹?

更新 我删除了spool:{ type: memory }以便邮件直接发送,我得到了这个例外。

Connection could not be established with host 127.0.0.1 [Connection refused #111] 

我的主机应该是smtp.gmail.com而不是localhost任何想法?

解决方案 我通过侵入供应商的文件解决了我的问题,我知道它不是最好的解决方案,但至少我可以发送邮件。

查找_establishSocketConnection()中的StreamBuffer.php功能应该在此路径中/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport进入项目。 并添加以下行以逃避SSL验证:

//...
$options = array();
if (!empty($this->_params['sourceIp'])) {
    $options['socket']['bindto'] = $this->_params['sourceIp'].':0';
}
//Add those two lines
$options['ssl']['verify_peer'] = FALSE;
$options['ssl']['verify_peer_name'] = FALSE;
//...

然后我重新启动浏览器,我的邮件就像魅力一样

3 个答案:

答案 0 :(得分:1)

首先检查你的Gmail帐户,你必须在你的Gmail帐户中打开"Access for less secure apps"

答案 1 :(得分:0)

<强>解决方案 我通过侵入供应商的文件解决了我的问题,我知道它不是最好的解决方案,但至少我可以发送邮件。

查找_establishSocketConnection()中的StreamBuffer.php功能应该在此路径中/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport进入项目。 并添加以下行以逃避SSL验证:

//...
$options = array();
if (!empty($this->_params['sourceIp'])) {
    $options['socket']['bindto'] = $this->_params['sourceIp'].':0';
}
//Add those two lines
$options['ssl']['verify_peer'] = FALSE;
$options['ssl']['verify_peer_name'] = FALSE;
//...

然后我重新启动浏览器,我的邮件就像魅力一样

答案 2 :(得分:0)

另一种方式:

$opt['ssl']['verify_peer'] = FALSE;
$opt['ssl']['verify_peer_name'] = FALSE;

$this->get('swiftmailer.mailer.default.transport.real')->setStreamOptions($opt);

上面的代码放在:\Swift_Message::newInstance()

之前