电子邮件不会发送到Swiftmailer Symfony2

时间:2014-11-29 17:04:30

标签: php email symfony swiftmailer

我使用Swiftmailer在我的Symfony 2.5项目中发送电子邮件。这是我的相关代码

parameters.yml

parameters:
mailer_transport: smtp
mailer_host: mail.domain.com
mailer_username: user@domain.com
mailer_password: password
mailer_auth_mode:  login

config.yml

swiftmailer:
transport: %mailer_transport%
auth_mode:  %mailer_auth_mode%
host:      %mailer_host%
username:  %mailer_username%
password:  %mailer_password%
spool:     { type: memory }

控制器:

$message = \Swift_Message::newInstance()
        ->setSubject('mana')
        ->setFrom('***@gmail.com')
        ->setTo('***@yahoo.com')
        ->setBody('this is test');
    $this->get('mailer')->send($message);

我在appKernel.php

中有swiftmailer
$bundles = array(
        ...
        new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
        ...
    );

电子邮件未收到。你能救我吗?

2 个答案:

答案 0 :(得分:4)

以下代码在命令中有效但我将其改编为控制器:

$message = \Swift_Message::newInstance()
    ->setSubject('mana')
    ->setFrom('****@gmail.com')
    ->setTo('mana@yahoo.com')
    ->setBody('this is test');
# I removed this line: $this->get('mailer')->send($message);

$mailer = $this->get('mailer');

$mailer->send($message);

$spool = $mailer->getTransport()->getSpool();
$transport = $this->get('swiftmailer.transport.real');

$spool->flushQueue($transport);

来源:Symfony2 documentation

答案 1 :(得分:0)

config.yml

# Swiftmailer Configuration
swiftmailer:
    transport:  smtp
    encryption: ssl
    auth_mode:  login
    host:       smtp.xx.eu
    username:   username
    password:   password 

控制器/动作

$messageObject = \Swift_Message::newInstance()
            ->setSubject('Subject')
            ->setFrom('username@xx.eu')
            ->setTo('xxx@stackoverflow.eu')
            ->setBody('message');
$this->get('mailer')->send($messageObject);

parameters.yml -default

mailer_transport: smtp
mailer_host: 127.0.0.1
mailer_user: null
mailer_password: null