Email sending to many headers

I am sending out an email but it contains a repeat of some of the headers.
It gets rejected from some servers as having >128 recipients or excessively long headers.

Example:

Reply-To: Webite Name
[email protected], Webite Name

Repeated too many times to count

X-Mailer: PHP/5.4.45From: Webite Name [email protected]

Also repeated too many times to count

Below is my script:

if ($todaydate > $almostdue){
	$to2 = "$fname $lname <$email>";
	$subject2 = "Your Membership Dues are Expiring";	
	$headers2 .= "From: Website Name <[email protected]>" . "\r\n";
	$headers2 .= "Reply-To: Website Name <[email protected]>" . "\r\n";
	$headers2 .= "MIME-Version: 1.0" . "\r\n";
	$headers2 .= "Content-type:text/html;charset=UTF-8" . "\r\n";
	$headers2 .= 'X-Mailer: PHP/' . phpversion();
	$fromEmail = "[email protected]";
	$fifth = "-f" . $fromEmail;
	$message2 = "<div style='font-family:arial;font-size:14px;'>
	Dear $fname,<br><br>blah, blah,blah</div><br><br>";
	mail($to2, $subject2, $message2, $headers2, $fifth);	

Any help in why this is happening would be greatly appreciated

The first time you use $headers2, don’t append it.

So,
$headers2 =“line1”;
Not
$headers2 .=“line1”

Also, quite often, servers limit the number of recipients to cut down on spammers. There are many ways to
fix that issue. One is to send out separate emails to each of the recipients instead of adding them all into the
one email. If you have 1000 recipients to send the email to, just run a loop thru the 1000 email addresses and
send one email to each. This will make your server run harder for the length of time it takes to send the emails
out, but, there will be no problem with the servers that receive the one email from you. Since recipients is part
of the header, my guess is that this is the problem.

@Stonecipher, I am confused. I look at NOObie’s code and he already had the first line not appended?

In the posted code, there is no ending brace. And, assuming this line: if ($todaydate > $almostdue){
is valid, the code mails only one email. Therefore, I do not understand where you get 128 items in your
note. Can you explain that further?

[php] $subject2 = "Your Membership Dues are Expiring"; $headers2 .= "From: Website Name " . "\r\n";[/php]

[member=72272]astonecipher[/member] , Yes, sorry, missed that one…

Thanx for the replies, but neither really answer the problem.

First, this is my server so there is nothing to prevent too many emails and the script sends one email at a time, thus there is only ONE recipient per email.

The problem is the headers being duplicated presumably more than 128 times. The “128 recipients” is coming from the bounce, not from the sending server (my server) and must relate to the headers and not recipients. I could include a copy of the headers from the bounced email, but it would contain 128 “Reply to” and 128 “X-Mailer”.

It appears only to be the Reply-to and X-Mailer.

[member=72272]astonecipher[/member] gave you the problem. Did you fix line #4 yet? The first assignment of your headers must NOT
be concatenated/appended…

$headers2 .= "From: Website Name <[email protected]>" . "\r\n";

Should be

$headers2 = "From: Website Name <[email protected]>" . "\r\n";

So, should only the first header not be appended or should all the headers be non-appended.
ie:

$headers2 = "From: Website Name <[email protected]>" . "\r\n";
$headers2 = "Reply-To: Website Name <[email protected]>" . "\r\n";
$headers2 = "MIME-Version: 1.0" . "\r\n";
$headers2 = "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers2 = 'X-Mailer: PHP/' . phpversion();

Because I am also getting repeats of the X-mailer.

Just the first. If you do them all that way, you will be overwriting the previous.

Thanx will give it a shot.
Won’t know if it works until the 15th when the cron job tries again

Throw a test email in to check it and run it manually. You don’t want to test code on production, especially if it is something automated.

It is not a problem with a single test as that is how I tested it initially. Needs to run through the entire database to have this error (repeated headers) and do not want to send out 250 emails unnecessarily. So, will have to wait and see.

Also added at the end of the mailto:
unset($headers2);

This might also help with the repeated headers after each cycle.

Test case:

Loop with 1 email_to
Incremental subjects

Limited to say 10 emails.

It is likely the header appending was the cause.

Yes, silly to test on a live system. Just duplicate the page. Change it to send the emails to just you.
Run it from the browser and see if you get the 250 emails… Or set it to 10 or whatever… Not a big deal
to test it on a duplicate page…

Sponsor our Newsletter | Privacy Policy | Terms of Service