2013-06-11 8 views
13

Sto usando la nuova classe di posta in Laravel 4, qualcuno sa come controllare se l'email è stata inviata? Almeno che la posta è stato consegnato con successo verso la MTA ...Laravel 4 mail class, come sapere se l'email è stata inviata?

+0

Sì smussa basta andare al App-> config-> elettronica e cambiare finta => true ?? In questo modo è possibile visualizzare i messaggi nel registro – KyleK

risposta

11

Se fai

if (! Mail::send(array('text' => 'view'), $data, $callback)) 
{ 
    return View::make('errors.sendMail'); 
} 

Saprete quando è stato inviato o no, ma potrebbe essere meglio, perché SwiftMailer sa wich destinatari è fallito, ma laravel non sta esponendo il parametro relativo per aiutarci ad ottenere queste informazioni:

/** 
* Send the given Message like it would be sent in a mail client. 
* 
* All recipients (with the exception of Bcc) will be able to see the other 
* recipients this message was sent to. 
* 
* Recipient/sender data will be retrieved from the Message object. 
* 
* The return value is the number of recipients who were accepted for 
* delivery. 
* 
* @param Swift_Mime_Message $message 
* @param array    $failedRecipients An array of failures by-reference 
* 
* @return integer 
*/ 
public function send(Swift_Mime_Message $message, &$failedRecipients = null) 
{ 
    $failedRecipients = (array) $failedRecipients; 

    if (!$this->_transport->isStarted()) { 
     $this->_transport->start(); 
    } 

    $sent = 0; 

    try { 
     $sent = $this->_transport->send($message, $failedRecipients); 
    } catch (Swift_RfcComplianceException $e) { 
     foreach ($message->getTo() as $address => $name) { 
      $failedRecipients[] = $address; 
     } 
    } 

    return $sent; 
} 

ma è possibile estendere Mailer di laravel e aggiungere tale funzionalità ($ failedRecipients) per il metodo di invio della nuova classe.

EDIT

In 4.1 ora è possibile avere accesso a destinatari che non usando

Mail::failures(); 
+0

Grazie, ma ho solo bisogno della prima parte, ma se ad esempio ho questo destinatario "ebc @ vd" ottengo ancora il numero 1, e se uso 'fdfdfd' per il destinatario si blocca! Mi piacerebbe ottenere 0 in casi fa. Penso che sia un problema con l'implementazione di Laravel .. – ebelendez

+0

Sì, è strano, non dovrebbe bloccarsi in questo modo, ma Laravel usa SwiftMailer, quindi deve esserci un problema. Per quanto riguarda l'invio di posta, a volte non si verifica un errore durante l'invio di un messaggio, poiché il server SMTP ha accettato il messaggio e invierà un'e-mail che informa che il messaggio non è stato recapitato. –

+2

Basta notare che con 4.1 siamo ora in grado di ottenere "failedRecipients" tramite 'failures()' :) – seus

1

Antonio ha un buon punto di non sapere che non è riuscito.

Le domande reali sono comunque il successo. Non ti interessa che sia fallito quanto se ANY abbia fallito. Ecco un esempio per verificare se c'è un errore.

$count=0; 
$success_count = \Mail::send(array('email.html', 'email.text'), $data, function(\Illuminate\Mail\Message $message) use ($user,&$count) 
{ 
    $message->from($user->primary_email, $user->attributes->first.' '.$user->attributes->last); 
    // send a copy to me 
    $message->to('[email protected]', 'Example')->subject('Example Email'); 
    $count++ 
    // send a copy to sender 
    $message->cc($user->primary_email); 
    $count++ 
} 
if($success_count < $count){ 
    throw new Exception('Failed to send one or more emails.'); 
} 
1
if(count(Mail::failures()) > 0){ 
       //$errors = 'Failed to send password reset email, please try again.'; 
       $message = "Email not send"; 
      } 
return $message; 
Problemi correlati