Created
September 5, 2025 08:27
-
-
Save apermo/d40c3f35e873520496fd383c29c65010 to your computer and use it in GitHub Desktop.
Simple Script to run and debug tests for smtp mail on WordPress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This is used to test and debug the Bedrock Environment Mailer | |
* https://github.com/hackur/bedrock-env-mailer | |
* | |
* Code is derived from it. | |
* License: MIT | |
*/ | |
use PHPMailer\PHPMailer\PHPMailer; | |
use function Env\env; | |
require_once __DIR__ . '/wp-config.php'; | |
if ( ! current_user_can( 'manage_options' ) ) { | |
// Redirect to home page if not accessed from allowed host | |
header( 'HTTP/1.1 302 Moved Permanently' ); | |
header( 'Location: /' ); | |
exit(); | |
} | |
$to = ! empty( $_POST['to'] ) ? $_POST['to'] : null; | |
$subject = ! empty( $_POST['subject'] ) ? $_POST['subject'] : 'Test Email from Bedrock Env Mailer Test Script'; | |
$message = ! empty( $_POST['message'] ) ? $_POST['message'] : 'This is a test email sent from the Bedrock Env Mailer plugin.'; | |
?> | |
<style> | |
i { color: grey; } | |
strong { color: green; } | |
strong i { color: red; } | |
</style> | |
<form method="post"> | |
<label>To: | |
<input type="text" name="to" value="<?php echo esc_attr( $to ); ?>"></label><br> | |
<label>Subject: | |
<input type="text" name="subject" value="<?php echo esc_attr( $subject ); ?>"></label><br> | |
<label>Message:<br> | |
<textarea name="message" rows="10" cols="50"><?php echo esc_attr( $message ); ?></textarea></label><br> | |
<button type="submit">Send Test Email</button> | |
<pre> | |
<?php | |
echo 'TEST ENV MAIL' . PHP_EOL; | |
// Display current SMTP settings from environment variables | |
echo 'WP_MAIL_SMTP_USERNAME: ' . ( env( 'WP_MAIL_SMTP_USERNAME' ) ?: '<i>Not Set</i>' ) . PHP_EOL; | |
echo 'WP_MAIL_SMTP_PASSWORD: ' . ( env( 'WP_MAIL_SMTP_PASSWORD' ) ? '<strong>Set</strong>' : '<strong><i>Not Set</i></strong>' ) . PHP_EOL; | |
echo 'WP_MAIL_SMTP_HOST: ' . ( env( 'WP_MAIL_SMTP_HOST' ) ?: '<i>Not Set</i>' ) . PHP_EOL; | |
echo 'WP_MAIL_SMTP_PORT: ' . ( env( 'WP_MAIL_SMTP_PORT' ) ?: '<i>Not Set</i>' ) . PHP_EOL; | |
echo 'WP_MAIL_SMTP_SECURE: ' . ( env( 'WP_MAIL_SMTP_SECURE' ) ?: '<i>Not Set</i>' ) . PHP_EOL; | |
echo 'WP_MAIL_SMTP_DEBUG: ' . ( env( 'WP_MAIL_SMTP_DEBUG' ) ?: '<i>Not Set</i>' ) . PHP_EOL; | |
add_action( | |
'wp_mail_failed', | |
function ( $wp_error ) { | |
echo 'wp_mail_failed: ' . print_r( $wp_error, true ) . PHP_EOL; | |
} | |
); | |
if ( $_SERVER['REQUEST_METHOD'] === 'POST' ) { | |
if ( empty( $to ) ) { | |
echo 'Please provide a recipient email address.' . PHP_EOL; | |
exit(); | |
} | |
if ( ! filter_var( $to, FILTER_VALIDATE_EMAIL ) ) { | |
echo 'Invalid email address format.' . PHP_EOL; | |
exit(); | |
} | |
$mail_sent = wp_mail( $to, $subject, $message ); | |
if ( $mail_sent ) { | |
echo 'Email sent successfully.' . PHP_EOL; | |
} else { | |
echo 'Failed to send email.' . PHP_EOL; | |
global $phpmailer; | |
if ( isset( $phpmailer ) ) { | |
echo 'PHPMailer ErrorInfo: ' . $phpmailer->ErrorInfo . PHP_EOL; | |
if ( ! empty( $phpmailer->smtp ) && method_exists( $phpmailer->smtp, 'getLastReply' ) ) { | |
echo 'SMTP Last Reply: ' . $phpmailer->smtp->getLastReply() . PHP_EOL; | |
} | |
if ( ! empty( $phpmailer->Mailer ) ) { | |
echo 'Mailer: ' . $phpmailer->Mailer . PHP_EOL; | |
} | |
if ( ! empty( $phpmailer->Host ) ) { | |
echo 'Host: ' . $phpmailer->Host . PHP_EOL; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment