2013-11-15 34 views
0

我正在開發一個應用程序與symfony2 在應用程序的一邊我發送電子郵件,一切正常與此。 但現在我創建一個命令在crontab中運行,但不發送電子郵件。 這是我的命令: 使用Doctrine \ ORM \ EntityManager; 使用Symfony \ Component \ Templating \ EngineInterface;symfony2電子郵件沒有發送命令

class Sender { protected $ em;受保護的$ twig;受保護的$郵件程序; public function __construct($ em,\ Twig_Environment $ twig,\ Swift_Mailer $ mailer){ $ this-> em = $ em; $ this-> twig = $ twig; $ this-> mailer = $ mailer; }

public function runSender() { 
    $proj = $this->em->createQuery ... 
    $maillist = $this->em->createQuery ... 
$templateFile = "projectprojBundle:MailList:emailNew.html.twig"; 
$templateContent = $this->twig->loadTemplate($templateFile); 
$body = $templateContent->render(array('proj' => $proj)); 

    foreach ($maillist as $m) { 
    $message = \Swift_Message::newInstance()->setSubject('New projects') 
    ->setFrom('...')->setTo($m['email']) 
    ->setContentType('text/html') 
    ->setBody(trim($body)); 
    $this->mailer->send($message); 
    } } } 

一切都確定了查詢,我測試。 如果我可以從其他班級發送爲什麼我不能在這裏?在config.yml

使用spool: { type: memory }

+0

您使用假脫機內存嗎?如果是,這是問題。 見http://stackoverflow.com/questions/13122096/unable-to-send-e-mail-from-within-custom-symfony2-command-but-can-from-elsewhere –

+0

是使用im閥芯內存。它可以在其他類中使用。但在這裏它不工作,我不知道爲什麼你 – BrunoRamalho

+0

可以澄清它可以在其他類CLI或當你在網絡工作。這是一個重點。檢查我發佈的鏈接。 –

回答

3

在你的命令的執行結束補充一點:

$spool = $this->mailer->getTransport()->getSpool(); 
$transport = $this->getContainer()->get('swiftmailer.transport.real'); 

$spool->flushQueue($transport); 

您必須擴展ContainerAwareCommand類才能訪問命令中的服務容器。

+0

發送的電子郵件,甚至從網絡 – BrunoRamalho

+0

上調用一個成員函數getKernel() @BrunoRamalho確保你不用'configure'方法調用這些方法。所有的代碼都必須從'execute'方法或這個方法的子方法中調用。 –

0

可能是你的後臺打印設置,以發送電子郵件,即時

# app/config/config.yml 
swiftmailer: 
    # ... 
    spool: { type: memory } 
+0

IM和我得到這個錯誤:非對象 – BrunoRamalho