Link to home
Start Free TrialLog in
Avatar of egoselfaxis
egoselfaxis

asked on

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
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

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?
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?
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&
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.
@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.
Avatar of egoselfaxis
egoselfaxis

ASKER

>> 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
ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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