【问题标题】:Symfony2 SwiftMailer don't workSymfony2 SwiftMailer 不工作
【发布时间】:2016-02-19 22:01:50
【问题描述】:

我想在我的动作控制器中完成插入数据库但不起作用时使用 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);

我该怎么办??

【问题讨论】:

    标签: php symfony send swiftmailer mailer


    【解决方案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);
    
    /.../
    

    【讨论】:

    • 我尝试过这种方式,但现在什么都没有,我再试一次但没有用..@scoolnico
    • 您是否有特定的错误信息或邮件只是没有发送?
    • 我解释一下,我的网络项目中有一个消息聊天,当我发送消息时,控制器会向消息的目的地发送一封电子邮件。但我没有收到任何错误......邮件只是不发送@scoolnico
    • 正如 Julien 所说,spool 可能是原因。在您的控制台中尝试此命令行并告诉我们您是否收到消息:php app/console swiftmailer:spool:send
    • 对这个方向不是很乐观,但在你的config.yml 中尝试disable_delivery: false...
    【解决方案2】:

    这是因为您的 config.yml 中有一个假脱机配置

    尝试评论这些行:

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

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

    【讨论】:

    • Nothing.. 在这个路径中我没有文件。我怎样才能收到错误消息以理解问题的句子? @scoolnico
    • 通过跟踪 symfony 的日志tail -f app/logs/dev 如果你使用的是 linux 或其他类似的东西。在你的 nginx/apache error.log 上做同样的事情
    • 我不知道...@Julien Bourdic 我看到了 Symfony 日志,但我不明白,真的..
    • 另一种拦截邮件发生情况的方法 => 在 app/config/config_dev.yml : web_profiler: intercept_redirects: true
    • @scoolnico 我试过但不是好主意..不是这个问题..现在我尝试使用 disable_delivery: false
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-10
    • 1970-01-01
    • 1970-01-01
    • 2014-10-27
    • 1970-01-01
    • 2012-11-19
    • 1970-01-01
    相关资源
    最近更新 更多