Gmail移除链接标签,如何避免?

10

我正试图使用函数mail()发送富文本,其中包含链接;我正在发送如下代码...

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

我抛出了一个Firebug,发现链接标签已被移除,代码变成了这样

 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个回答

3

不起作用了,我删掉了这个函数,但问题没有解决。我说过这个问题在 Gmail 上出现,但在其他电子邮件服务中没有问题。那么问题就在 Gmail 上,但还是谢谢你的回答 Jah ;) - mIRU

1

问题的一部分是长行,但是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码<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;
}

这就是解决我的方法。我甚至没有使用wordwrap,但似乎gmail(而不是其他电子邮件服务提供商)正在为我的电子邮件进行自动换行,因此从<a href =“ ...”>中删除了=,导致链接未显示出来。 - V. Rubinetti

0

试一下这个,使用stripslashes()函数处理电子邮件正文,它应该可以完美地工作。

$body= stripslashes($mail_message);

0
这可能有点晚了,但是没关系,你可以自己想办法解决。至少这是我在这里看到的。 基本上,我根据一些数据动态生成通讯,当我在字符串中发现特定语法时,我必须用锚标签替换它。无论如何,这就是它:

格式不正确:

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

良好的格式:

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

直截了当的回答是,href 属性使用双引号

不,对我没用(我想知道它是否对任何人都有效过)。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接