2

I'm experiencing a strange problem and difficult to diagnose because
it's random. I have built an application that sends out an email with
a nice amount of text (don't have exact char count, but could get it).

On the html email, a random whitespace appears in the content.

See below for examples of how the space wanders and is random:

1- "Th ere are several things being discussed in this email."
2- "There are se veral things being discussed in this email."
3- "There are severa l things being discussed in this email."

This whitepace issue also happens for links. Found this issue in hotmail and gmail so far.

Anybody have any ideas?

$header = "MIME-Version: 1.0\r\n";
$header .= "Content-type: text/html; charset=utf-8\r\n";
$header .= "From: abc \r\n";
$subject = "abc";
$mail_body = "Hello!";
$mail_body .= "content";
mail($email_address, $subject, $mail_body, $header);

That is the code to give you an idea roughly..

rico
  • 21
  • 1
  • 3

4 Answers4

1

You might be getting this issue when you have a long line and it is automatically broken to then next line after the RFC's 72 char recommendation.

where

hey lots of stuff here Jimmy will probably be way longer than 72 characters

will become

hey lots of stuff here Jimmy will probably be way longer than 72 charac
ters

And which would render in a HTML e-mail as

hey lots of stuff here Jimmy will probably be way longer than 72 charac ters
Sam Figueroa
  • 2,301
  • 22
  • 21
0

It would help if you posted offending code, as there are many things that could cause this.

Do you use mail() or phpmailer or swiftmailer? Are your emails multipart or text only? Do you parse your templates yourself?

If it's HTML, please post the HTML code that clients receive, etc.

Is it possible that there are trailing/heading spaces when you concatenate strings? Try "trim"ming on each element you add to your $mail_body like:

$mail_body .= trim($content);
Terry Felkrow
  • 653
  • 1
  • 8
  • 18
0

If you "view source" of the email message, is the space in the source, or is it just appearing in the rendered (visible) text? First thing I would do is look at the email source to see what's up - I wouldn't trust the rendered/visible text for debugging this.

Also, you said "on the html email". That implies that you might be sending a plain text version either to different people, or maybe to the same people (as a multipart message). If that's the case, can you confirm that it's not there also?

As Terry said, can you post some of the code, specifically the code that sends the message and the some/all (if it's short) of the code that builds the message text up for sending?

Chirael
  • 3,025
  • 4
  • 28
  • 28
  • I've just posted the code..hope that helps. The problem is...even the links have white spaces and it is converted to "+" sign and that broke the links . The white space is in HTML source as well. Unfortunately, I can't share the email, coz it is a private emails...Again this happens randomly. So frustating... – rico Nov 24 '09 at 04:07
  • If it were me, I'd add code to save the body text to a temporary log file every time a message is sent (along with any date/time stamp, subject, etc. you need to identify that message from another). Then every time the mysterious space appeared, I'd compare the corresponding log file entry to that message. That would at least let you figure out whether your script is doing it or it's "out it the ether". Chances are, of course, that your script is inserting the spaces. But then you'd have a place to play with trying different approaches (the log file). – Chirael Nov 24 '09 at 04:27
  • Some stuff to try... Try using single quotes when you don't need variable interpolation. Instead of building up the string in parts in that function, put the message-building into a function of its own, with the fill-in values put as parameters, and returning one big message string. Then, of course, unit test the hell out of that message-building function :) (Personally I like to create message template files like "Dear [Name], Thanks for buying [Product] etc", and then have a generic function that reads the files and does substitutions - that way you can change the message w/o editing code) – Chirael Nov 24 '09 at 04:34
  • You could also try using PHP's "heredoc" method of building the message instead of a bunch of string concatenations; see http://php.net/manual/en/language.types.string.php - but personally I really like to get email message text out of my PHP/scripting code entirely as I said in the last comment. – Chirael Nov 24 '09 at 04:37
  • Rico, let me know if you tried saving the email text to a log file and if it also has the spaces there. I don't think you're going to get many more answers though because the pseudo-code you posted is fine - the problem is probably in the "real" code and has been abstracted away so it's not present in the pseudo-code. – Chirael Nov 24 '09 at 18:49
  • (i.e., feel free to post more of the "real code" and you might get more answers :) – Chirael Nov 24 '09 at 19:06
0

The issue, as I understand it, is from your lines running too long. The PHP mailer adds line breaks (and with them, white space) to break up long lines.

My recommendation for this is to add "\r\n" to the end of your lines. This purposely adds line breaks to your text, keeping the mailer from doing so on its own. eg:

There are several things being discussed in this email.\r\n

notice double quotes are used here. Single quotes will simply append \r\n to your text.

For reference: This question answered the same question when I had it.

BEingprabhU
  • 1,618
  • 2
  • 21
  • 28