WordPress users are no longer unfamiliar with the SMTP email sending function, Code to send SMTP email from Gmail right? Normally, WordPress needs an email server to send content from the website to the admin email address, or to the email address of the recipient of the notification, for example sending the user’s contact form to the admin. website, email confirmation of password change…etc.
Code to send SMTP email from Gmail for WordPress
Code gửi email SMTP
Copy the code below and paste it into the functions.php file of the theme you are using.
- // tu dong gui email
- add_action( ‘phpmailer_init’, function( $phpmailer ) {
- if ( !is_object( $phpmailer ) )
- $phpmailer = (object) $phpmailer;
- $phpmailer->Mailer = ‘smtp’;
- $phpmailer->Host = ‘smtp.gmail.com’;
- $phpmailer->SMTPAuth = 1;
- $phpmailer->Port = 587;
- $phpmailer->Username = ‘Account’;
- $phpmailer->Password = ‘Application Password’;
- $phpmailer->SMTPSecure = ‘TLS’;
- $phpmailer->From = ‘Send email address’;
- $phpmailer->FromName = ‘Sender Name’;
- });
- // set the user’s email address
Notice to change the following contents:
‘Account’ replace with your gmail account.
‘App password’ how to get the app password see below.
‘Send email address’ replaces the email address to send in.
‘Sender Name’ replace the name you want to enter.
How to get app password
Click on the link below then log into your Gmail account.
https://myaccount.google.com/u/1/security

Select 2-Step Verification.

Select Confirm code via text message , after confirming and enabling 2-layer security, return to the account security section.

Continue to select the item App password.

Select Other app (Optional Name).

Name the application, you can give it whatever you want.

Well, finally, our application password is displayed. Copy this app password and paste it in the code above.
Note: With this method, the mail sent will never be in the spam folder. However, Gmail limits us to only sending a maximum of 100 messages per day and if we exceed the number of messages, we will send them 24 hours later.