Skip to content

Latest commit

 

History

History
56 lines (44 loc) · 1.26 KB

logging-outgoing-emails.md

File metadata and controls

56 lines (44 loc) · 1.26 KB
title version authors tags date added
Logging all outgoing e-mails
1.0.0
ryan
email
log
hook
ready
2023-03-14
2016-10-30

Problem

Lets say that you want to maintain a log of every email sent from your site.

Solution

This can be done quite easily by hooking the WireMail class. Place the following in your /site/ready.php file:

$wire->addHookAfter('WireMail::send', function($event) {
  $mail = $event->object;
  $event->wire('log')->save('sent-mail',
    "SENT ($event->return of " . count($mail->to) . "), " .
    "SUBJECT ($mail->subject), " .
    "TO (" .  implode(", ", $mail->to) . "), " .
    "FROM ($mail->from)"
  );
});

Test things out by sending an email. Here's an example of how I might send a message with the API:

$mail->new()
  ->to('test@processwire.com')
  ->from('ryan@test.com')
  ->subject('Hello world')
  ->body('How are you doing?')
  ->send();

In your admin, if you go to Setup > Logs > sent-mail, you should see a log entry like this:

SENT (1 of 1), SUBJECT (Hello world), TO (test@processwire.com), FROM (ryan@test.com)

Resources