【问题标题】:Symfony2 send email from serviceSymfony2 从服务发送电子邮件
【发布时间】:2017-08-05 03:09:27
【问题描述】:

我创建了下一个类:

//src/AppBundle/Services/RegisterMail.php 命名空间 AppBundle\服务; 类注册邮件{ 受保护的$mailer; 公共函数 __construct($mailer) { $this->mailer = $mailer; } 公共函数发送密码(){ $message = \Swift_Message::newInstance() ->setSubject('Otro correo') ->setFrom('fromEmail@fromEmail.com') ->setTo('toEmail@toEmail.com') ->setBody('hola desde el servicio') ; $envia = $this->mailer->send($message); } }

我在 services.yml 中将其声明为服务

服务: 注册邮箱: 类:AppBundle\Services\RegisterMail 论据:[@mailer]

在我的控制器中调用服务:

//src/AppBundle/Controller/DefaultController

namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;

class DefaultController extends Controller
{
    /**
    * @Route("/")
    */
    public function indexAction()
    {
        //Envío el email con el password
        $mailer = $this->get('registermail');
        $mailer->sendPassword();
        return $this->render(':Default:index.html.twig');
    }

}

电子邮件已发送,但页面仍在加载 30 秒,我从开发人员工具栏收到警告:“加载 Web 调试工具栏时出错(404:未找到)。您要打开分析器吗? 如果接受消息 symfony profiler 不会显示任何错误。 如果取消消息开发者工具栏不出现。

我做错了什么?

谢谢!

【问题讨论】:

    标签: symfony email service send


    【解决方案1】:

    @RL83 您可能正在通过在 swiftmailer 中不使用任何类型的假脱机来同步发送消息,并且您的 smtp 提供程序运行缓慢。

    您应该尝试使用异步假脱机队列,我建议使用https://github.com/radutopala/TSSAutomailerBundle,它为您提供数据库队列。所以基本上,您不仅会有一个假脱机队列,还有一个已发送电子邮件的历史记录,存储在数据库层中。

    【讨论】:

      【解决方案2】:

      尝试用以下代码替换您的代码:

      //src/AppBundle/Services/RegisterMail.php
      
      namespace AppBundle\Services;
      
      class RegisterMail{
        protected $mailer;
      
        protected $transport; // <- Add transport to your service
      
        public function __construct($mailer, $transport)
        {
            $this->mailer    = $mailer;
            $this->transport = $transport;
        }
      
        public function sendPassword() // <- change the method like this
        {
             $email = $mailer->createMessage()
              ->setSubject('Otro correo')
              ->setFrom('fromEmail@fromEmail.com')
              ->setTo('toEmail@toEmail.com')
              ->setCharset("UTF-8")
              ->setContentType('text/html')
              ->setBody('hola desde el servicio')
          ;
      
          $send = $mailer->send($email);
          $spool->flushQueue($transport);   
        }
      }
      

      注册您的服务并添加新的依赖项 - 传输服务"@swiftmailer.transport.real"

      services:
      registermail:
          class: AppBundle\Services\RegisterMail
          arguments: ["@mailer", "@swiftmailer.transport.real" ]
      

      你的麻烦就解决了

      【讨论】:

      • 感谢您的回答,对延误表示歉意。 @lyberteam 当我输入您的代码时,我收到此错误消息:注意:未定义变量:mailer 500 内部服务器错误 - ContextErrorException 这里:$email = $mailer->createMessage() 有什么建议吗?谢谢
      【解决方案3】:

      感谢您的回答,抱歉耽搁了。

      @lyberteam 当我输入您的代码时,我收到以下错误消息: 注意:未定义变量:mailer 500 内部服务器错误 - ContextErrorException

      这里:$email = $mailer->createMessage()

      有什么建议吗?

      谢谢

      【讨论】:

        猜你喜欢
        • 2012-10-09
        • 1970-01-01
        • 1970-01-01
        • 2015-09-10
        • 1970-01-01
        • 1970-01-01
        • 2016-09-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多