Symfony2 Swiftmailer 不发送 [英] Symfony2 Swiftmailer Not Sending

查看:22
本文介绍了Symfony2 Swiftmailer 不发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 symfony2 和 swiftmailer 发送电子邮件时遇到问题.我对如何调试这个问题也有点迷茫.下面是代码.首先,我正在创建一个要显示的表单.在提交(请求-> 方法 == 发布)时,我尝试发送电子邮件.我没有收到任何错误,它把我带到了谢谢页面,但是,我没有收到任何电子邮件.我已经在 prod 和 dev 上进行了测试.在开发中,我在提交后打开了分析器,它显示了 0 封电子邮件.任何帮助表示赞赏!谢谢!

I am having trouble sending emails with symfony2 and swiftmailer. I am also a bit lost on how to debug this issue. Below is the code. First I am creating a form to display. On submit (request->method == post) I then try to send the email. I am getting no errors and it is taking me to the thankyou page, however, I am not receiving any email. I have tested on prod and dev. In dev I have opened the profiler after submission and it shows 0 emails. Any help is appreciated! Thanks!

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());
}

推荐答案

您可以在此处找到有关如何使用 symfony2 发送电子邮件的整个过程.我刚刚测试它似乎工作正常.

You can find here the entire procedure on how to send emails with symfony2. I just tested it at it seems to work fine.

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

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

这篇关于Symfony2 Swiftmailer 不发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆