dsc56927
dsc56927
2014-12-20 07:33

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 drpfu51608120170 7年前

    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
    
    点赞 评论 复制链接分享