Symfony2 SwiftMailer无法正常工作

时间:2015-11-18 08:54:14

标签: php symfony send swiftmailer mailer

我希望在我的动作控制器中完成插入数据库但使用SwiftMailer进行发送和发送电子邮件但不起作用.. 我使用swiftmailer作为FOSUserBundle,它的工作非常好.. 这是config.yml:

swiftmailer:
    transport: "%mailer_transport%"
    host:      "%mailer_host%"
    username:  "%mailer_user%"
    password:  "%mailer_password%"
    port:                 false
    encryption:           ~
    auth_mode:            ~
    spool:
        type:                 file
        path:                 "%kernel.cache_dir%/swiftmailer/spool"
    sender_address:       ~
    antiflood:
    threshold:            99
    sleep:                0
    delivery_address:     ~
    disable_delivery:     ~
    logging:              "%kernel.debug%"

# Routing
    be_simple_i18n_routing: ~

# FOSUserBundle Configuration
fos_user:
    db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel'
    firewall_name: main
    user_class: Dt\EcBundle\Entity\User 
profile:
    form:
        type: dt_ec_profile
registration:
    confirmation:
        enabled:    true
    form:
        type: dt_ec_registration
from_email:
    address:        ------@------.com
    sender_name:    ------
service:
    mailer: fos_user.mailer.twig_swift
resetting:
    email:
        template: DtEcBundle:User:resetting.email.html.twig

这是控制器的代码:

$container->get('swiftmailer.mailer');

$mailer = \Swift_Mailer::newInstance();
$message = \Swift_Message::newInstance()
  ->setSubject('Hello Email')
        ->setFrom('------@-----.com')
        ->setTo('------@-----.com')
        ->setBody('You should see me from the profiler!')
    ;

/*    // Pass a variable name to the send() method
if (!$mailer->send($message, $failures))
{
  echo "Failures:";
  var_dump($failures);
} */

$this->get('mailer')->send($message);

我该怎么办?

2 个答案:

答案 0 :(得分:1)

你基本上试过你的控制器了吗?

/.../

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

$message = \Swift_Message::newInstance()
    ->setSubject('Hello Email')
    ->setFrom('------@-----.com')
    ->setTo('------@-----.com')
    ->setBody('You should see me from the profiler!')
;

$mailer->send($message);

/.../

答案 1 :(得分:0)

这是因为您在config.yml

中设置了假脱机配置

尝试评论这些行:

spool:
    type:                 file
    path:                 "%kernel.cache_dir%/swiftmailer/spool"

假脱机会将您的所有电子邮件存储在app/cache/dev/swiftmailer/spool文件夹中,并等待命令刷新它们。