How to send mail via PHP mail() and avoid spam filters

Well, here is the zipped up folder you need.
Download it, unzip it, move it to your server where your test page code is.
Then, add these two lines to your test page at the top of it…

<?PHP
//  Allow this page to display error messages
error_reporting(E_ALL);
ini_set("display_errors", 1);

//  Set up links to the PHPmailer library
use PHPMailer\PHPMailer;
use PHPMailer\Exception;
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

This will be needed just before you set up the PHPmailer connection, but, after the error message setup.
The paths in the five above lines might be different depending on how your server folder directory layout is.

Sorry it took so long to get back to you. Darn Windows Updates! Lost nearly 3 days because of that issue!
Let us know of any errors this produces…

I don’t see the zipped-up folder you mentioned… Where can I find that?

Thanks!

Sorry, it was blocked for some reason. I had to put it on a server for you. Here it is…

PHPmailer 6.7

Thank you!.. I have unzipped PHPmailer-6.7.ZIP, and here’s a screenShot of the results:

Do I put ALL of the files at the same level of my existing code, or do I keep the two subfolders one level below?

Thanks!

normally you have your “root” folder and most programmers put the libraries into one folder named something like “lib” or “libraries” or something like that. Then you steer to the libraries using “/” to get to the root folder and then the library folder and lastly the library’s folder name.
You would not normally separate libraries sub folders from itself. In the test code above, you would need to change them to point to the folder. If you put the PHPmailer folder in the same folder as your code, then it would point as they are already. If you put it into a “lib” folder, then point them to lib/PHPmailer/etc.
If you put it in a folder in your “root” folder, then point them to /lib/PHPmailer/etc…

Hope that makes sense. Leave all of the parts of the file I sent to you as-is, just put it on your server somewhere and then point to that location.

OK… Here is what I have now, and a few questions…
AddedPHPMailer1

Your suggested code included these lines:

// Set up links to the PHPmailer library
use PHPMailer\PHPMailer;
use PHPMailer\Exception;
require ‘PHPMailer/src/Exception.php’;
require ‘PHPMailer/src/PHPMailer.php’;
require ‘PHPMailer/src/SMTP.php’;

The last 3 lines above look like they point to where the required files are.
Regarding the two “use” lines, they don’t seem to align with the structure
– that is there is no PHPMailer\PHPMailer folder…
–OR-- do these two lines do something else?

Thanks!

PS: I ran the code, and got this Error:

Fatal error: Uncaught Error: Class ‘PHPMailer\PHPMailer’ not found in /home2/hdlenhan/public_html/CardSender/0-MailTester.php:41 Stack trace: #0 {main} thrown in /home2/hdlenhan/public_html/CardSender/0-MailTester.php on line 41

Here’s the code:

Well, please don’t post images. Too small to read them. To post code use the “back-tick” character.
It is in the upper left of your keyboard and looks like a backwards apostrophe. Type 3 of them then enter.
Then your code and then enter and 3 more of them. 3 of them in a row of it’s own is the start and finish
of code you want to post.

Looking into the error and your set up, that way is if you use composer to set up the library. Since we did it manually, you need to alter it and set it up this way. ( Sorry for the confusion. )

// Set up links to the PHPmailer library
require ‘PHPMailer/src/Exception.php’;
require ‘PHPMailer/src/PHPMailer.php’;
require ‘PHPMailer/src/SMTP.php’;
use PHPMailer\PHPMailer;
use PHPMailer\Exception;
use PHPMailer\SMTP;

The “use” command sets up a “namespace” for the items. This allows objects to be created using the classes in the three items. Items like " $mail->Subject " mean that the object is $mail and Subject is part of the object. In simple terms.

Try this change and let us know what happens.

One other thing I found out. You do not need the language folder since you will not need to send errors to the users. I guess that is the only reason they include it.

I replaced the code with the new lines you sent, and received these Warnings & Error Message:
Warning: Use of undefined constant ‘PHPMailer - assumed ‘‘PHPMailer’ (this will throw an Error in a future version of PHP) in /home2/hdlenhan/public_html/CardSender/0-MailTester.php on line 33

Warning: Use of undefined constant src - assumed ‘src’ (this will throw an Error in a future version of PHP) in /home2/hdlenhan/public_html/CardSender/0-MailTester.php on line 33

Warning: A non-numeric value encountered in /home2/hdlenhan/public_html/CardSender/0-MailTester.php on line 33

Warning: A non-numeric value encountered in /home2/hdlenhan/public_html/CardSender/0-MailTester.php on line 33

Warning: Division by zero in /home2/hdlenhan/public_html/CardSender/0-MailTester.php on line 33

Warning: Use of undefined constant Exception - assumed ‘Exception’ (this will throw an Error in a future version of PHP) in /home2/hdlenhan/public_html/CardSender/0-MailTester.php on line 33

Warning: A non-numeric value encountered in /home2/hdlenhan/public_html/CardSender/0-MailTester.php on line 33

Warning: Division by zero in /home2/hdlenhan/public_html/CardSender/0-MailTester.php on line 33

Warning: Use of undefined constant php’ - assumed ‘php’’ (this will throw an Error in a future version of PHP) in /home2/hdlenhan/public_html/CardSender/0-MailTester.php on line 33

Warning: require(NANphp’): failed to open stream: No such file or directory in /home2/hdlenhan/public_html/CardSender/0-MailTester.php on line 33

Warning: require(NANphp’): failed to open stream: No such file or directory in /home2/hdlenhan/public_html/CardSender/0-MailTester.php on line 33

Fatal error: require(): Failed opening required ‘NANphp’’ (include_path=’.:/opt/cpanel/ea-php74/root/usr/share/pear’) in /home2/hdlenhan/public_html/CardSender/0-MailTester.php on line 33

Here is what my code looks like now:

<html>
<head>
<title>PHP HDL Mail Tester </title>
<style>
body, TD{
	font-family: 'verdana';
	font-size: '13px';
}
</style>
</head>
<body bgcolor="#d0d0d0">
<?PHP

//  Allow this page to display error messages

error_reporting(E_ALL);
ini_set("display_errors", 1);

//  Set up links to the PHPmailer library

require ‘PHPMailer/src/Exception.php’;

require ‘PHPMailer/src/PHPMailer.php’;

require ‘PHPMailer/src/SMTP.php’;

use PHPMailer\PHPMailer;

use PHPMailer\Exception;

use PHPMailer\SMTP;

// Set up new PHPmailer connection and set to SMTP version

$mail = new PHPMailer(); // create a new object

$mail->IsSMTP(); // enable SMTP

$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only

$mail->SMTPAuth = true; // authentication enabled

$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for Gmail

$mail->Host = "smtp.gmail.com";

$mail->Port = 465; // or 587

$mail->IsHTML(true);

//This is in a loop that selects just ONE Recipent… ( Set up recipient info which is just an email address )

$mail->AddAddress("***********");

// Set up user information and server information

$mail->Username = "**********";

$mail->Password = "***********";

$mail->SetFrom = "************";

$mail->Subject = "TEST EMAIL ! ";

$mail->Body = "JUST IGNORE THIS EMAIL, TESTING PHPmailer SYSTEM ! ";

//  Send message to current recipient

if(!$mail->Send()) {

    echo "Mailer Error: " . $mail->ErrorInfo;

} else {

    echo "Message has been sent";

}

?>
  
 </body>
</html>

I removed your email and password. Never post those. Spammer robots find emails and you don’t want to get junk mail from them.

Which line is number 33? All of the errors were on that line. I have time today to set this up on my server, a Godaddy one, so I will test the code for you and see what is going on with it.

Line 33 is:…

require ‘PHPMailer/src/Exception.php’;

I’m not sure why it is a different color. Odd… I will test this afternoon when I am free.
Odd…

This works on my server. Try it on yours…

//  Set up links to the PHPmailer library
use PHPMailer\SMTP;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require "PHPMailer/src/Exception.php";
require "PHPMailer/src/PHPMailer.php";
require "PHPMailer/src/SMTP.php";

No quotes at all for the USE commands. Seems to work in this order!
Try it.

And, for testing PHP only, you do not need the HTML codes you added to the test page… Just use the PHP code…

<?PHP ... ?>

I added your new code, and removed the HTML…
Running resulted in these:
2023-03-07 18:09:52 CLIENT → SERVER: EHLO www.hdlenhancement.com
2023-03-07 18:09:52 CLIENT → SERVER: AUTH LOGIN
2023-03-07 18:09:52 CLIENT → SERVER: [credentials hidden]
2023-03-07 18:09:52 CLIENT → SERVER: [credentials hidden]
2023-03-07 18:09:52 SMTP ERROR: Password command failed: 534-5.7.9 Application-specific password required. Learn more at534 5.7.9 Sign in with App Passwords - Google Account Help n1-20020a6546c1000000b0050301745a5dsm8081556pgr.50 - gsmtp
SMTP Error: Could not authenticate.
2023-03-07 18:09:52 CLIENT → SERVER: QUIT
SMTP connect() failed. Troubleshooting · PHPMailer/PHPMailer Wiki · GitHub
Mailer Error: SMTP connect() failed. Troubleshooting · PHPMailer/PHPMailer Wiki · GitHub

Okay, now it is working.
Your Google Account was not set up correctly. You need to set it to two-factor-authentication.
That was mentioned to you early on in this process. Did you do that?

To recap, you need to go to a browser and GMAIL.com and log in using your email address and password.
Then, go to settings and alter the security or privacy and turn on 2-way-auth… The above error code
the one that says ERROR on it is a link to Google’s instructions on how to handle it. Make sure you get this done and try again. Let us know the next error… You have the library working now, so you are close!

Yes, my Google Account has 2FA, via an Authenticator App. I believe I need to generate an App Password for it to be accessed via this code. I thought I already did that, but I don’t see it in the code…

I just replaced the PW string with a newly generated App PW, and it looks like it worked!

2023-03-07 19:42:26 CLIENT -> SERVER: EHLO www.hdlenhancement.com
2023-03-07 19:42:26 CLIENT -> SERVER: AUTH LOGIN
2023-03-07 19:42:26 CLIENT -> SERVER: [credentials hidden]
2023-03-07 19:42:26 CLIENT -> SERVER: [credentials hidden]
2023-03-07 19:42:26 CLIENT -> SERVER: MAIL FROM:<>
2023-03-07 19:42:26 CLIENT -> SERVER: RCPT TO:<[email protected]>
2023-03-07 19:42:26 CLIENT -> SERVER: DATA
2023-03-07 19:42:26 CLIENT -> SERVER: Date: Tue, 7 Mar 2023 12:42:25 -0700
2023-03-07 19:42:26 CLIENT -> SERVER: To: [email protected]
2023-03-07 19:42:26 CLIENT -> SERVER: From:
2023-03-07 19:42:26 CLIENT -> SERVER: Subject: TEST EMAIL !
2023-03-07 19:42:26 CLIENT -> SERVER: Message-ID: <cYdlmqNAT1mywz7diUWrBZ3ajMcujVXUxPPT2Krv8U@www.hdlenhancement.com>
2023-03-07 19:42:26 CLIENT -> SERVER: X-Mailer: PHPMailer 6.7.1 (https://github.com/PHPMailer/PHPMailer)
2023-03-07 19:42:26 CLIENT -> SERVER: MIME-Version: 1.0
2023-03-07 19:42:26 CLIENT -> SERVER: Content-Type: text/html; charset=iso-8859-1
2023-03-07 19:42:26 CLIENT -> SERVER:
2023-03-07 19:42:26 CLIENT -> SERVER: JUST IGNORE THIS EMAIL, TESTING PHPmailer SYSTEM !
2023-03-07 19:42:26 CLIENT -> SERVER:
2023-03-07 19:42:26 CLIENT -> SERVER: .
2023-03-07 19:42:27 CLIENT -> SERVER: QUIT
Message has been sent

…and the eMail was delivered to my inbox!

Thank you very much for all of your help! I hope I don’t break it when I incorporate it into my larger file,

Wow! That is so cool !

I am very happy for you and you didn’t have to spend a dime. Just time learning…
Sorry it too so long! Nice when a good programming puzzle gets solved! See you in your next post!

When I adjusted the code to access my database of addresses, I encountered MANY problems…
One of which is the process logs-in for every message, instead of just once to send a batch of messages:

2023-03-11 23:00:47 CLIENT -> SERVER: EHLO www.hdlenhancement.com
2023-03-11 23:00:47 CLIENT -> SERVER: AUTH LOGIN
2023-03-11 23:00:47 CLIENT -> SERVER: [credentials hidden]
2023-03-11 23:00:48 CLIENT -> SERVER: [credentials hidden]
2023-03-11 23:00:48 SMTP ERROR: Password command failed: 454 4.7.0 Too many login attempts, please try again later. y12-20020a17090a2b4c00b00234115a2221sm1929678pjc.39 - gsmtp
SMTP Error: Could not authenticate.
2023-03-11 23:00:48 CLIENT -> SERVER: QUIT
2023-03-11 23:00:48 SMTP ERROR: QUIT command failed:
![image|65x161](upload://cxqsRuqAqRt4oqVNb9gfdfH8Ikm.png)

The above error occurred after about 80 messages are sent.
What can be done to do the login just once before the loop that walks through the database to select every recipient & send their unique message?

Here is a brief summary of what the code is supposed to do:

I have about 700 records in my database, and select them in batches of 50 to 100; each record contains:

  • Priority
  • Name
  • Personal Message
  • eMail address
  • Date of last send

There is a web page that is used to select some images for the current batch/Priority, and a checkbox to confirm a Production Run or a Test Run.
A button labeled “Send” launches a page of code that does the following for the current Batch/Priority:
Set static variables for this run:

  • Sender’s Name & eMail & Password
  • Message
  • Subject
  • Selected Images

Then it opens the database, and Selects all records with the current Priority, and custom variables for each record:

  • A Greeting is pre-pended to the Message.
  • Error checks ensure all variables are defined; if this fails, the record is skipped, and tagged with a “NotSent” Label
  • The message is sent, and the record is tagged with a “Sent” Label, along with the Date & Time

After all records in the Batch have been processed, the loop is exited, and a “Results Message” is sent to me, with a list of the records, sorted by Sent/NotSent
The database is then closed, and the App Exits to it Main Page.

Well, I will suggest to just place a delay in between each message that is sent out. You can use the PHP “sleep” function or use a time delay. I sent out a news-blast on one server and just added in a delay using something like this:

sleep(2);   //  Pause emails for two seconds

Sleep function will delay processing for “x” number of seconds. These are integer only, a number of full seconds. If you want to use milliseconds, you can use ‘usleep’. function and adjust it for a more accurate timing. Something like this:

usleep(1000000);  //  Pause emails for `1,000,000` miiliseconds ( one second )

Yes, this will delay the processing, but, if you delay one second for 700 records and 1 second between each, it is only 11 minutes. Or sleep for usleep(500000) for 1/2 second and it is about 5.5 minutes. I think you understand the logic. Just need to slow down the sending time between each email.
I think that might solve your issue.

Sponsor our Newsletter | Privacy Policy | Terms of Service