Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unwanted spaces in php HTML mail 1

Status
Not open for further replies.

PeDa

Technical User
Oct 10, 2002
227
NL
I am generating HTML mails using the following script:

Code:
$to = '"xxxxxxxx" <' . EXCURSIE . '>';
$bcc = '"xxxxxxxx" <' . WEBMASTER . '>';
$from = '"xxxxxxxx" <' . EXCURSIE . '>';
$subject = 'XXXXXXX: Inschrijving';
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
$headers .= 'Reply-To: ' . $from . "\r\n";
$headers .= 'BCC: ' . $bcc . "\r\n"; 
$headers .= 'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $t, $headers);

This works exactly as planned, except single spaces are occasionally inserted into the message contents ($t - about 20 rows of text and tables), often interfering with html -or css-elements. How can I circumvent this problem
 
Hi

PeDa said:
This works exactly as planned, except single spaces are occasionally inserted into the message contents ($t - about 20 rows of text and tables), often interfering with html -or css-elements.
Are you sure those are single spaces ? I would say, more probably CR+LF pairs are inserted, which then are rendered as single spaces. In which case the fact that you have 20 rows of text is less interesting. The big question is, how long are those rows ?
RFC 2822 said:
Each line of characters MUST be no more than 998 characters, and SHOULD be no more than 78 characters, excluding the CRLF.
[silver](...)[/silver]
The more conservative 78 character recommendation is to accommodate the many implementations of user interfaces that display these messages which may truncate, or disastrously wrap, the display of more than 78 characters per line, in spite of the fact that such implementations are non-conformant to the intent of this specification (and that of [RFC2821] if they actually cause information to be lost).
( RFC 2822 Internet Message Format | 2. Lexical Analysis of Messages | 2.1. General Description | 2.1.1. Line Length Limits )

So you were warned, do not send long lines as problem may occur on wrapping. Better wrap it yourself. But of course, that alone does not help much, so you also have to send the content in a format immune to line wrapping. And that is why the [tt]Content-Transfer-Encoding[/tt] exists. The most important encoding types you can use in PHP like this :
[ul]
[li]Base64 ( using [tt]base64_encode()[/tt] and [tt]chunk_split()[/tt] )
Code:
[navy]$headers[/navy] [teal].=[/teal] [i][green]'Content-Transfer-Encoding: base64'[/green][/i][teal];[/teal]
[COLOR=orange]mail[/color][teal]([/teal][navy]$to[/navy][teal],[/teal] [navy]$subject[/navy][teal],[/teal] [COLOR=orange]chunk_split[/color][teal]([/teal][COLOR=orange]base64_encode[/color][teal]([/teal][navy]$t[/navy][teal])),[/teal] [navy]$headers[/navy][teal]);[/teal]
[/li]
[li]Quoted-printable ( using [tt]quoted_printable_encode()[/tt] )
Code:
[navy]$headers[/navy] [teal].=[/teal] [i][green]'Content-Transfer-Encoding: quoted-printable'[/green][/i][teal];[/teal]
[COLOR=orange]mail[/color][teal]([/teal][navy]$to[/navy][teal],[/teal] [navy]$subject[/navy][teal],[/teal] [COLOR=orange]quoted_printable_encode[/color][teal]([/teal][navy]$t[/navy][teal]),[/teal] [navy]$headers[/navy][teal]);[/teal]
[/li]
[/ul]


Feherke.
feherke.github.io
 
Thank you for this, but infortunately it yielded a mail full of ASCII. I did however subsequently find
Code:
mail($to, $subject, wordwrap($t, 70), $headers);

that solved this problem.
 
Hi

PeDa said:
Thank you for this, but infortunately it yielded a mail full of ASCII.
I have no access to a system where line wraps to be forced on the e-mail message, so can not reliably test it. But I would say, if you received an ASCII mail, then maybe something wrong happened to the $headers. I only posted the [highlight]important changes[/highlight] but the rest of headers are still needed and hopefully you took care to have [highlight pink]header separator[/highlight] everywhere where needed :
Code:
[teal]<?php[/teal]
[gray]// ... whatever else you may have...[/gray]
[navy]$to[/navy] [teal]=[/teal] [i][green]'"xxxxxxxx" <'[/green][/i] [teal].[/teal] EXCURSIE [teal].[/teal] [i][green]'>'[/green][/i][teal];[/teal]
[navy]$bcc[/navy] [teal]=[/teal] [i][green]'"xxxxxxxx" <'[/green][/i] [teal].[/teal] WEBMASTER [teal].[/teal] [i][green]'>'[/green][/i][teal];[/teal]
[navy]$from[/navy] [teal]=[/teal] [i][green]'"xxxxxxxx" <'[/green][/i] [teal].[/teal] EXCURSIE [teal].[/teal] [i][green]'>'[/green][/i][teal];[/teal]
[navy]$subject[/navy] [teal]=[/teal] [i][green]'XXXXXXX: Inschrijving'[/green][/i][teal];[/teal]
[navy]$headers[/navy]  [teal]=[/teal] [i][green]'MIME-Version: 1.0'[/green][/i] [teal].[/teal] [i][green]"\r\n"[/green][/i][teal];[/teal]
[navy]$headers[/navy] [teal].=[/teal] [i][green]'Content-type: text/html; charset=iso-8859-1'[/green][/i] [teal].[/teal] [i][green]"\r\n"[/green][/i][teal];[/teal]
[navy]$headers[/navy] [teal].=[/teal] [i][green]'From: '[/green][/i] [teal].[/teal] [navy]$from[/navy] [teal].[/teal] [i][green]"\r\n"[/green][/i][teal];[/teal]
[navy]$headers[/navy] [teal].=[/teal] [i][green]'Reply-To: '[/green][/i] [teal].[/teal] [navy]$from[/navy] [teal].[/teal] [i][green]"\r\n"[/green][/i][teal];[/teal]
[navy]$headers[/navy] [teal].=[/teal] [i][green]'BCC: '[/green][/i] [teal].[/teal] [navy]$bcc[/navy] [teal].[/teal] [i][green]"\r\n"[/green][/i][teal];[/teal]
[navy]$headers[/navy] [teal].=[/teal] [i][green]'X-Mailer: PHP/'[/green][/i] [teal].[/teal] [COLOR=orange]phpversion[/color][teal]()[/teal] [highlight pink][teal].[/teal] [i][green]"\r\n"[/green][/i][/highlight][teal];[/teal]
[highlight][navy]$headers[/navy] [teal].=[/teal] [i][green]'Content-Transfer-Encoding: base64'[/green][/i][teal];[/teal][/highlight]
[highlight][COLOR=orange]mail[/color][teal]([/teal][navy]$to[/navy][teal],[/teal] [navy]$subject[/navy][teal],[/teal] [COLOR=orange]chunk_split[/color][teal]([/teal][COLOR=orange]base64_encode[/color][teal]([/teal][navy]$t[/navy][teal])),[/teal] [navy]$headers[/navy][teal]);[/teal][/highlight]

PeDa said:
I did however subsequently find
Code:
mail($to, $subject, wordwrap($t, 70), $headers);
that solved this problem.
That will mostly do it. But on a long URL or some decoration included with data URI it may still fail. So I would insist on the use of the dedicated [tt]Content-Transfer-Encoding[/tt].


Feherke.
feherke.github.io
 
Aaaargh! That was it; in my haste to try your solution I failed to add the [highlight #F57900]header separator[/highlight] to the previously last, and now penultimate, header. It is now of course working. Thank you for your forbearance!
PeterDa.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top