2015-10-01 4 views
7

Ich habe eine Funktion gesendet, die E-Mails auf Laravel5 mit diesenüberprüfen Mail erfolgreich oder nicht auf 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'; 
} 

und gesendet wird erfolgreich senden kann. Aber wie überprüft man, ob es gesendet wurde oder nicht? Wie

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

Ich rate nur diesen Code.

+0

Sie versucht haben, 'Mail :: failures()' – haakym

+0

@haakym wie man meinen Code ändert, um sehen zu können, ob das funktioniert oder nicht ? Um diese Methode zu feuern? Danke –

+0

Hilft das? http://stackoverflow.com/questions/24772531/laravel-mail-sending-email-but-returning-false – haakym

Antwort

10

Ich bin nicht ganz sicher, dass dies funktionieren würde, aber man kann es versuchen

/** 
* 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

Ich denke nicht, dass es eine äquivalente Methode für die Anzahl der Erfolge gibt? 'Mail :: results()' gibt einen Fehler mit 'Call to undefined method' aus. – trysis

+0

https://laravel.com/api/5.2/Illuminate/Mail/Mailer.html Nein. Tun Sie einfach die Anzahl der Empfänger minus die Anzahl der Fehler und Sie erhalten Ihre Erfolgszählung. – haakym

4

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!"; 
} 

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

+1

Zumindest Quelle die Antwort;) http://laravel.io/forum/08-08-2014-how-to-know-if-e-mail-was-sent – haakym

+1

Entschuldigung für die Erwähnung dieser .. –