在Symfony2中向外部控制器操作发送电子邮件
我使用的是Symfony2和FOSUserBundle 我必须在我的mailer类中使用SwiftMailer发送电子邮件,该类不是控制器或其操作。我正在显示我的代码在Symfony2中向外部控制器操作发送电子邮件,symfony,fosuserbundle,Symfony,Fosuserbundle,我使用的是Symfony2和FOSUserBundle 我必须在我的mailer类中使用SwiftMailer发送电子邮件,该类不是控制器或其操作。我正在显示我的代码 <?php namespace Blogger\Util; use Symfony\Bundle\FrameworkBundle\Controller\Controller; class FlockMailer { public function SendEmail(){ $message
<?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);
}
}
如何继续?编辑:由于我没有测试代码,如果不使用服务容器获取邮件程序实例,还应指定传输层。看看: 你做错了。您基本上想要的是服务,而不是扩展
控制器
的类。它不工作,因为服务容器在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']]
或其他地方而不访问服务容器:
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']]
或在自定义服务中访问容器:
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']]
忘了二传手和三传手吧:
$transport = \Swift_MailTransport::newInstance();
$mailer = \Swift_Mailer::newInstance($transport);
$helper = new MailHelper($mailer);
$helper->sendEmail($from, $to, $body,$subject);
对于从侦听器方法调用的MailHelper,这对我很有效。我不明白。这段代码是作为您当前的解决方案,但您想将邮件转移到其他地方吗?如果这是正确的,您可能应该阅读有关将服务注入自定义类的内容:我使用了FOSUserBundle和FOSFacebookbundle当用户成功使用facebook帐户登录时,我希望使用他的密码向用户发送电子邮件,以便他可以使用该电子邮件密码登录。为此,我必须在provider类中编写函数以发送电子邮件..我在未访问服务容器的情况下实现了以下错误:可捕获致命错误:传递给Swift_Mailer的参数1::_construct()必须是Swift_传输的实例,未给出任何实例,@MuhammadUmair是的,您必须指定传输层。我没有测试代码。你应该跟着