Symfony2 Swiftmailer没有发送

时间:2012-04-03 20:20:09

标签: symfony swiftmailer

我无法使用symfony2和swiftmailer发送电子邮件。我对如何调试此问题也有点迷失。下面是代码。首先,我正在创建一个要显示的表单。在提交(request-> method == post)后,我尝试发送电子邮件。我没有收到任何错误,它将我带到了谢谢你的页面,但是,我没有收到任何电子邮件。我已经测试了prod和dev。在开发中,我在提交后打开了分析器,它显示了0封电子邮件。任何帮助表示赞赏!谢谢!

public function contactAction(Request $request)
{
    $defaultData = array('name' => 'Name', 'email' => 'Email', 'subject' => 'Subject', 'message' => 'Message');
    $form = $this->createFormBuilder($defaultData)
        ->add('name', 'text')
        ->add('email', 'email')
        ->add('subject', 'text')
        ->add('message', 'textarea')
        ->getForm();

    if($request->getMethod() == 'POST') {
        $form->bindRequest($request);
        $data = $form->getData();
        $message = \Swift_Message::newInstance()
            ->setSubject($data['subject'])
            ->setFrom('no-reply@mysite.com')
            ->setTo('email@mysite.com')
            ->setBody($this->renderView('AdaptiveSiteBundle:Default:email.txt.twig', array('name' => $data['name'], 'message' => $data['message'])))
        ;
        $this->get('mailer')->send($message);

        return $this->redirect($this->generateUrl('thankyou'));
    } 

    return array("form" => $form->createView());
}

7 个答案:

答案 0 :(得分:24)

您可以发布parameters.yml吗?

还要确保禁用假脱机,以便立即发送电子邮件。如果您在Swiftmailer配置下有假脱机条目,请将其删除,例如:

swiftmailer:
    transport: %mailer_transport%
    host:      %mailer_host%
    username:  %mailer_user%
    password:  %mailer_password%
    spool:     { type: memory }

应该是:

swiftmailer:
    transport: %mailer_transport%
    host:      %mailer_host%
    username:  %mailer_user%
    password:  %mailer_password%

答案 1 :(得分:13)

您可能有邮件假脱机设置。如果是这种情况,您需要运行:

php app/console swiftmailer:spool:send

发送假脱机电子邮件。

查看http://symfony.com/doc/master/cookbook/email/spool.html了解更多信息。

答案 2 :(得分:10)

您可以在此处找到有关如何使用symfony2发送电子邮件的完整步骤。我只是测试它似乎工作正常。

http://tutorial.symblog.co.uk/docs/validators-and-forms.html#sending-the-email

http://symfony.com/doc/current/email.html

答案 3 :(得分:5)

我经常在config_dev.yml中将以下配置设置为在测试期间发送的--prevent-邮件,也许你已经做了同样的事情并忘了?

如果是在config_dev.yml中,请将其设置为false:

swiftmailer:
  disable_delivery:  true

答案 4 :(得分:5)

除了上述解决方案之外,我建议您从使用swiftmailer代码的函数中删除dieexit。如果您的代码正确,这将解决您的问题。

答案 5 :(得分:0)

我无法通过ovh,siwftmailer和fosUserBundle接收电子邮件,

请考虑在config.yml中添加此内容

fos_user:
    from_email:
            address:        noreply@yourdomain.com
            sender_name:    yourname

如果您不这样做,fos用户捆绑包将发送电子邮件至noreply@exemple.com,OVH将此标记为垃圾邮件。

来源:https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/emails.md

答案 6 :(得分: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);
相关问题