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

Well, the guys played extremely late and I have been a bit busy today. Have a great Thanksgiving day and dinner! I will check in later on. But, my thought was to have you give me access to your server so I can directly run some tests and see how your mail server is set up. I do not understand why it fails on yours and several Godaddy servers that I have similar code on works just fine. I did look at one and that one I had set up SwiftMail as it seemed to work well for them. That was the one in Australia.

It you want to do that, private message me and we can discuss it further. Planning on watching some football today. I run a football pool on one Godaddy server. It uses standard PHP Mail() functions and does not use SMTP. This also works very well for this site. We could just use regular mail() functions if you wish. Either way is good for me…

I sent you a PM. and no, using mail() is what started this thread to begin with. remember? it is getting spammed out by gmail. that’s why we’re implementing this. if i had to go back to using maik(), that would mean we’ve wasted 3 whole days with this stuff.

ok ernie, here is the code I ran in the test.php file:

<?php

error_reporting(E_ALL); // TEMP ! ! ! ! ! ! ! ! ! ! !
ini_set(“display_errors”, 1); // TEMP ! ! ! ! ! ! ! ! ! ! !

// Import PHPMailer classes into the global namespace
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”;

//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don’t have access to that
date_default_timezone_set(“America/New_York”);

//Create a new PHPMailer instance
$mail = new PHPMailer;
// Set PHPMailer to use the sendmail transport
$mail->isSendmail();
//Set who the message is to be sent from
$mail->setFrom(“[email protected]”, “Business Name Corporate”);
//Set an alternative reply-to address
$mail->addReplyTo(“[email protected]”, “Business Name Corporate”);
//Set who the message is to be sent to
$mail->addAddress(“[email protected]”, “Adam Evanovich”);
//Set the subject line
$mail->Subject = “PHPMailer sendmail test”;
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents(“contents.html”), DIR);
//Replace the plain text body with one created manually
$mail->AltBody = “This is a plain-text message body”;
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: ". $mail->ErrorInfo;
} else {
echo “Message sent!”;
}
?>

and it worked just fine. i put the HTML markup test in a separate file called contents.html and put it in the /php directory, cuz obviously the code is pointing to it. I got the following message on “test.php” after running the script:

Notice: Use of undefined constant DIR - assumed 'DIR' in /home/ownersName/public_html/DOMAIN/php/test.php on line 36
Message sent!

so what is the argument DIR?? am I spose to include the absolute path of “contents.html” in there? or just surround it in quotes? that’s what it seems like the message is saying…the message did indeed appear in gmail’s inbox and was not spammed out. so why exactly did this one work!? I’m a little stumped in that regard. here is an image of what I saw in gmail’s inbox:

What do you mean by "It is getting SPAMMED out by gmail???
Do you mean it is going into your spam filter? That happens with a lot of emails if the email looks like spam.
You just need to mark it as NOT spam. So does that mean it is actually sending out mail and then your
email client, Gmail, is doing something wrong? That would mean it is nothing to do with your server, but,
a problem with Gmail which can be fixed by just putting your server on the Gmail Whitelist…

I would do the HTML template part like this:

$template = file_get_contents(“contents.html”);
$mail->msgHTML($template);

The “DIR” part is supposed to have a couple underlines before and after it to point at the current dir.
like __DIR__ But, it is NOT even needed.

Ernie, you just said:

What do you mean by "It is getting SPAMMED out by gmail???
Do you mean it is going into your spam filter? That happens with a lot of emails if the email looks like spam.

based on what i’ve just quoted you, your words from your last post, ur telling me that I once again said gmail was sending the message to the spam folder. NO it isn’t! it worked! my question was: why did your latest code snippet get thru to gmail’s inbox when all your previous scripts did not? is it because we are including the HTML content in a separate HTML file and pointing to it with the PHP code?

No, the old code was for SMTP. This version is NOT. This version is the plain type of emails.
I highly doubt it was “spammed out”. Most likely the other code just was not sending the email out.

But, it appears this version is working for you. Nice!

well I spose we should consider this thread closed then? So why would I use SMTP over this current method that works? just for the feature of SMTP authentication?

sorry, not been online much till now…

Yep! Don’t fix it if it’s not broke… There are reasons to use SMTP, but, really online if you need to
send mail from other sites. As far as my humble opinion goes. Since the email can only be sent from
your server, it is safe to use without the SMTP. I think you are all set. See you in your next puzzle…

I will test it thoroughly Ernie, and if I have any troubles I will get back to you here via PM or by starting another thread. thanks for all the time you dedicated!

Ernie,

It works fine, however what I have noticed is that if multiple messages are submitted at the same time by different site visitors, or even if the forms are submitted within like 30 seconds - 1 minute apart, the mail is not always getting sent. is that normal for PHPMailer? Regardless though, here is the final code I used:

error_reporting(E_ALL); // TEMP ! ! ! ! ! ! ! ! ! ! !
ini_set(“display_errors”, 1); // TEMP ! ! ! ! ! ! ! ! ! ! !

//get form data submitted
$firstname = $_POST[‘firstname’];
$lastname = $_POST[‘lastname’];
$email = $_POST[‘email’];
$phone = $_POST[‘phone’];
$products_interested_in = “”;
$message = $_POST[‘message’];
$errors = ‘’;

//gather error message is form data incomplete
if(empty($_POST[‘firstname’]) ||
empty($_POST[‘lastname’]) ||
empty($_POST[‘email’]) ||
empty($_POST[‘phone’]))
{

$errors .= "Error: Please fill in all required fields.";
exit($errors);

}

if(empty($_POST[‘message’]))
{
$message = “No message provided”;
}

//check for valid email address
if (!preg_match(
“/^[_a-z0-9-]+(.[_a-z0-9-]+)@[a-z0-9-]+(.[a-z0-9-]+)(.[a-z]{2,})$/i”, $email))

{
$errors .= “Error: You’ve entered an invalid email address. Please try again.”;
exit($errors);
}

//gather info on products interested in
if(isset($_POST[‘checkboxes’])) {
foreach($_POST[‘checkboxes’] as $selected){
$products_interested_in .= ", " . $selected;
}
$products_interested_in = substr($products_interested_in, 1, strlen($products_interested_in) - 1);
} else {
$products_interested_in = “No Products Were Indicateed by the Sender.”;
}

//put the body of the email together
$email_body = “A visitor to your website has sent you their contact information:”

"<strong>Name:</strong> " . $firstname . " " . $lastname
"<strong>Email:</strong> ". $email
"<strong>Phone:</strong> " . $phone
"<strong>Products Interested In:</strong> " . $products_interested_in
"<strong>Message:</strong> " . $message;

// Import PHPMailer classes into the global namespace
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”;

//SMTP needs accurate times, and the PHP time zone MUST be set
//This should be done in your php.ini, but this is how to do it if you don’t have access to that
date_default_timezone_set(“America/Chicago”);

//Create a new PHPMailer instance
$mail = new PHPMailer;
// Set PHPMailer to use the sendmail transport
$mail->isSendmail();
//Set who the message is to be sent from
$mail->setFrom("[email protected]", “CompanyName Corporate”);
//Set an alternative reply-to address
$mail->addReplyTo($email, $firstname . " " . $lastname);
//Set who the message is to be sent to
$mail->addAddress("[email protected]", “OwnersName”);
//Set the subject line
$mail->Subject = “Contact Form Submission Notification”;
//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body

$temp_HTML =
"

Website Form Submission

$email_body

";

$mail->msgHTML($temp_HTML);
//plain text body alternative
$mail->AltBody = $email_body;
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: ". $mail->ErrorInfo;
} else {
echo “”;
}

Well, on shared servers, it is best to send one email at a time. Therefore, you run the entire script once per email. If you pile them up to multiple to’s all at once, it can bog down the server and break the script. This is quite often due to filling up memory. Memory is limited when sent to the browser. And, if you have a large number of browsers accessing the email system, you need to process one at a time. Send an email, reload and send the next. You do NOT have to recreate the entire routine each time, just loop thru the email list using the same data. In your case, you would loop thru the $mail->send() function just changing the email address. This handles one email at a time, but, little overhead. Also, many email accounts such as AOL, Yahoo and Gmail limit how many you can send out in a list of email addresses. Sending one at a time solves this issue. Hope all that makes sense…

yep it’s fine. I understand it. thanks again man.

You are welcome! But, one more thing.
If you use a template which is loaded, quite often you use keywords to replace first names or other items inside the email. Like <<>> or {first_name}… Something unique. If you do this, you will also need to reload the template and replace the first name each loop of an email. Really depends on your final product. For example, I have a football pool that sends a really fancy email reminding people to make their picks and to announce winners. I have to load it, change the first name and data, send it and then repeat. But, this is fairly fast. If you need to send out thousands of emails, you quite often need to set up timers not to send too many at one time. Godaddy server’s have a limit per hour and per month depending on your level of server. So, just a bit more info for you…

is that possibly related to this problem?

https://www.phphelp.com/t/ridiculously-easy-else-if-problem/30538

Well, that question is a loaded one. I usually use this function:
if ($_SERVER[‘PHP_SELF’]==“somepage.php”) {

But, there are several other $_SERVER functions that give you the current page.
Some give the folders too, like \some-folder\somepage.php so you have to use the basename function.
Depends on how much detail you want in the results.

But, using templates have nothing to do with that post. Off to bed soon… Sounds like you made some progress today…

Ernie, I can’t use "($_SERVER[‘PHP_SELF’]==“somepage.php”) " because that script of mine is running on the POST page after the form is submitted. that’s why i’m checking for REFERRER instead of SELF. i will checkout basename()

You can also just use the SESSION array and pass the page to it. Quite often I do that.
I set the “calling” page in a SESSION variable that I call $return_page and then when done processing,
just pull that variable out and return to it. I mean if that is what you needed it for… Night!

Ernie,

I ended up getting help from another who posted code using an array object as well. So I just copied what he provided and it worked just fine. I’m kinda running out of time on this one as far as learning about what I’m doing cuz the pres of the business contacted me the other day and asked how long this was gonna take. So I had to finish it rather quickly to make him happy. But it does work, so i owe a lot to both of you guys. thanks!

https://www.phphelp.com/t/ridiculously-easy-else-if-problem/30538/6

Always nice to make the boss happy!

Sponsor our Newsletter | Privacy Policy | Terms of Service