【问题标题】:Send email outside Controller Action in Symfony2在 Symfony2 中的控制器操作之外发送电子邮件
【发布时间】:2020-06-22 17:42:45
【问题描述】:

我正在使用 Symfony2 和 FOSUserBundle

我必须在我的邮件程序类中使用 SwiftMailer 发送电子邮件,这不是控制器或其操作。我正在展示我编码的内容

<?php

namespace Blogger\Util;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class FlockMailer {


    public function SendEmail(){
        $message = \Swift_Message::newInstance()
        ->setSubject('Hello Email')
        ->setFrom('send@example.com')
        ->setTo('to@example.com')
        ->setBody('testing email');

        $this->get('mailer')->send($message);
    }
}

但我收到以下错误

Fatal error: Call to undefined method Blogger\Util\FlockMailer::get() ....

我该如何继续?

【问题讨论】:

  • 我不明白。该代码 sn-p 是否意味着您当前的解决方案,但您想将邮件移动到其他地方?如果这是正确的,您可能应该阅读有关将服务注入自定义类的信息:stackoverflow.com/questions/6124444/…
  • 我已经使用了 FOSUserBundle 和 FOSFacebookbundle 当用户使用 facebook 帐户成功登录时我想要的我想用他的密码向用户发送电子邮件,以便他可以使用该电子邮件密码登录为此我必须编写函数在提供者类中发送电子邮件 ....

标签: symfony fosuserbundle


【解决方案1】:

编辑:因为我没有测试代码,如果你不使用服务容器来获取邮件程序的实例,你还应该指定传输层。看:http://swiftmailer.org/docs/sending.html

你做错了。您基本上想要一个服务,而不是扩展Controller 的类。它不起作用,因为SendMail() 函数中没有服务容器。

您必须将服务容器注入到您自己的自定义助手中才能发送电子邮件。几个例子:

namespace Blogger\Util;

class MailHelper
{
    protected $mailer;

    public function __construct(\Swift_Mailer $mailer)
    {
        $this->mailer = $mailer;
    }

    public function sendEmail($from, $to, $body, $subject = '')
    {
        $message = \Swift_Message::newInstance()
            ->setSubject($subject)
            ->setFrom($from)
            ->setTo($to)
            ->setBody($body);

        $this->mailer->send($message);
    }
}

控制器操作中使用它:

services:
    mail_helper:
        class:     namespace Blogger\Util\MailHelper
        arguments: ['@mailer']

public function sendAction(/* params here */)
{
    $this->get('mail_helper')->sendEmail($from, $to, $body);
}

或其他地方无需访问服务容器

class WhateverClass
{

    public function whateverFunction()
    {
        $helper = new MailerHelper(new \Swift_Mailer);
        $helper->sendEmail($from, $to, $body);
    }

}

或者在自定义服务中访问容器

namespace Acme\HelloBundle\Service;

class MyService
{
    protected $container;

    public function setContainer($container) { $this->container = $container; }

    public function aFunction()
    {
        $helper = $this->container->get('mail_helper');
        // Send email
    }
}

services:
    my_service:
        class: namespace Acme\HelloBundle\Service\MyService
        calls:
            - [setContainer,   ['@service_container']]

【讨论】:

  • 我在没有访问服务容器的情况下实现了以下错误:====== 可捕获的致命错误:传递给 Swift_Mailer::__construct() 的参数 1 必须是 Swift_Transport 的实例,没有给出,
  • @MuhammadUmair 是的,您必须指定传输层。我没有测试代码。你应该关注symfony.com/doc/current/cookbook/email/email.html
【解决方案2】:

忘记 setter 和 getter:

$transport = \Swift_MailTransport::newInstance();
$mailer = \Swift_Mailer::newInstance($transport);
$helper = new MailHelper($mailer);
$helper->sendEmail($from, $to, $body,$subject);

这对我来说适用于从侦听器方法调用的 MailHelper。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-05
    • 2020-11-03
    • 2016-09-02
    • 1970-01-01
    • 2016-11-17
    • 2013-11-28
    相关资源
    最近更新 更多