【问题标题】:html email issue for a tag value标签值的 html 电子邮件问题
【发布时间】:2011-11-28 08:37:36
【问题描述】:

我正在通过 php 邮件功能向客户端发送 html 电子邮件。而 & 符号以粗体显示在电子邮件替换中产生问题! %20 类似于我的 id 中的字符,如下所示(粗体)。

http://test.com/test-page.php?id=abcd**!**1234&cat_id=23

下面是我的代码。 $to = 'test@abc.com';

// subject
$subject = 'test';

//message
 $message.='<html><head><meta charset="UTF-8" /></head><body><p><a href="http://test.com/test-page.php?id=abcd1234&cat_id=23" target="_blank">Wine **&** Dine Offers</a></p></body></html>';

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";

// Additional headers
$headers .= 'To: test <test@abc.com>' . "\r\n";
$headers .= 'From: test user <no-reply@test.com>' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers); 

发送邮件后,我收到了!和 %20 喜欢电子邮件中的字符。 我也在电子邮件中尝试了 & except & 但仍然没有用!在我的电子邮件 html 中添加。

【问题讨论】:

    标签: php email html-email


    【解决方案1】:

    尝试在传入参数上运行urldecode()。示例:

    $id = urldecode( $_GET['id'] );
    

    【讨论】:

    • 虽然我同意,但如果有必要,客户端会出现问题,即参数被双重编码(因为 PHP 会自动对 $_GET 中的参数进行 urldecodes),最好找到并解决这个问题...
    • 没用 ?id=83f428239f!%20f3e338d02779a25e0bd641&cat_id=10
    【解决方案2】:

    我认为您应该对电子邮件中的 URL 进行编码。另外,您可能不推荐“开启”magic_quotes_gpc。

    我一直使用 PHPMailer,它可以节省很多工作并帮助解决这些问题

    【讨论】:

      【解决方案3】:

      试试这个:

      $toName  = 'test'; 
      $toAddress  = 'test@abc.com'; 
      
      $fromName  = 'test user'; 
      $fromAddress  = 'no-reply@test.com'; 
      
      // subject
      $subject = 'test';
      
      // URL for link
      // this should have any URL encoded characters literally in the string
      $url = 'http://test.com/test-page.php?id=abcd1234&cat_id=23&msg=URL%20encode%20this%20string';
      
      // HTML for message
      // Call htmlspecialchars() on anything that may need it (like the URL)
      $messageHTML = '<html><head><meta charset="UTF-8" /></head><body><p><a href="'.htmlspecialchars($url).'" target="_blank">Wine &amp; Dine Offers</a></p></body></html>';
      
      // Text version of message
      // Remember, not everyone has an HTML email client!
      $messageText = "Wine & Dine Offers: $url";
      
      // Build a multipart MIME message
      $boundary = '------'.md5(microtime()).'------';
      $body =
      "This is a multipart message in MIME format.\r\n"
      ."$boundary\r\n"
      ."Content-Type: text/plain\r\n"
      ."\r\n"
      ."$messageText\r\n"
      ."$boundary\r\n"
      ."Content-Type: text/html; charset=UTF-8\r\n"
      ."\r\n"
      ."$messageHTML\r\n"
      ."--$boundary";
      
      // To send HTML mail, the Content-type header must be set correctly
      $headers  = "MIME-Version: 1.0\r\n";
      $headers .= "Content-type: multipart/alternative; boundary=\"$boundary\"\r\n";
      
      // Additional headers 
      // the To: header will be set by mail() and is not required here
      $headers .= "From: $fromName <$fromAddress>\r\n";
      
      // Mail it
      mail("$toName <$toAddress>", $subject, $body, $headers); 
      

      【讨论】:

        【解决方案4】:

        我将 phpmailer 上的编码设置更改为使用 base64 编码

        $mail->Encoding=”base64

        这解决了我的问题。

        由于 HTML 邮件消息的长度超过 998 或某些问题,我得到了上述解决方案。 :)

        http://www.jeremytunnell.com/posts/really-hairy-problem-with-seemingly-random-crlf-and-spaces-inserted-in-emails

        【讨论】:

          猜你喜欢
          • 2020-04-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多