Gmail删除了链接标记,如何避免这种情况

时间:2010-03-04 16:00:00

标签: php html email gmail

我正在尝试使用函数mail()发送;包含链接的富文本;我发送这种代码......

 Please,  access <a href="http://www.site.md/contact/en/"> Contact </a> to send all these information

抛出萤火虫我可以看到链接标签已被删除,代码变得像这样

 Please,  access <a>Contact</a> to send all these information

在禁止违反规则的人之后,我需要这个脚本发送电子邮件告诉我们禁止他的原因。 在另一个电子邮件服务电子邮件没有问题,我的错误,对不起我的英语,我会显示发送电子邮件的脚本,重要的一部分...

     // Set and wordwrap message body
 $body = "From: $name\n\n";
 $body .= "Message: $message";
 $body = wordwrap($body, 70);

 // Build header

 $headers  = 'MIME-Version: 1.0' . "\r\n";
 $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";

 $headers .= "From: $email\n";
 if ($cc == 1) {
  $headers .= "Cc: $email\n";
 }
 $headers .= "X-Mailer: PHP/Contact";


 // Send email
 if(!@mail($to, $subject, $body, $headers)) { 
  echo '<b> ERROR ! </b> Unfortunately, a server issue prevented delivery of your message<br />'; }

4 个答案:

答案 0 :(得分:2)

除非你在这里没有发布的代码中对$ body执行某些操作,否则我的猜测是wordwrap()会导致问题。在PHP手册中是一​​个用户贡献的功能,可能会有所帮助: http://www.php.net/manual/en/function.wordwrap.php#89782

答案 1 :(得分:0)

这可能是迟到的,但是,好吧只是搞清楚。至少那是我在这里看到的。 基本上我基于一些数据生成新闻稿动态,当我在字符串中找到特定语法时,我不得不用锚标记替换它。无论如何,这是:

格式不正确:

"<a href='" + url + "'>" + name + "</a>";

格式良好:

'<a href="' + url + '">' + name + '</a>';

直接回答,对href属性使用双引号

答案 2 :(得分:0)

问题的一部分是长行,但wordwrap()不足以解决这个问题。

电子邮件可能有任意长的行,但这些是通过仅允许短行的协议传输的。所以必须拆分长线。协议标记已经通过添加=然后结束它们而被拆分的行,所以开始看起来像这样。

Characters       2         3         4         5         6         7
12346790123456789012345678901234567890132456789012345678901234567890123456789

This is a long line with text which goes beyond the 78 character limit imposed by the protocol

最终看起来像这样

This is a long line with text which goes beyond the 78 character limit=
imposed by the protocol

使用=就像那样意味着=不能在你的消息中使用,所以它必须被转义。所以你需要在你的消息中用= 3D替换=(其中3D是=的十六进制代码)。

将任何控制字符(使用ascii代码&lt; 32)替换为= xx以及使用ascii代码超过126的任何内容也是明智的。我使用这些函数来执行此操作,然后您需要在发送之前执行$message=encodeText($message)并且问题应该消失。

function encodeText($input) {
  // split input into lines, split by \r\n, \r or \n
  $lines=preg_split("/(?:\r\n|\r|\n)/", $input);
  $text="";

  // for each line, encode it into a 78 char max line.
  for ($i=0; $i<count($lines); $i++) {
     $text.=encodeLine($lines[$i]);
  }

  return $text;
}

/**
 * This splits a line into a number of pieces, each shorter than the 78 char
 * limit. It also add continuation marks (i.e. =) to then end of each piece
 * of the resulting line, and escapes any = characters, control characters
 * or characters with bit 8 set, and backspace.
 * @return a multiline string (with /r/n linebreaks)
 */
function encodeLine($line) {
  $split=Array();
  $result="";
  $piece='';
  $j=0;
  for ($i=0; $i<strlen($line); $i++) {
     // if you've just split a line, you'll need to add an = to the
     // end of the previous one
     if ($j>0 && $piece=="") $split[$j-1].="=";

     // replace = and not printable ascii characters with =XX
     if (ord($line{$i})==0x3D) {
        $piece.="=3D";
     } else if (ord($line{$i})<32) {
        $piece.="=".bin2hex($line{$i});
     } else if (ord($line{$i})>126) {
        $piece.="=".bin2hex($line{$i});
     } else {
        $piece.=$line{$i};
     }

     // if the resulting line is longer than 71 characters split the line
     if (strlen($piece)>=72) {
        $split[$j]=$piece;
        $piece="";
        $j++;
     }
  }

  // the last piece being assembled should be added to the array of pieces
  if (strlen($piece)>0) $split[]=$piece;

  // if a piece ends in whitespace then replace that whitespace with =20
  // for a space or =09 for a tab.
  for ($i=0; $i<count($split); $i++) {
     $last=substr($split[$i],-1);
     if ($last=="\t") $last="=09";
     if ($last==" ") $last="=20";
     $split[$i]=substr($split[$i],0,strlen($split[$i])-1).$last;
  }

  // assemble pieces into a single multiline string.
  for ($i=0; $i<count($split); $i++) {
     $result.=$split[$i]."\r\n";
  }

  return $result;
}

答案 3 :(得分:0)

尝试这一点,将stripslashes()与电子邮件正文一起使用,它应该可以正常使用

$body= stripslashes($mail_message);