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

So, you want to change all this now? I am confused. Just put the ‘use’ lines back in and test it again first.
If you use PHPMailer WITHOUT SMTP, you can do that, too. But, that is NOT what you asked help with.
And, it is NOT a spf-record issue. Not in my opinion. It is just a library setup problem which I have working now on my site. If you are getting a mydomain.com error, it is because you did not set up the rest of the test code to work with SMTP. At the beginning of all this you said you needed code to run using SMTP.

Well, do you wish to now change to POP3 instead of SMTP? Either will work once you put the use lines in… Let me know, but, I am heading to bed fairly soon, so not long for me to quit for the nite…

To test email without SMTP, you would remove the references to it in the use and include lines.
Then, rewrite the email send code to use POP3 instead of SMTP. That is minor. Let me know which
you want done at this point. I assumed you were trying to do it with SMTP from the beginning…
( Might be my error… )

I have no idea which one will work Ernie. I only posted initially about SMTP cuz that’s what github was talking about. and I already have tried the code with the “use” lines in it and I gave you the error. the file will error out either way…WITH double quotes around the file paths and without the double quotes. doesn’t matter. in my server’s view, the “use” lines are apparently a problem.

Are you testing in just one file? If so, you could send it to me in a private message and I can put it on my server and see where it is failing. I would just dummy out the actual sending of emails to make sure it does not send any. My Godaddy server does not throw an error on the ‘use’ lines. What was the error it is throwing on the ‘use’ line?

pretty strange crap my friend. if I include all 3 “use” lines WITHOUT double quotes around the file paths, I now get a different error than I did before (before I was getting a parse error on the first “use” line. Now I get:

2019-11-25 05:54:42 SMTP ERROR: Failed to connect to server: php_network_getaddresses: getaddrinfo failed: Name or service not known (0)

SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

Message could not be sent. Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

That actually means that the library is working correctly now.

This version using SMTP needs to have SMTP set up on your server. Have you done that yet?
This would be in the Godaddy’s control panel under the Email section. There would be an option to
use POP3 or SMTP. I suspect it is not set to SMTP.

Or, you did not set up the correct SMTP calling codes. Which port are you set to? Here is the full
code to send a SMTP email. Note you must change the host, user, password as you have set up
in the control panel under the email section on Godaddy. You will also need to put in your email
addresses and names in the to, from, etc…

//Create a new PHPMailer instance
$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
try {
    //Server settings
    $mail->SMTPDebug = 2;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = gethostbyname('mail.domain.com'); // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = '[email protected]';                 // SMTP username
    $mail->Password = 'Password123#';                           // SMTP password
    $mail->SMTPSecure = 'ssl';                            // Enable TLS encryption, `ssl` also accepted
    $mail->Port = 465;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('[email protected]', 'Alex');
    $mail->addAddress('[email protected]', 'John');     // Add a recipient
    //$mail->addAddress('[email protected]');               // Name is optional
    $mail->addReplyTo('[email protected]', 'Test');
    //$mail->addCC('[email protected]');
    //$mail->addBCC('[email protected]');

    //Attachments
    //$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    //$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Here is the subject';
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';
    $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo '<br><br>Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}

Otherwise, we can change the sample code you are using to just use POP3. It is a little easier that
way, but, some say not as secure as SMTP. I have used SMTP on one server, although I can not locate
the old code for it yet. POP3 is a few less lines.

where you have:

$mail->Host = gethostbyname('mail.domain.com');

i put:

$mail->Host = gethostbyname('mail.MyDomainName.com');

I used port 465 like you have in your code. but I use port 21 to connect to my server using filezilla. does that mean i need port 21? putting in a port in FZ is optional, it connects to my server regardless of whether i spec a port or not. I do not know if I have smtp enabled on godaddy, but I will check. When I ran your code modified like I mentioned above in this post, I get these errors:

2019-11-25 18:01:00 SMTP ERROR: Failed to connect to server: (0)
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Message could not be sent. Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

HUH? Your host name is your server name. So, if you have a domain named, Ernie.com, that would make it mail.Ernie.comdomain.com is just a dummy where you put YOUR domain name in.

Usually, 465 is the port number. There are 65,000 ports in every computer. 21 is for FTP. 80 is for HTML.
etc… Godaddy has it’s own port, I think 465 is correct, but, can look it up if it fails.
Also, you need to set up a SMTP username and password for the SSL connection and enter those, too.
I am thinking you may not understand servers usage of SMTP. Perhaps we should skip to just POP3
which needs less security set up in the control panel.

you say:

“So, if you have a domain named, Ernie.com, that would make it mail.Ernie.comdomain.com is just a dummy where you put YOUR domain name in.”

that’s what I did! in my code I say “mail.MyDomainName.com”, and “MyDomainName” indicates that I put my actual domain name in there when I ran my code. so it’s all good. in the SMTP username and password lines, I put my credentials for my webmail account at godaddy. I assume that’s a mistake? I put in:

    $mail->Username = '[email protected]';                 // SMTP username
    $mail->Password = 'MyPassword';                           // SMTP password

the other thing that might make a difference is that the employer’s domain does NOT have an SSL certificate! is that the problem!?

No, you do not need an SSL cert. That is something else.
What is the current SMTP error you are getting. It should have a number like #220 or #500, etc…

this is the error I get. It has no so number:

2019-11-25 18:01:00 SMTP ERROR: Failed to connect to server: (0)

also, godaddy says that port 465, which is what i’m using, IS a secure SMTP port and that it IS open. so no issue there.

Okay, I did some research since you appear to want to stick with SMTP instead of POP3.
I found one person who said you need to use the localhost instead of your mail.host.com.
They said to use:
$mail->isSMTP();
$mail->Host = ‘localhost’;
$mail->Port = 25;

In another place, I found this for the setup. Note that the host is the actual mail server that Godaddy gives you in the control panel. In the control panel there is a connect example for emails and in there, you will find the correct version of “secureserver” address that is where your mail server is set to.
$mail->isSMTP();
$mail->SMTPDebug = 0; // for production use, but set to 3 for debugging
$mail->Host = ‘n3plcpnl0012.prod.ams3.secureserver.net’; //my specific shared server
$mail->Port = 25;
$mail->SMTPAuth = false;
$mail->SMTPSecure = ’ ';
$mail->Username = ’ ';
$mail->Password = ’ ';

    $mail->SMTPAutoTLS = false; // Prevents opportunistic TLS encryption

Since you are not even connecting to the mail server (not your hosting web server), I am guessing that you have the incorrect host listed. In the second example, they say not to set the secure,user,pass options. Set them to nothing… ‘’ They said it was due to this:

The thing that got it working for me is setting SMTPAutoTLS to false (final line of code), as the shared servers do not have a valid SSL/TLS certificate and so are rejected by the mail server.

I would say try both of these and if not working, you would need to talk to Godaddy as something must be wrong with your SMTP setup.

Okay, I spent some time at Godaddy for you. In my control panel, I went into the email list which shows all your email accounts. Once there, on the right side it has a button for “setup email center”. Go there and it asks you which account and how you want to configure it. Select “other” instead of Outlook. Then, click next and it will show you your email hosting account. Somehow mine is “smtpout.asia.secureserver.net
which is odd as my server must be in Asia… Don’t understand why… Anyway, as you see, my Godaddy
SMTP server is clearly marked and I would need to put that into my PHPMailer code if I used SMTP.

Hope that helps!

1 Like

I will try both of the code updates you gave, and I will look in the godaddy email hosting account settings and see what the smtp server is that’s listed, then I will try the script again with that in the "$mail->Host = " line. Give me about 30-60 minutes. i’ll get back to you. thanks for all of this so far! I hope my ignorance hasn’t ticked you off!

Never get ticked off while helping a fellow programmer solve a problem.
I am sure you are close to a solution. Just really think it is the connection to the mail server…
We will solve it. Take your time.

ok, here are the result from the 3 attempts I made:

attempt #1:

code used:

ini_set(“include_path”, “PHPMailer”);
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require “…/PHPMailer/src/PHPMailer.php”;
require “…/PHPMailer/src/SMTP.php”;
require “…/PHPMailer/src/Exception.php”;

//Create a new PHPMailer instance
$mail = new PHPMailer(true); // Passing true enables exceptions
try {
//Server settings
$mail->SMTPDebug = 0; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = ‘smtpout.secureserver.net’; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = ‘[email protected]’; // SMTP username
$mail->Password = ‘password’; // SMTP password
$mail->SMTPSecure = ‘ssl’; // Enable TLS encryption, ssl also accepted
$mail->Port = 25; // TCP port to connect to

//Recipients
$mail->setFrom('[email protected]', 'Corporate');
$mail->addAddress('[email protected]', 'Adam');     // Add a recipient
$mail->addReplyTo('[email protected]', 'Adam');

//Content
$mail->isHTML(true);                                  // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

$mail->send();
echo 'Message has been sent';

} catch (Exception $e) {
echo '

Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}

the above resulted in this error:

Message could not be sent. Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

attempt #2)

code used:

ini_set(“include_path”, “PHPMailer”);
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require “…/PHPMailer/src/PHPMailer.php”;
require “…/PHPMailer/src/SMTP.php”;
require “…/PHPMailer/src/Exception.php”;

//Create a new PHPMailer instance
$mail = new PHPMailer(true); // Passing true enables exceptions
try {
//Server settings
$mail->SMTPDebug = 0; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = ‘p3plcpnl0398.prod.phx3.secureserver.net’; // Specify main and backup SMTP servers
$mail->SMTPAuth = false; // Enable SMTP authentication
$mail->Username = ‘’; // SMTP username
$mail->Password = ‘’; // SMTP password
$mail->SMTPSecure = ‘’; // Enable TLS encryption, ssl also accepted
$mail->Port = 25; // TCP port to connect to
$mail->SMTPAutoTLS = false; // Prevents opportunistic TLS encryption

//Recipients
$mail->setFrom('[email protected]', 'Corporate');
$mail->addAddress('[email protected]', 'Adam');     // Add a recipient
$mail->addReplyTo('[email protected]', 'Adam');

//Content
$mail->isHTML(true);                                  // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

$mail->send();
echo 'Message has been sent';

} catch (Exception $e) {
echo '

Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}

the result of attempt #2:

*Message has been sent*

And I got the email in the gmail inbox with all specs that are in my script! I didn’t even try attempt #3 cuz this worked! Hooray! Can you believe it took this long!?

Wow! Great and bittersweet as my Grandma would have said! Great cuz it is working! Bad cuz it took so long. But, very glad we BOTH stuck with it. Now you can go back and adjust it for future use. What I mean, is that you can make it into a PHP file by itself and call it sendemail.php or something and then
pass data to that file as needed and you can use it in all of your pages you need to send emails from.

Very nice! See you in your next post… ( CYA in the bitstream, cuz that is all this really is… Ha! )

Ive already have my form page script set up and the post operation code and js redirects in place and it works fine. Now i will add this to it! Btw is there anyway to officially thank u for this or mark this thread as solved?

On the previous version of this site, you could give a poster “Karma” and I had thousands of them!
But, on this version, you can click on the heart and it marks it in my list of “likes”. Not the same really.
I stopped being on this site for awhile after they switched to this version. I found it more “canned” and
did not have as many great features as the last one. But, I came back… Ha!

Your thanks are appreciated… Post another issue and I and others will help you out! Glad it is solved!

I talked to an admin on www.phpfreaks.com and he said they did the same thing over there years ago and he didn’t like it either. at any rate, I marked this thread solved. I’m sure we’ll talk again!

Sponsor our Newsletter | Privacy Policy | Terms of Service