Spaces being randomly inserted into notification emails sent using PHP mail() function

I'm running in to an issue where spaces are being randomly inserted into notification emails which I'm generating and sending using PHP's built-in mail() function.

Whenever it occurs (which isn't always) .. the spaces are inserted in random spots in the emails, .. which sometimes results in hyperlinks being broken & not resolving correctly.  

Why is this happening?  And what can I do to fix it?

Just in case it's relevant, .. these are the mail headers that I'm using:

$headers = "From: $mailFrom" . "\r\n" . "Reply-To: $mailFrom" . "\r\n" . "Content-type: text/html; charset=iso-8859-1" . "\r\n";

Open in new window


Also -- is it possible that the mail client being used to retrieve and view the emails might be the culprit (ie: Outlook, .. or Windows Mail) ?  Or what else could it possibly be?

Thanks,
- Yvan
egoselfaxisAsked:
Who is Participating?
 
gr8gonzoConnect With a Mentor ConsultantCommented:
If your code looks like this:

mail($to,$subject,$body);

...then BEFORE your mail() command, you would do something like this:

file_put_contents("maildebuglog.txt",$body);

Then you run the script, and open the maildebuglog.txt file that was created.
0
 
gr8gonzoConsultantCommented:
Have you tried to save the message bodies to a log file so you can see if the space is somehow getting inside the content BEFORE it gets to the mail() function?
0
 
Julian HansenCommented:
How do you know spaces are being inserted? Is it because people receiving the emails are reporting it?

It is very likely that the mail client is to blame - especially since the emails are html.

I would have a closer look at the spaces - you might find they are not actually random - if you can find the pattern you should be able to figure out what is causing it.

Do you have a sample mail with the spaces?
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
COBOLdinosaurCommented:
The fact that it does not happen all the time makes me suspect that the email client is responsible.  Using HTML in email generally has problems with at least some clients.

Cd&
0
 
gr8gonzoConsultantCommented:
I wouldn't be so quick to blame the mail client, unless the mail client is a relatively new/untested one. If you're seeing it on Gmail, Yahoo, Hotmail, Thunderbird, Outlook, etc... then it's probably the content of the message.

It's easiest to just log the outgoing messages until you find an email with the spaces, and then go back to the log to see if the message body actually went out that way. Code is more likely to have a simple mistake than for most major mail clients to have a bug that modifies content incorrectly.
0
 
Julian HansenCommented:
@gr8gonzo I was thinking primarily of Outlook - not in a very MS-friendly mood today so trying to blame as much on them as possible.

which sometimes results in hyperlinks being broken & not resolving correctly.
Just a thought are your hyperlinks always the same or are they different?

If different are you encoding them before you put them in the email ?

In other words
http://www.someurl.com/?category=My Category

Open in new window

vs
http://www.someurl.com/?category=My%20Category

Open in new window

Could result in the behaviour you are seeing.
0
 
egoselfaxisAuthor Commented:
>> It's easiest to just log the outgoing messages until you find an email with the spaces, and then go back to the log to see if the message body actually went out that way.

Please refresh my memory ... How would I "log" the outgoing messages?
I assume that you mean write them to a log/txt file of some kind on the server?  
What's the code for that again?

Thanks,
- Yvan
0
 
egoselfaxisAuthor Commented:
Thanks .. I took it one step further by adding a couple of additional flags:

file_put_contents("maildebuglog.txt", $body, FILE_APPEND | LOCK_EX);

http://php.net/manual/en/function.file-put-contents.php

- yg
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.