Configuring SMTP for sending mail in WordPress is a common requirement, allowing you to customize mailing from your site instead of using the default WordPress settings. In this article, we are sharing how to send SMTP email from WordPress without a plugin. Copy the code and paste it into functions.php
; your website will then be able to send emails effortlessly. This method eliminates the need for any additional plugins.
Sharing Code Send SMTP Email from WordPress Without Plugin
SMTP (Simple Mail Transfer Protocol) is a popular protocol used to send email on the internet. When you configure SMTP in WordPress, it will use this protocol to send email from your website through a valid email server. The email server here we will use is gmail SMTP, and through this method you will not need to install any additional plugins related to sending mail in Wordpress.
Step 1. Determine Gmail SMTP Configuration Information
To send SMTP email from WordPress without a plugin using Gmail as the SMTP server, you need to have the SMTP configuration information for Gmail, which includes:
- SMTP Domain: smtp.gmail.com.
- SMTP port: SSL port is 465 or TLS port is 587.
- Secure protocol: With port 465, use SSL. With port 587, use TLS.
- Email account: Your Gmail email address.
- Email Password: The Gmail account SMTP client password.
Step 2. Create a Gmail App Password to Use SMTP
To create an app and password on Google you can follow these steps:
- Sign in to your Google account
- Enable 2-step verification under “Security”.
- Under “Sign in to Google,” select “Two-step verification.”
- Scroll to the bottom of the page and choose “App passwords.”
- Select “Other (Custom name)” and name your application.
- Select “Other (Custom name)” and give your app a name.
- Click “ Generate ” to generate the application.
- Copy and save the password and move on to the next step to configure SMTP to send Mail in Wordpress without using Plugins.
Step 3. Add SMTP configuration code to functions.php
There are several methods to add a PHP script to a WordPress site and make it functional, but the most common and straightforward way is to add it to the functions.php file. You can edit the provided configuration below with your application account and password, and then add it at the end of the functions.php file.
add_action( 'phpmailer_init', function( $phpmailer ) {
if ( !is_object( $phpmailer ) )
$phpmailer = (object) $phpmailer;
$phpmailer->Mailer = 'smtp';
$phpmailer->Host = 'smtp.gmail.com';
$phpmailer->SMTPAuth = true;
$phpmailer->Port = 587;
$phpmailer->Username = 'arriveddev.bz@gmail.com';
$phpmailer->Password = '16 character application password';
$phpmailer->SMTPSecure = 'TLS';
$phpmailer->From = 'arriveddev.bz@gmail.com';
$phpmailer->FromName = 'We';
});
Note: If you choose to send SMTP email from WordPress without a plugin, it is advisable to deactivate any plugins that allow similar email functionalities. Avoiding the use of plugins can help streamline your site by eliminating unnecessary code or other email functions.
Step 4. Test sending email in WordPress
After completing the steps as instructed, you can now test the email functionality in your WordPress by using any relevant features such as user registration, placing orders, subscribing, contacting, etc. Additionally, in your WordPress, issues with delayed email notifications for your order/payment functionalities may arise due to the default cron function. You can check how to fix slow payment/order issues in WordPress here.