Skip to content

Instantly share code, notes, and snippets.

@joychetry
Created July 30, 2024 05:47
Show Gist options
  • Save joychetry/0adb50509a16adefd967e3a4e61e319f to your computer and use it in GitHub Desktop.
Save joychetry/0adb50509a16adefd967e3a4e61e319f to your computer and use it in GitHub Desktop.
SMTP Test in WordPress Dashboard
add_action('admin_menu', 'smtp_test_email_menu');
function smtp_test_email_menu() {
add_menu_page(
'SMTP Test Email', // Page title
'SMTP Test Email', // Menu title
'manage_options', // Capability
'smtp-test-email', // Menu slug
'smtp_test_email_page' // Callback function
);
}
function smtp_test_email_page() {
?>
<div class="wrap">
<h1>Send Test Email</h1>
<?php
if (isset($_POST['send_test_email'])) {
$to = sanitize_email($_POST['to_email']);
$subject = 'SMTP Configuration Test';
$message = 'Hello,
This is a test email to confirm that your WordPress site is configured correctly to send emails using Gmail SMTP.
Best regards,
Your WordPress Site';
if (wp_mail($to, $subject, $message)) {
echo '<div class="notice notice-success"><p>Email sent successfully!</p></div>';
} else {
echo '<div class="notice notice-error"><p>Failed to send email.</p></div>';
}
}
?>
<form method="post">
<table class="form-table">
<tr valign="top">
<th scope="row">Recipient Email</th>
<td><input type="email" name="to_email" required /></td>
</tr>
</table>
<?php submit_button('Send Test Email', 'primary', 'send_test_email'); ?>
</form>
</div>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment