doumingchen3628 2012-06-15 16:30
浏览 120
已采纳

php邮件中的空格问题

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. "
";
    $header .= "MIME-Version:1.0" . "
";
    $header .= "Content-type:text/html;charset=utf-8" . "
";

    $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

  • 写回答

4条回答 默认 最新

  • duanpa1980 2012-06-15 16:38
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler