2015-10-01 20 views
7

Tôi có một chức năng có thể gửi mail về Laravel5 sử dụng nàyCheck mail được gửi thành công hay không trên Laravel 5

/** 
* Send Mail from Parts Specification Form 
*/ 
public function sendMail(Request $request) { 
    $data = $request->all(); 

    $messageBody = $this->getMessageBody($data); 

    Mail::raw($messageBody, function ($message) { 
     $message->from('[email protected]', 'Learning Laravel'); 
     $message->to('[email protected]'); 
     $message->subject('Learning Laravel test email'); 
    }); 

    return redirect()->back(); 
} 

/** 
    * Return message body from Parts Specification Form 
    * @param object $data 
    * @return string 
    */ 
private function getMessageBody($data) { 

    $messageBody = 'dummy dummy dummy dummy'; 
} 

và được gửi thành công. Nhưng làm thế nào để kiểm tra xem nó đã được gửi hay chưa? Giống như

if (Mail::sent == 'error') { 
echo 'Mail not sent'; 
} else { 
echo 'Mail sent successfully.'; 
} 

Tôi chỉ đoán mã đó.

+0

bạn đã thử 'Mail :: failures()' – haakym

+0

@haakym làm thế nào để thay đổi mã của tôi để có thể xem nếu công việc đó hay không ? Để bắn phương pháp đó? Cảm ơn –

+0

Điều này có hữu ích không? http://stackoverflow.com/questions/24772531/laravel-mail-sending-email-but-returning-false – haakym

Trả lời

10

Tôi không hoàn toàn chắc chắn điều này sẽ làm việc nhưng bạn có thể cho nó một shot

/** 
* Send Mail from Parts Specification Form 
*/ 
public function sendMail(Request $request) { 
    $data = $request->all(); 

    $messageBody = $this->getMessageBody($data); 

    Mail::raw($messageBody, function ($message) { 
     $message->from('[email protected]', 'Learning Laravel'); 
     $message->to('[email protected]'); 
     $message->subject('Learning Laravel test email'); 
    }); 

    // check for failures 
    if (Mail::failures()) { 
     // return response showing failed emails 
    } 

    // otherwise everything is okay ... 
    return redirect()->back(); 
} 
+0

Tôi không cho rằng có một phương pháp tương đương cho số lần thành công? 'Mail :: successes()' đưa ra một lỗi với 'Call to undefined method'. – trysis

+0

https://laravel.com/api/5.2/Illuminate/Mail/Mailer.html không. Chỉ cần làm số người nhận trừ số lần thất bại và bạn sẽ nhận được số thành công của mình. – haakym

4

Hope this helps

Mail::send(...) 

if(count(Mail::failures()) > 0) { 

    echo "There was one or more failures. They were: <br />"; 

    foreach(Mail::failures as $email_address) { 
     echo " - $email_address <br />"; 
    } 

} else { 
    echo "No errors, all sent successfully!"; 
} 

nguồn: http://laravel.io/forum/08-08-2014-how-to-know-if-e-mail-was-sent

+1

Ít nhất là nguồn câu trả lời;) http://laravel.io/forum/08-08-2014-how-to-know-if-e-mail-was-sent – haakym

+1

xin lỗi vì đã không đề cập đến điều đó .. –

Các vấn đề liên quan