As I continue to test and work with sure mail I needed a way to test success, blocked and failed attempts to work with a new plugin I am working on.
I solved this with a little testing snippet which is below which allows me to visit the site its on i.e this site https://techarticles.co.uk/?sn-test-email=1
would sent a test email that will actually get through but https://techarticles.co.uk/?sn-test-block=1
would get blocked by the header
<?php
// Send a quick test email through wp_mail()
function sn_send_test_email() {
$to = 'you@you.com'; // replace with your own email
$subject = 'Suremail Notify test';
$message = "This is a test email sent via wp_mail().\n\nIf you see this, wp_mail() worked, and Suremail Notify should have captured it.";
$headers = [ 'Content-Type: text/plain; charset=UTF-8' ];
$sent = wp_mail( $to, $subject, $message, $headers );
if ( $sent ) {
echo 'Test email sent successfully.';
} else {
echo 'Failed to send test email.';
}
}
// Run it once by visiting: https://yoursite.com/?sn-test-email=1
add_action( 'init', function() {
if ( isset($_GET['sn-test-email']) && current_user_can('manage_options') ) {
sn_send_test_email();
exit;
}
});
// Run it once by visiting: https://yoursite.com/?sn-test-block=1
add_action( 'init', function() {
if ( isset($_GET['sn-test-block']) && current_user_can('manage_options') ) {
sn_send_test_email_block();
exit;
}
});
// Run it once by visiting: https://yoursite.com/?sn-test-fail=1
add_action( 'init', function() {
if ( isset($_GET['sn-test-fail']) && current_user_can('manage_options') ) {
sn_send_test_email_fail();
exit;
}
});
function sn_send_test_email_block() {
$to = 'you@you.com'; // replace with your own email
$subject = 'Suremail Notify test';
$message = "This is a test to tell you ***Insert Threats to Kill and Swear words here*** sent via wp_mail().\n\nIf you see this, wp_mail() worked, and Suremail Notify should have captured it.";
$headers = [ 'Content-Type: text/plain; charset=UTF-8' ];
$sent = wp_mail( $to, $subject, $message, $headers );
if ( $sent ) {
echo 'Test email sent successfully.';
} else {
echo 'Failed to send test email.';
}
}
// Note for this one to work you need to create an SMTP connection or another which no longer actually works and set the broken@you.com header to be that accounts sending name so suremail actually uses that
function sn_send_test_email_fail() {
$to = 'you@you.com'; // replace with your own email
$subject = 'Suremail Notify test';
$message = "This is a test email sent via wp_mail().\n\nIf you see this, wp_mail() worked, and Suremail Notify should have captured it.";
$headers = [ 'Content-Type: text/plain; charset=UTF-8',
'From: Suremail Tester <broken@you.com>'
];
$sent = wp_mail( $to, $subject, $message, $headers );
if ( $sent ) {
echo 'Test email sent successfully.';
} else {
echo 'Failed to send test email.';
}
}
PHPWith the above I have been able to real-world test Sure Mail Notify rather than just relying on simulated tests.