php邮件中的空格问题

hel*_*lle 2 php email postfix-mta

我在某些电子邮件问题上遇到了一些严重的问题。

简而言之:

  1. 加载 html 模板文件fopen
  2. %password用实际值替换一些像这样标记的值str_replace
  3. 通过以下函数发送邮件,$content加载的html模板文件在哪里:

公共函数发送($接收者,$主题,$内容){

    $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);

}
Run Code Online (Sandbox Code Playgroud)

服务器是带 postfix 的 Debian。

邮件模板开头为

<html> <body style="background-color: #fff;"> <table border="0"...
Run Code Online (Sandbox Code Playgroud)

问题是,我无法重现它,在某些情况下,我可以在邮件中找到几个空格,但我没有放在那里。最有问题的是用户和密码字符串中的问题。

其他似乎都是正确的!编码没问题,html被接受,所有邮件都可以投递...

密码生成:

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;
    }
Run Code Online (Sandbox Code Playgroud)

有什么想法吗?从哪里开始搜索?

是模板、str_replace、后缀、客户端……吗?

到目前为止谢谢

Ja͢*_*͢ck 5

我首先将 HTML 保存到一个单独的文件中,然后使用浏览器打开该文件。

如果您看到空格,则表明是 HTML。

如果看起来不错,那么邮件就会被破坏。

芒林

这通常是因为您的行太长而邮件服务器使用超过 80 列(古老标准)的行来执行一些奇怪的事情。

为了防止损坏,我建议如下:

  1. 添加标题Content-Transfer-Encoding: base64

  2. 适用chunk_split(base64_encode(...))于整个电子邮件内容。