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

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