简体   繁体   English

在发送PEAR Mail_Mime电子邮件时,邮件html中缺少点(。)s

[英]dot(.)s are missing here & there in the mail html while sending PEAR Mail_Mime emails

I am sending a mail using PEAR's mail and mail_mime packages and sample code as below: 我使用PEAR的mail和mail_mime包发送邮件,示例代码如下:

$sendStart=array();
require_once('Mail.php');
require_once('Mail/mime.php');

$sendStart['mail'] =& Mail::factory('mail');
$sendStart['mime'] = new Mail_mime("\n");

$sendStart['mime']->setHTMLBody($html);
$sendStart['headers']['Subject']=$title;
$sendStart['headers']['X-SMTPAPI']='{"category": ["MailID-XXX"]}';

$body=$sendStart['mime']->get(array(
        'html_charset'=>'UTF-8',
        'text_charset'=>'UTF-8',
        'head_charset'=>'UTF-8'
    ));

//echo ($sendStart['mime']->_htmlbody); exit;
$sendStart['mail']->send('xxx@example.com',$sendStart['mime']->headers($sendStart['headers']),$body);

I am facing a strange issue while sending the mails through this code. 通过此代码发送邮件时,我遇到了一个奇怪的问题。 I have images inside the email body, sometimes the images not displaying. 我在电子邮件正文中有图像,有时图像不显示。 When I debug the issue, I found that . 当我调试问题时,我发现了. is missing in the image url. 在图片网址中缺少。 But if I print the mail just before the send line (as I comment out in the code) it prints with the image perfectly. 但是,如果我在发送行之前打印邮件(因为我在代码中注释掉),它会完美地打印出图像。

correct image url: http://www.domain.com/image.png 正确的图片网址: http://www.domain.com/image.pnghttp://www.domain.com/image.png

in the mail : http://www.domaincom/image.png or http://www.domain.com/imagepng ... etc. 在邮件中: http://www.domaincom/image.pnghttp://www.domain.com/imagepng ...等

part of the HTML code which has the image as below: HTML代码的一部分,其图像如下所示:

<table cellpadding="0" cellspacing="0" border="0" class="image-table image-2" align="center" style="float:none;margin-left:auto;margin-right:auto;text-align:left;">
    <tbody>
      <tr>
        <td class="element" style="text-align: left;height: auto;overflow: hidden;-webkit-text-size-adjust: none;">
          <!--[if gte mso 9]><img alt="Placeholder Image" src="http://www.domain.com/image.png" style="outline: none; text-decoration: none; display: block; clear: none; float: none;    margin-left: auto; margin-right: auto;display:none; mso-hide: none;" align="center" width="394"><![endif]--><![if !mso]><!-- --><img alt="Placeholder Image" src="http://www.domain.com/image.png" style="outline: none;text-decoration: none;display: block;clear: none;float: none;width: 100%;height: auto;max-width: 394px;margin-left: auto;margin-right: auto;*width: 394px;-ms-interpolation-mode: bicubic;" align="center"><!--<![endif]-->
        </td>
      </tr>
    </tbody>
</table>

And the very strange thing is it is displaying properly in outlook but not in other clients as for outlook I have separate code(as per the code). 而奇怪的是它在outlook中正确显示但在其他客户端没有显示,因为我有单独的代码(根据代码)。

Does anybody have any idea how to debug the issue or any comment on this issue. 有没有人知道如何调试问题或对此问题的任何评论。

Edited: 编辑:

This issue is nothing to do with any specific tag (although I explain it using image tag), I experience it in several places such as in styles. 这个问题与任何特定的标签无关(虽然我用图片标签解释),我在几个地方体验它,比如样式。

example : line-heigth:1.5; 例如: line-heigth:1.5; is the original and it changes to line-heigth:15; 是原始的,它变为line-heigth:15; while sending out. 发送时。

Basically it just remove the . 基本上它只是删除了. here & there in the email HTML. 在这里和那里的电子邮件HTML。

I'm pretty sure this is caused by dot-stuffing; 我很确定这是由点填充引起的; as the dot is used as a special indicator in emails. 因为点被用作电子邮件中的特殊指示符。 You can read about this in the rfc where it says (emphasis added): 你可以在它说的rfc中读到这个(强调添加):

To allow all user composed text to be transmitted transparently, the following procedures are used: 要允许透明传输所有用户组合文本,请使用以下过程:

  • Before sending a line of mail text, the SMTP client checks the first character of the line. 在发送一行邮件文本之前,SMTP客户端会检查该行的第一个字符。 If it is a period, one additional period is inserted at the beginning of the line. 如果是句点,则在行的开头插入一个附加句点。
  • When a line of mail text is received by the SMTP server, it checks the line. 当SMTP服务器收到一行邮件文本时,它会检查该行。 If the line is composed of a single period, it is treated as the end of mail indicator. 如果该行由单个句点组成,则将其视为邮件结尾指示符。 If the first character is a period and there are other characters on the line, the first character is deleted. 如果第一个字符是句点并且该行上还有其他字符,则删除第一个字符。

It seems the client you're using to compose these emails does not implement the first procedure, while the server it sends the mail to does implement it; 您用来撰写这些电子邮件的客户端似乎没有实现第一个过程,而它发送邮件的服务器确实实现了它; leading to dots disappearing. 导致点消失。

The fix would be to make your client implement the padding. 修复方法是让您的客户端实现填充。

使用PHPMailer,它将使生活更轻松。

Here is the example of it - 这是它的例子 -

     // Set up the headers that will be included in the email.
    $recipient = 'someemail@gmail.com';
    $from = 'someemail1@gmail.com';

    $headers = array(
      'To'            => $recipient,
      'From'          => $from,
      'Return-Path'   => $from,
      'Reply-To'      => $replyto, //based on your need
      'Subject'       => $subject,
      'Errors-To'     => '<<a href="mailto:errors@example.com">errors@example.com</a>>',
      'MIME-Version'  => '1.0',
    );

    // Set up parameters for both the HTML and plain text mime parts.
    $textparams = array(
      'charset'       => 'utf-8',
      'content_type'  => 'text/plain',
      'encoding'      => 'quoted/printable',
    );
    $htmlparams = array(
      'charset'       => 'utf-8',
      'content_type'  => 'text/html',
      'encoding'      => 'quoted/printable',
    );

    // Create the email itself. The content is blank for now.
    $email = new Mail_mimePart('', array('content_type' => 'multipart/alternative'));

    // Add the text and HTML versions as parts within the main email.
    $textmime = $email->addSubPart($textbody, $textparams);
    $htmlmime = $email->addSubPart($htmlbody, $htmlparams);

    // Get back the body and headers from the MIME object. Merge the headers with
    // the ones we defined earlier.
    $final = $email->encode();
    $final['headers'] = array_merge($final['headers'], $headers);

    // Perform the actual send.
    $smtp_params = array();
    $smtp_params['host'] = '127.0.0.1';
    $smtp_params['port'] = '25';
    $smtp_params['persist'] = TRUE;

    $mail =& Mail::factory('smtp', $smtp_params);
    $status = $mail->send($recipient, $final['headers'], $final['body']);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM