Contents
How to set up SMTP email on wordpress. The thing is quite simple, especially if you use a plugin. In this text, we will show both ways, both through a plugin and through a regular PHP function, so which is easier for you.
What is needed?
In order to enable your site to send e-mails, for example from a contact form or any other, it is necessary to set up SMTP on your site.
In order for SMTP to be set up, you must first have an email address created.
Creating a business email address
The second step when you now have an email address is to install WP Mail SMTP and configure that plugin.
Installing the WP mail SMTP plugin
First, we enter our dashboard of our wordpress site. If you don't know how, here is a video tutorial.
Now when we go there we go to the plugin (Additions if it is in Serbian) then we add a new one and search for our plugin.

When we got there, type "wp mail smtp" in the search field, install and activate

Setting up WP Mail SMTP
During the installation, it takes us to some setup, we ignore it and go back.

Go to Settings as in the picture below.

Now we select Other SMTP. (gmail, yahoo, etc. cannot)

We set it up according to the instructions below:
- The SMTP host is mail.yourdomain.extension means you change it with your domain, but it must start with the word "mail" followed by a dot, then your domain, then a dot and extension. If your domain is marko.rs, then it goes to mail.marko.rs. Do not replace the word "mail" with your email address, it is the word as in the example below.
- SMTP port 587.
- TLS encryption
- Select Authentication to ON
- SMTP username is your email address that you created example@yourdomain (can't use gmail, yahoo...)
- The SMTP password is the password you entered when creating the email address primer@vasdomen
- you are not typing webhostingsrbija.rs but your data

Click Save and that's it. Is it correct that you have a Test option in Tools (under Settings).
Congratulations, you have successfully set up SMTP mail for your wordpress site.
Validity test:
Let's go to WP mail SMTP again, but don't go to settings, but to Tools
When you click on the test, you will get a field for entering your email address and click on the Send Email button as in the picture below:

After that, if it says some error, it means that you did not set it as above and go back to the settings and the beginning of this setting and set it correctly.
Error if it says: Could not authenticate
This means that your username (email) or password is incorrect.
_DMARC setup
Error sending email
When you go to the test and get that it has not been sent, it means that they did not enter something correctly. Below is how to see what the problem is.

When we go to the button View Full Error The log will show us how the mail works and then we can see what the problem is.

As we can see from this picture the most common reason Incorrect authentication data which means the password or email address is incorrect.
Also an error may appear among all these lines "Could not connect to host" or "Failed to connect to host" this means that the SMTP server is not set up properly. So go back up to the settings and set it right.
The second option, if you have set 1000% as in this guide, means that the MX record and DNS are not correct on that domain, and that needs to be set first.
Second option which is also possible is to use PHP code to create the same.
The first thing you need to do with this option is if you don't have and haven't activated a child theme, you have to do it right away. Creating a child theme can be found on this site as a guide.
After that, open functions.php in the child theme and copy the following code:
add_action( "phpmailer_init", "wpse8170_phpmailer_init" );
function wpse8170_phpmailer_init( PHPMailer $phpmailer ) {
$phpmailer->Host = "mail.vasdomen.nesto";
$phpmailer->Port = 587; // it can be different
$phpmailer->Username = "primer@vasdomen.nesto"; // your email address
$phpmailer->Password = "password"; // password is required
$phpmailer->SMTPAuth = true; // mandatory
$phpmailer->SMTPSecure = "true"; // this is important
$phpmailer->IsSMTP();
}
note that you can also set SSL for the same thing port 465 and ssl. Save the changes to functions.php from the child theme and you're done.
If you did this in the main theme functions.php after updating the theme, this change will disappear.
Gmail SMTP
To set it up via Gmail, you need to use the option where you create a special password for access. Instructions: https://support.google.com/accounts/answer/185833
Gmail SMTP via app password option.
First, we log in to our gmail account and access the > link https://myaccount.google.com/
A page with our options will open and we go to Security

Now that we have clicked we need to find the App Password option and click on it:

Now that we've clicked on that we need to choose what exactly we're creating a password for. Since we need an email from the first drop-down menu, we choose Mail and from the second Other, considering that we need it for the site.

Now when we did that and clicked on GENERATE we got a window like the picture below

These characters in orange is our password and you need to save it and set it in SMTP settings

Host: smtp.gmail.com
Port: 587
Encryption: TLS
Username: Email address
Password: Password from the orange square
That's completely everything. You now have an email ready.
The Less secure apps option no longer exists.
The instructions below can no longer be used as of 5/30/2022 because google turned off that option.
You need to approve the GMAIL setting
- The first step is to allow connecting outside of gmail: https://myaccount.google.com/lesssecureapps
- You may also need to remove the captcha, this takes 10 minutes, sometimes less or more depending on what you do: https://accounts.google.com/DisplayUnlockCaptcha
- If you use 2-step verification, then you will be offered to create a special password for gmail that you use in the plugin (it is another password for accessing gmail through the application, i.e. the plugin) https://support.google.com/accounts/answer/185833?hl=en
We will use the following data to set up gmail:
Host: smtp.gmail.com
Port: 587
Encryption: TLS
Username: Email address
Password: Password from gmail.
Connecting via the plugin we listed above:

What we set up is:
From email: your email gmail address
Mailer: We selected Other SMTP
SMTP host: smtp.gmail.com
Encryption: TLS
SMTP port: 587
Authentication: On
SMTP username: gmail email address
SMTP password: password from gmail
and click save settings.
Then we return to the top of the page and select "email test", enter the email address and click send email. If everything is fine then you will get a message that it has been sent and the email will look like this:

This completes the setup.
Possible error if this message appears during the test:
There was a problem while sending the test email.
Then you will receive the following email in the inbox of your gmail account:

This means that there is a setting on your account that you need to turn off.
Your setting is on https://myaccount.google.com/u/0/security?hl=en-GB
Then you find where it says

You need to click on the "Turn on access" link and you will see the only option to turn it on as in the picture:

If you still cannot send a test email, see the log on that same page by clicking on the last sentence, which is actually a link:

After that, you will be shown everything that happened in the background with sending:

As indicated in the first square in that text somewhere it should say Connection: opened, it means that the plugin has successfully connected to gmail.
In the second square it says Could not authenticate which actually means that you have not approved on your gmail account from the beginning of this part of the article.
That's all. Now go back to the test in wordpress and this will enable and you will receive the test email and the process is complete.