Support the ongoing development of Laravel.io →
posted 10 years ago
Mail

In development and QA, i'd like to record the Mail object's transaction history to a DB table, but am unsure as to how to go about accessing the data for the class. At the moment i do not want to log to a static file.

It's particularly useful for tracing emails - a lot of users experience problems when doing password resets etc because their provider (Hotmail, Gmail) marks the mail as spam, meaning i get a lot "i never got the email it was supposed to send" messages. Looking it up in the DB means i can see directly that the email was sent successfully, and the problem lies with them, not on the server-side. Equally, it helps debug SMTP errors.

 // try/catch block {

Mail::send('emails.any_view', $data, function($message) use ($user) {
    $message->from('server@example.com', 'PHP Code');
    $message->to($user->email);
    $message->subject($subject);
    
    // logging code here, e.g.
    /*
   If( Config::get('record_smtp') ) {
       $record = new MailRecord;
       $record->to          = $user->email; //(or id)
       $record->subject  = $subject;
       $record->body     = $message->getBody(); //(not sure if that's correct)
       $record->result    = 1; //(or 0 if it goes wrong)
       $record->smtp_transaction = $message->isThereAMethodToGetThis();
       $record->save();
   }
   */
});
Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

azcoppen azcoppen Joined 15 Mar 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.