如何在 symfony 2 中配置 SwiftMailer?

Posted

技术标签:

【中文标题】如何在 symfony 2 中配置 SwiftMailer?【英文标题】:how to configure SwiftMailer in symfony 2? 【发布时间】:2019-11-02 07:00:16 【问题描述】:

拜托,我是 Symfony 的新手,我正在尝试在 Symfony 2 上配置邮件程序,但我没有找到任何明确的文档。

我需要一个路由示例来从一个地址向另一个地址发送电子邮件来测试这个。 谢谢

我尝试安装 swift mailer 并写了一些代码


<?php

namespace MailManager\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;

/**
* Email Sender Manager
* @Route("/service-mail/mail")
**/

Class MailManagerController extends AbstractController 

    /**
      * @Route("/send")
      */
      public function send(\Swift_Mailer $mailer)
          $message = \Swift_Message::newInstance()
          ->setsubject('Mail test')
          ->setFrom(['mail@example.com'] => 'xxxxxx')
          ->setTo(['mail1@example.com'] => 'xxxxxx')
          ->setBcc(['mail2@example.com'] => 'vvvvv')
          ->setBody('Test Test');
          $this->get('mailer')
          ->send($message);
      


我希望在我在邮递员中输入路线时自动将电子邮件从我的电子邮件地址发送到另一个预定义的电子邮件地址。

【问题讨论】:

如果你是 symfony 的新手,你不应该使用 2.1 版本。是否可以选择使用更现代的版本(当前版本 4.3)? 为什么会有symfony4symfony-2.1标签,你用的也是那个寿命结束的版本 @ehymel 实际上我正在从事一个已经用 symfony 2 编写的大项目,所以我必须使用 symfony 2 来开发这个项目。 Symfony2, send mail to gmail的可能重复 【参考方案1】:

你能改吗

->setFrom(['mail@example.com'] => 'xxxxxx')
          ->setTo(['mail1@example.com'] => 'xxxxxx')
          ->setBcc(['mail2@example.com'] => 'vvvvv')

->setFrom(['mail@example.com' => 'xxxxxx'])
          ->setTo(['mail1@example.com' => 'xxxxxx'])
          ->setBcc(['mail2@example.com' => 'vvvvv'])

【讨论】:

以上是关于如何在 symfony 2 中配置 SwiftMailer?的主要内容,如果未能解决你的问题,请参考以下文章

如何在Symfony4中配置bundle?

如何禁用 Symfony 2 分析器栏?

如何在 Symfony 中增加会话超时

如何在 Symfony2 配置中添加具有值的数组?

如何在 Symfony 3.4 中设置基于域的配置?

如何在 Sonata/Symfony 3.3 中设置内容配置?