Time to Test That Everything Works
To ensure everything is working correctly, you’ll want to add a testing mechanism. Add the following code to your theme’s functions.php file:
<?php
add_action('wp_footer', 'add_test_email_link');
function add_test_email_link() {
if (current_user_can('manage_options')) {
echo '<div style="position:fixed; bottom:20px; right:20px;">';
echo '<a href="' . add_query_arg('send_test_email', '1') . '"
style="background:#007cba; color:white; padding:10px; text-decoration:none; border-radius:5px;">
Send Test Email</a>';
echo '</div>';
}
}
add_action('init', 'handle_test_email');
function handle_test_email() {
if (
isset($_GET['send_test_email']) &&
current_user_can('manage_options')
) {
wp_mail('[email protected]', 'Test Email', 'This is a test email from WordPress');
wp_redirect(remove_query_arg('send_test_email'));
exit;
}
}
This code creates a convenient testing button that appears in the bottom-right corner of your website, but only when you’re logged in as an administrator. When you click the button, it sends a test email to the address specified in the code (make sure to replace ‘[email protected]’ with your actual email address).
How to Test
- Log into your WordPress admin panel, as an administrator.
- Visit any page on your website.
- On the bottom, you will find “Send Test Email” button.
- Click the button to send a test email.
- Check your inbox for the test message.
Important: Cleaning Up After Testing
Once you’ve confirmed that your SMTP configuration is working correctly, you should remove the testing code from your functions.php file. This means deleting both the add_test_email_link and handle_test_email functions. This step is important for security and keeping your code clean. You can keep the SMTP configuration in wp-config.php as it’s needed for ongoing email functionality.
Now your WordPress site is configured to send emails through Google’s SMTP relay service without the need for any plugins!