dsc56927 2014-12-20 07:33
浏览 172
已采纳

PHP Sendmail发送的电子邮件内容在包含CSS内联样式时未显示

I have a very strange problem that I can't seem to find out why. I know why it's apparently being caused, though ...which is when the HTML content of the email contains inline CSS styling. When I remove the inline styling, the email arrives fine. When I add it, it doesn't.

I am running the script like this:

$text_message = 'HELLO! Some more email content goes here';

$html_message = '<table>
<tbody><tr>
<td class="mcnTextContent" style="padding-top: 9px;padding-right: 18px;padding-bottom: 9px;padding-left: 18px;mso-table-lspace: 0pt;mso-table-rspace: 0pt;-ms-text-size-adjust: 100%;-webkit-text-size-adjust: 100%;color: #606060;font-family: Helvetica;font-size: 15px;line-height: 150%;text-align: left;" valign="top">

<h3 style="margin: 0;padding: 0;display: block;font-family: Helvetica;font-size: 18px;font-style: normal;font-weight: bold;line-height: 125%;letter-spacing: -.5px;text-align: left;color: #606060 !important;">HELLO!</h3>Some more email content goes here

</td>
</tr>
</tbody></table>';



$link = 'link';

$text_message = sprintf($text_message, $msg_details['name'], $link, $this->setts['sitename']);
$html_message = sprintf($html_message, $msg_details['name'], $link, $this->setts['sitename']);

send_mail($msg_details['email'], 'Message Received - ' . $setts['sitename'], $text_message, 
    $this->setts['admin_email'], $html_message, null, $send);

And then the backend of the "send_mail" function is:

function send_mail($to, $subject, $text_message, $from_email, $html_message = null, $from_name = null, $send = true, $reply_to = null) 
{
    global $setts, $current_version;

    if ($send)
    {
        ## set date
        $tz = date('Z');
        $tzs = ($tz < 0) ? '-' : '+';
       $tz = abs($tz);
        $tz = ($tz / 3600) * 100 + ($tz % 3600) / 60;
        $mail_date = sprintf('%s %s%04d', date('D, j M Y H:i:s'), $tzs, $tz);

        $uniq_id = md5(uniqid(time()));

        ## create the message body
        $html_message = ($html_message) ? $html_message : $text_message;

        $html_msg = "<!--
" . $text_message . "
-->
". "<html><body>". $html_message . "</body></html>";

        $from_name = (!empty($from_name)) ? $from_name : GMSG_MAIL_FROM_ADMIN;
        switch ($setts['mailer'])
        {
            case 'sendmail': ## send through the UNIX Sendmail function
                ## create header
                $header = "Date: " . $mail_date . "
".
                    "Return-Path: " . $from_email . "
".
                    "To: " . $to . "
".
                    "From: " . $from_name . " <" . $from_email . ">
".
                    (($setts['enable_bcc']) ? "Bcc: " . $setts['admin_email'] . "
" : "").
                    "Reply-to: " . ((!empty($reply_to)) ? $reply_to : $from_email) . "
".
                    "Subject: " . $subject . "
".
                    sprintf("Message-ID: <%s@%s>%s", $uniq_id, $_SERVER['SERVER_NAME'], "
").
                    "X-Priority: 3
".
                    "X-Mailer: Sendmail [version " . $current_version . "]
".
                    "MIME-Version: 1.0
".
                    "Content-Transfer-Encoding: 7bit
".
                    sprintf("Content-Type: %s; charset=\"%s\"","text/html",LANG_CODEPAGE).
                    "

";

                if ($from_email)
                {
                    $output = sprintf("%s -oi -f %s -t", $setts['sendmail_path'], $from_email);
                }
                else
                {
                    $output = sprintf("%s -oi -t", $setts['sendmail_path']);
                }

                if(!@$mail = popen($output, "w")) 
                {
                    echo GMSG_COULDNT_EXECUTE . ': ' . $setts['sendmail_path'];
                }

                fputs($mail, $header);
                fputs($mail, $html_msg);

                $result = pclose($mail) >> 8 & 0xFF;

                if($result != 0) 
                {
                    echo GMSG_COULDNT_EXECUTE . ': ' . $setts['sendmail_path'];
                }
                break;
        }
    }
}

I know, the code is messy ...but it's not my own, it's a script I'm editing. The problem is just ...whenever I remove the "style..." in the $html_message variable, the HTML email is displayed correctly, including the h3 styling, etc. When I add the style, then it shows only the $text_message content.

What part could be stripping out or disabling the HTML code?

  • 写回答

1条回答 默认 最新

  • drpfu51608120170 2014-12-20 07:46
    关注

    probably your mail client, or a mail filter.

    you seem to understand sendmail well enough, do some tests with

    sendmail -oi -t < /tmp/email.txt
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值