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

Before I try that, there is another issue: The following lines are displayed on screen for EACH Message:

2023-03-11 22:56:30 CLIENT -> SERVER: EHLO www.hdlenhancement.com
2023-03-11 22:56:30 CLIENT -> SERVER: AUTH LOGIN
2023-03-11 22:56:30 CLIENT -> SERVER: [credentials hidden]
2023-03-11 22:56:30 CLIENT -> SERVER: [credentials hidden]
2023-03-11 22:56:30 CLIENT -> SERVER: MAIL FROM:<>
2023-03-11 22:56:30 CLIENT -> SERVER: RCPT TO:<[email protected]>
2023-03-11 22:56:31 CLIENT -> SERVER: DATA
2023-03-11 22:56:31 CLIENT -> SERVER: Date: Sat, 11 Mar 2023 15:56:30 -0700
2023-03-11 22:56:31 CLIENT -> SERVER: To: [email protected]
2023-03-11 22:56:31 CLIENT -> SERVER: From:
2023-03-11 22:56:31 CLIENT -> SERVER: Subject: TEST EMAIL !
2023-03-11 22:56:31 CLIENT -> SERVER: Message-ID: <[email protected]>
2023-03-11 22:56:31 CLIENT -> SERVER: X-Mailer: PHPMailer 6.7.1 (https://github.com/PHPMailer/PHPMailer)
2023-03-11 22:56:31 CLIENT -> SERVER: MIME-Version: 1.0
2023-03-11 22:56:31 CLIENT -> SERVER: Content-Type: text/html; charset=iso-8859-1
2023-03-11 22:56:31 CLIENT -> SERVER:
2023-03-11 22:56:31 CLIENT -> SERVER: JUST IGNORE THIS EMAIL, TESTING PHPmailer SYSTEM !
2023-03-11 22:56:31 CLIENT -> SERVER:
2023-03-11 22:56:31 CLIENT -> SERVER: .
2023-03-11 22:56:31 CLIENT -> SERVER: QUIT
Message has been sent

That was about 2,000 lines for 86 messages… How can I suppress these?

Well, the line

$mail->SMTPDebug 

sets the debugging option and will post back the details of the email.
I think you just need to set that to zero and it will not “debug” the email services.

FYI, I already had a ‘sleep(8)’ in my original code… I’ll add it to the new code.

I need to put the PHPMailer code in a loop, so it can step through the list of recipients, one at a time. My loop begins with:

$newQuery = "SELECT * FROM Recipients ";
	$newQuery .= $kw . "Priority = $Priority" ; 
	$newQuery .= " ORDER BY $sortBy";


$result = mysqli_query($link, $newQuery) or die(mysqli_error($link));
$num = 0;
 
while($row = mysqli_fetch_array($result)){
	$num = 1 + $num;
	$recipientAddress = $email4;
	$recipAddr = $row['EmailAddress'];
	if($ProductionChecked == '1') {$recipientAddress = $recipAddr;}
	$RecipientName = $row['LastName'] . ", " . $row['FirstName'];
	$GreetingName = $row['GreetingName'];
	$PersMsg = $row['PersMsg'];
	$CurrPriority = $row['Priority'];
	$SentPriority = $CurrPriority + (10 * $CurrPriority);

HOWEVER, when I add the PHPMailer code after this, the process fails (just a blank screen, and no messages are sent.

As a test, I ended the while loop BEFORE the PHPMailer code, and it all worked, but as you know, the PHPMailer code sends just one message.

My test retains the hard-coded recipient, but since there are 4 records selected, it should loop through 4 times and send that hard-coded message to me 4 times.

Evidently, including the PHPMailer code in this loop breaks something… I have ended the loop prior to the PHPMailer, and added echo statements to confirm my loop has found the 4 unique recipients (it does…), but simply moving the ending bracket “}” to after the PHPMailer, it breaks. When I move that bracket to a line before the PHPMailer, it works, and my echo statements confirm the loop is stepping through 4 records, and the PHPMailer sends just one message to my hard-coded address.

Is the PHPMailer unable to work in a loop?

Sorry, I was out of town for several days. yes, it works fine inside loops. You just need to create a new object each pass and remove when completed. In your WHILE loop, place the code I gave you originally. gave you. You do NOT need the REQUIRES or USE lines. Those should be before the WHILE loop. You only need them once each. Inside the WHILE, you should start with:$mail = new PHPMailer(); // create a new object, which creates a new mailer object instance for each email address retrieved from your database…

Hope that make sense…

I moved the 3 use lines and the 3 require lines to just before the while() statement, and ran the code.
I received this error:
Deprecated: Invalid characters passed for attempted conversion, these have been ignored in /home2/hdlenhan/public_html/CardSender/0-MailTester.php on line 289

where line 289 is the second line in the code below:

$headers2 .= 'Message-ID: ' . sprintf("<%s.%s@%s>",
                                base_convert(microtime(), 10, 36),
                                base_convert(bin2hex(openssl_random_pseudo_bytes(8)), 16, 36),
                                'hdlenhancement.com') . $eol;

But it works in spite of that…
I set up the loop to get 6 records, and my on-screen alerts confirmed that each one was selected, and ALL 6 were received… So the code seems to work…
EXCEPT when I put it into my larger file, which has a front-end that lets me select the cover image, and a few other selections. The front-end loads as before, and I am able to make the various selections, but when I click the button to start the send process, all I get is a blank screen. I even have a few
echo “On line nnn”
statements, but none of them display, so I am guessing something is broken when that code is being compiled…
Stay tuned…

SUCCESS!!! I had a couple of typos in my larger file… I corrected those, and now the process works as intended.

THANK YOU VERY MUCH FOR ALL OF YOUR HELP!

Glad to help! that is why we are here! See you in your next project !

Sponsor our Newsletter | Privacy Policy | Terms of Service