2012-04-20 84 views
5

我使用的Symfony2和FOSUserBundle發送電子郵件外控制器動作的Symfony2

我要送使用SwiftMailer電子郵件在我的郵件類,這是不是一個控制器或它的行動我顯示我所編碼

<?php 

namespace Blogger\Util; 

use Symfony\Bundle\FrameworkBundle\Controller\Controller; 

class FlockMailer { 


    public function SendEmail(){ 
     $message = \Swift_Message::newInstance() 
     ->setSubject('Hello Email') 
     ->setFrom('[email protected]') 
     ->setTo('[email protected]') 
     ->setBody('testing email'); 

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

但我收到以下錯誤

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

任何機構可以幫助我,這真的是我fustrating .....

+0

我不明白。該代碼片段是否意味着您當前的解決方案,但您想將郵件移動到其他位置?如果這是正確的,你應該閱讀有關注入服務到你的自定義類:http://stackoverflow.com/questions/6124444/how-can-i-access-a-service-outside-of-a-controller-with- symfony2 – geca 2012-04-20 13:09:18

+0

我已經使用FOSUserBundle和FOSFacebookbundle我想要什麼時,用戶成功登錄Facebook帳戶時,我想用他的密碼發送電子郵件給用戶,以便他可以使用該電子郵件密碼登錄,因爲我必須在提供者類中編寫函數以發送電子郵件... – 2012-04-20 13:17:17

回答

1

忘掉的調節器和吸氣:

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

,對我從監聽方法稱爲MailHelper工作。

8

編輯:因爲我沒有測試過代碼,所以如果你不使用服務容器來獲取郵件的實例,你還應該指定傳輸層。看看: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']] 
+0

我已經實現了無需訪問服務容器的錯誤來了====== Catchable致命錯誤:傳遞給Swift_Mailer :: __ construct()的參數1必須是Swift_Transport的實例,沒有給出, – 2012-04-20 13:31:15

+0

@MuhammadUmair yes ,你必須指定傳輸層。我沒有測試代碼。你應該按照http://symfony.com/doc/current/cookbook/email/email.html – gremo 2012-04-20 13:34:21

+0

@MuhammadUmair也看看http://swiftmailer.org/docs/sending.html – gremo 2012-04-20 13:36:18