2014-05-07 86 views
8

我試圖使用Mail ::隊列來發送和發送電子郵件,但是當我調用這個函數時,它簡單地發送郵件,並且響應被延遲了......我認爲使用Mail ::隊列的一點是要排隊....Laravel:如何排隊郵件以後發送

我想應對瞬間來了,不必等待發送

爲如

Mail::queue('emails.template', $data, function($message) { 
    $message->to('[email protected]'); 
    $message->subject('Notificacion'); 
}); 

return Response::json(array('error' => 0, 'message' => 'Ok')); 

我的電子郵件希望在不等待郵件發送的情況下收到回覆。 我該怎麼做?

+0

Mail :: later方法做同樣的事情,它只是等待任何時候我告訴它等待和響應延遲 –

+0

這實際上是不可能的。您將從服務器獲得您執行的操作的響應。 –

+0

@GabrielMatusevich請問您是否可以獲得解決方案?我也遇到了同樣的問題,當使用laravel5 – atom2ueki

回答

3

你正在使用什麼隊列驅動程序(app/config/queue.php - 'default' param)?如果您使用的是sync,並且沒有設置其他人之一,那麼您使用的是同步驅動程序,它的名稱確實如此:創建任務後立即運行排隊的任務。

您需要爲Laravel配置一個MQ服務器來與之通話。你可以得到一個免費的iron.io考慮到這一點,然後你需要對它進行配置,例如:

'iron' => array(
    'driver' => 'iron', 
    'project' => 'iron-io-project-id', 
    'token' => 'iron-io-queue-token', 
    'queue' => 'queue-name', 
), 

然後,當你使用它Mail::queue()將推指令iron.io.然後,您必須讓另一個線程在隊列中偵聽 - 只需運行php artisan queue:listen並在消息被推入隊列時保持運行。

+1

這真的很有幫助,但我實際上正在尋找一種方式來做到這一點,而不依賴於另一項服務:P –

+0

beanstalkd呢?你有一個配置它的例子嗎? –

+1

使用iron.io真的會是最快的方式來建立它。對於beanstalkd,一旦你安裝了它,你應該可以使用配置的默認值 - 只要將第18行的'default'更改爲'beanstalkd'。 – Wogan

-1
/** 
* Get all email recipients and include their user details for Mailgun's 
* template tags - %recipient.userToken% 
*/ 
private function getRecipients() 
{ 
    foreach (User::get() as $user) 
    { 
     $this->recipients[$user->email] = [ 
      'id' => $user->id, 
      'userToken' => $user->user_token, 
      'first_name' => $user->first_name, 
      'last_name' => $user->last_name, 
      'email' => $user->email 
     ]; 
    } 
} 

private function sendEmail() 
{ 
    $subject = 'Demo Subject'; 
    /** 
    * Data for the Blade template 
    */ 
    $data = [ 
     'foo' => 'bar' 
    ]; 
    // Inline the CSS for the email 
    $inliner = new InlineEmail('emails.some-email', $data); 
    $content = $inliner->convert(); 

    // Create Emails table entry for this email. Used for Mailgun webhooks 
    $email = Email::create(['user_id' => $this->userId, 'subject' => $subject, 'email_id' => str_random()]); 

    // Prepare the email addresses 
    $emailAddresses = array_column($this->recipients, 'email'); 

    $this->mailgun->sendMessage('demo.org', [ 
     "from" => '[email protected]', 
     "to" => implode(',', $emailAddresses), // Comma separated list of email addresses 
     "subject" => $subject, 
     "html" => $content, // Inlined CSS HTML from Blade 
     "text" => "Plain text message here", 
     "recipient-variables" => json_encode($this->recipients), // Required for batch sending, matches to recipient details 
     "v:messageId" => $email->id, // Custom variable used for webhooks 
    ]); 
} 
相關問題