Setting SMTP in php.ini to make mail() function work

i cannot find an answer for this on google. according to this thread: https://stackoverflow.com/questions/8803994/php-mail-not-working-for-some-reason, mail() doesn’t work when smtp is not configured properly. I checked my server files and I do not have a .ini file. am I spose to have one by default? and, according to this question in the godaddy community: https://www.godaddy.com/community/Using-WordPress/PHP-ini-how-to-make-changes-using-cPanel/td-p/119704, configuration of PHP in general should be done in the .user.ini file instead of in php.ini. in the attached code, which is what I have now, it results in no mail being sent to my address so either my code is wrong or my configs are not set properly I’m guessing.

also, in the stackoverflow article I mentioned above, one of the responders suggests using PHPMailer as a better way to send mail using PHP. Is that true? I’ve always used mail() in the past and I’ve never been let down in terms of a message not being sent. any help greatly appreciated. thanks!

Yes a library like PHPMailer is ALWAYS better than using the mail function.

You have to have an ini file, but if you are on shared hosting, you won’t have access to it. However, shared hosting will have the stmp configuration there, it has too.

The mail function is a known issue, just because you aren’t receiving anything does not mean that it isn’t configured. Aside from there possibly being a typo; It could be in your spam or junk folder; or most likely, your ISP or mail client could be rejecting it as spam.

PHP has it’s own mailer system. It can use the general “send mail” system on the server or can be set up to use SMTP if needed. Some server require SMTP so that it is fully authenticated. In my humble experience it is not needed on most servers I have access to. Mail “libraries” are more complete or robust code systems that have many extra features that are sometimes needed to process mail. Two of my favorites are PHPmailer and SwiftMail. Both work well and both have options to set up SMTP if needed.

Now with that said, there are many ways to set up SMTP, but the standard PHP mail function can not do this for you. You need to go into your control panel and set up SMTP inside your mail options. This is 100% dependent on your server set up. Some are complicated to set up. Some shared servers do not allow it. Also some shared servers do not let you directly access your PHP.ini file. This also has nothing to do with WordPress since that is a website platform and does not control the mail system.

Both PHPmailer and SwiftMail are free and easy to download and install. Both have very simple setup to use SMTP. It is just a couple extra lines to use it versus regular php mail() functions. I personally have access to many different GoDaddy servers. I have got the email system to work on all of them. But, in two of them, I needed to use SMTP and ended up using a library which made it much easier to do.

Now, I have found that Godaddy servers need one thing handled to get mail to send out. You MUST put the FROM set to your domain’s email account. You MUST be telling the mail system that the mail came from your server. If you do not do that, Godaddy’s email system will not send the email. Something to do with spammers and security.

But, my suggestion is to use a library. You can get PHPmailer at Github here: PHPMailer
Just download the zipped file. Expand it. Copy the folder to your server. ( I usually put it in a root folder called “lib” so that all domains on the server can access it. ) Then, you can set up the SMTP in the library’s config and it should work for you. You will need to learn how to call the library.

Well, hope this helps. Sorry for the long post!

Ernie,

I downloaded the zip from github and they recommend using composer to install it. I downloaded composer’s .exe from here: https://getcomposer.org/download/, and tried to run it on my local machine but it searches for another .exe apparently? see my image. obviously I dont know what I’m doing, but if u can guide me a little through this installation thing I will do the research I need to do to learn how to call the libraries and send the mail. the composer webpage says “Download the installer to the current directory”. I assume this means the root dir of my website? I do have it there. thanks.

no%20exe

You don’t need to install it locally, you need to install it on your server. Do you have ssh access to the godaddy account?

I do not use composer to install it. I just unzip it and store it on the server.
You can use this link to see how SMTP is actually used with the library. It is commented so you can easily understand each option. Not hard at all to understand. But, please ask here if you get stuck.
PHPMailer SMTP Example

I’ve never heard of secure shell before, and certainly never used it. what r u asking me this aston? is that related to it’s configuration? I will check with godaddy to see if I have access, but still…explain to me why ur asking? I will take a look at the link Ernie and get back to u if I get stuck. thanks guys.

Well, you need either FTP to move the files to your server OR use SSH to upload the files to your server.
Either works, but, I find FTP is easier. You need to put the PHPMailer folder onto your server.
I usually create a folder on the server called “lib” and then put the PHPMailer folder in that.
Then, in the mail code, you steer that to lib/PHPMailer/ folder to access the library.
The rest is explained in that example link I posted.

Let us know how you do with it.

Ernie,

I use filezilla as my FTP application so I tranferred all the files to the server just fine. but where am I spose to put that code in the SMTP example? obviously it goes in a separate php file that goes on the server right? but where? the following lines of code I don’t understand. the dirs and files in these lines of code don’t exist in the PHPMailer directory trees:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;

require '../vendor/autoload.php';

help?

Well, this again depends on what you did. You uploaded the file after unzipping it. Where on your serer did you place the php mailer forlder? If you place it in your root directory, the code should work.
But, then again, as you see it says look into one folder named “PHPMailer” and then inside that folder, it looks inside a second sub folder named “PHPMailer” to get the library info.
That is not how my version is. So, I downloaded the latest version. It for some odd reason has been changed. So, the PHPMailer file is inside “PHPMailer-master\src\PHPMailer” NOT the “PHPMailer\PHPMailer\PHPMailer” Very odd.
I think you need to use “PHPMailer-master\src\PHPMailer” And, the same for SMTP: “PHPMailer-master\src\SMTP”. Lastly, the vendor autoload.php file is not in the library any more. So, remove that line.
If that works, let us know. If not, I can upload this newer version to my test server and get it working for you.

Ernie,

I already have researched this, but due to the fact that I am on a shared hosting server, I do not have access to my configuration file (the “include_path” config file). that’s a problem isn’t it? the instructions on git hub say I have to copy those PHP code lines (I mean the alternative ones you supplied me in ur previous post) into that file:

You don’t need to alter include paths for your server. A path is a pointer to steer code to the correct folder. That is what I explained in the previous post. Change the pointers in the mail example you are using to point to the two folders I mentioned. It should work for you.

I am using a shared server and have no problems using this library. Actually, I have it on three different shared servers, one in Australia. All work fine. But, two of them I had to tweak the pointers to the correct folders.

What that posted comment means is to copy your PHPMailer folder onto a path in your server that you can access. That is what I said. Copy the folder to your server. Change the pointers in the two use lines to point to where the files are. In the downloaded version, it is in the “src” folder. Then, test it and let us know any further errors. Does this make sense to you? Don’t overthink what you read on their site.
If you can not get it to work, post your email test page and we can help you sort it out. Do not show us your SMPT passwords, etc…

I’ve already talked to godaddy as well and they were able to provide a little guidance too. I’ll try everything I know of and what you just explained and I’ll get back to you soon here. thanks.

Godaddy is not good helping with this type of thing. Take my word for it.
Actually this is very simple to do and I can help you solve it. I will put up a test version of it on my Godaddy server and post an example. But, you you try, let me know what errors you get.

By the way, on godaddy’s sites, some of the errors show up in the folder as a file named “error.txt” or similar file after you run a script. So, sometimes you need to check your folder where you are calling the mail page to see if that file shows up. Let us know how it works for you…

ok ernie, here’s an update for you > I tried to run the code under the heading " A Simple Example" on this github page: PHPMailer/README.md at master · PHPMailer/PHPMailer · GitHub. The Eclipse IDE threw a syntax error on the use() statements:

and when I tried to run the page on the server, it also threw an exception:

I also tried the use() statement without “…” in front by putting the PHPmailer dir in the same dir as the test.php file I’m running it on and I still got a syntax error in eclipse and an exception through the server.

Okay, step by step we will solve this one…
First, you have a root folder which is your server.
In that you have a folder for your testing or live web code which is where you test file is.
What is it’s name?

Next, in your root folder you have a folder named PHPMailer-master, correct?
( I might have just named that folder PHPMailer to make it easier to read and use. )

Then, inside your first folder where your code is you have a file named test.php. Correct?
( OR, is it inside your root folder? Normally, you do not place tests in your root folder so it is more secure.)

So, if the test.php file is inside the root folder, you would tell it to “use PHPMailer\src\PHPMailer;”
But, if you did not rename the library folder, it would be “use PHPMailer-master\src\PHPMailer;”

Now, if the test.php file is inside another folder, like “tests” or “mail-tests” or whatever, then you would
need to back up one level using the “…” at the beginning. So, perhaps you need to explain hour server’s
folder structure. This is what I am assuming:

ROOT on Server
… PHPMailer (folder on Server)
… All of the mailer stuff including the src folder
… Tests (folder on Server)
… test.php (file inside of Tests folder)
In this case, the test.php file point to the Tests folder. To move back one, you would need to use the
…\PHPMailer address to get to the root and then to the PHPMailer folder.
If the test.php file is in the root folder, it does not need the …\ and needs just PHPMailer to get you to the library stuff.

Does that make sense?

One more thing I just thought of. Sometimes on some servers, you need to use quotes around the files you are using. Such as use “PHPMailer\src\PHPMailer”;

Not sure, but you might need that…

Here is my directory tree (the top line being my ROOT dir) Ernie (indentation indicates a subdir):

ROOT dir

PHPMailer-master

All of the mailer stuff including the src folder

and my test.php file is in the following place:
ROOT dir

php

php.test

So, test.php is inside the folder php. Therefore use this to access the library…
use “…\PHPMailer-master\src\PHPMailer”;

Basically, what this says is to move up one level (from php folder to root folder) then go into the PHPMailer-master folder and then src folder then use the PHPMailer file. Hope that makes sense…

Sponsor our Newsletter | Privacy Policy | Terms of Service