Problems with spaces in php mails

只愿长相守 提交于 2019-12-10 11:04:03

问题


I am having some serious problem with some e-mail issue.

in short:

  1. loading a html template file fopen
  2. replacing some values, marked like this %password with real values str_replace
  3. sending mail via the following function, where $content is the loaded html template file:

public function send($receiver, $subject, $content){

    $header = "From:".sender. "\n";
    $header .= "MIME-Version:1.0" . "\n";
    $header .= "Content-type:text/html;charset=utf-8" . "\n";

    $mailText = $content;
    mail($receiver, $subject, stripslashes(iconv('utf-8', 'iso-8859-1', $mailText)) , $header);

}

the server is debian with postfix.

The Mail template starts with

<html> <body style="background-color: #fff;"> <table border="0"...

The thing is, and I cannot reproduce it, that in some cases I can find several spaces in the mail, which I didn't put there. Most problematical are those in the user and password string.

Evering else seems correct! The encoding is ok, the html is accepted, all mails can be delivered ...

password generation:

public static function create_password($length = 12) {
        $characters = array("a", "b", "c", "d", "e", "f", "g", "h", "k", "m", "n", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "_");
        $password = "";
        for($i=0; $i<$length; $i++) {
            $index = rand(1, count($characters)) -1;
            $password .= $characters[$index];
        }
        $password = str_replace("__","_", $password);
        return $password;
    }

Any Ideas, where to start my search?

Is it the template, str_replace, the postifix, the client, ... ?

Thanks so far


回答1:


I would start by saving the HTML into a separate file and open that with a browser.

If you see spaces, it's the HTML.

If it looks fine, the mail gets mangled.

Mangling

This is often caused because your lines are simply too long and mail servers do funky stuff with lines longer than 80 columns (ancient standards).

To prevent mangling I would suggest the following:

  1. Add header Content-Transfer-Encoding: base64.

  2. Apply chunk_split(base64_encode(...)) over the whole e-mail content.




回答2:


I recently had this same problem but for the To: line with multiple email addresses. Here are the technical details.

According to the PHP docs for mail(): The formatting of this string must comply with » RFC 2822. This is for the $to and $message parameters.

In particular section: 2.1.1. Line Length Limits

There are two limits that this standard places on the number of
characters in a line. Each line of characters MUST be no more than
998 characters, and SHOULD be no more than 78 characters, excluding
the CRLF.

The 998 character limit is due to limitations in many implementations which send, receive, or store Internet Message Format messages that simply cannot handle more than 998 characters on a line. Receiving implementations would do well to handle an arbitrarily large number of characters in a line for robustness sake. However, there are so many implementations which (in compliance with the transport requirements of [RFC2821]) do not accept messages containing more than 1000 character including the CR and LF per line, it is important for implementations not to create such messages.

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). Again, even though this limitation is put on messages, it is encumbant upon implementations which display messages

to handle an arbitrarily large number of characters in a line
(certainly at least up to the 998 character limit) for the sake of
robustness.

There are multiple ways to overcome this found all over the web.

  1. From the PHP docs: $message = wordwrap($message, 70, "\r\n");
  2. From Jack's answer: chunk_split(base64_encode(...))
  3. From experience: foreach($emails as $email){ $to_email = $email."\n";}



回答3:


It looks like you might be exceeding the maximum body line length, when this happens, email clients do strange things such as this. Don't put all your body content on one line, use \n to create new lines.

Like this

<html>
    <body style="background-color: #fff;">
        <table border="0"...

Instead of

<html> <body style="background-color: #fff;"> <table border="0"...



回答4:


this:

stripslashes(iconv('utf-8', 'iso-8859-1', $mailText));

Will product a NOTICE if any of your characters passed is NOT latin (UTF8 encoded) That means they will be messed up (stop in first non-latin, if any).



来源:https://stackoverflow.com/questions/11054746/problems-with-spaces-in-php-mails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!