简体   繁体   English

Gmail的HTML电子邮件中表格的内联样式,并使用PHPMailer发送

[英]Inline styles for table in html email for Gmail and sending with PHPMailer

I've spent so much time on this and I'm still stumped! 我花了很多时间在这上面,但我仍然很困惑!

Goal: To apply styles (inline) to table, td, th, p, elements in an html email for Gmail being sent with PHPMailer. 目标:将样式(内联)应用于通过PHPMailer发送的Gmail的HTML电子邮件中的表,td,th,p,元素。

Problem: Inline styles not showing 问题:内联样式未显示

HTML Code: HTML代码:

<section id="email">
    <table cellspacing="0" cellpadding="25" border="10" style="font-family: Arial, sans-serif; text-align: center;">
    <tbody>
        <tr>
            <th style="border: 1px solid rgb(255, 0, 0);">A</th>
            <th style="border: 1px solid rgb(255, 0, 0);">B</th>
            <th style="border: 1px solid rgb(255, 0, 0);">C</th>
            <th style="border: 1px solid rgb(255, 0, 0);">D</th>
        </tr>
        <tr>
            <td style="border: 1px solid rgb(255, 0, 0);">red</td>
            <td style="border: 1px solid rgb(255, 0, 0);">100</td>
            <td style="border: 1px solid rgb(255, 0, 0);">120</td>
            <td style="border: 1px solid rgb(255, 0, 0);">45</td>
        </tr>
        <tr>
            <td style="border: 1px solid rgb(255, 0, 0);">blue</td>
            <td style="border: 1px solid rgb(255, 0, 0);">50</td>
            <td style="border: 1px solid rgb(255, 0, 0);">250</td>
            <td style="border: 1px solid rgb(255, 0, 0);">35</td>
        </tr>
        <tr>
            <td style="border: 1px solid rgb(255, 0, 0);">green</td>
            <td style="border: 1px solid rgb(255, 0, 0);">65</td>
            <td style="border: 1px solid rgb(255, 0, 0);">180</td>
            <td style="border: 1px solid rgb(255, 0, 0);">360</td>
        </tr>
    </tbody>
    </table>
<p style="color:#FF0000;font-weight: bold;">I want this text to be bold and red!</p></section>

JS Code: JS代码:

var data = {
    to:"test@gmail.com",
    from:"<my@email.com>",
    fromName:"me",
    subject:"test html email",
    message: $("#email").html()
};

$.ajax({
    type: "POST",
    url: "email.php",
    data: data,
    success: function(){
        alert("email sent");
    }
});

email.php Code: email.php代码:

<?php
require '../PHPMailer-5.2.9/PHPMailerAutoload.php';

if($_POST){
    $mail = new PHPMailer;
    $mail->From = $_POST['from'];
    $mail->FromName = $_POST['fromName'];
    $mail->addAddress($_POST['to']);     
    $mail->Subject = $_POST['subject'];
    $mail->Body    = $_POST['message'];
    $mail->AltBody = $_POST['messageAlt'];

    $mail->isHTML(true);
    $mail->send();
}
?>

I do receive the email in Gmail with all the data. 我确实在Gmail中收到了包含所有数据的电子邮件。 However, the inline styles in the html are completely ignored. 但是,html中的内联样式将被完全忽略。

Thanks in advance to all who take the time to respond! 在此先感谢所有抽出宝贵时间回复的人!

Ok I managed to figure it out! 好吧,我设法弄清楚了!

There was a problem with my PHP code. 我的PHP代码有问题。 The HTML string needed to have the backslashes stripped. 去除反斜杠所需的HTML字符串。 First I store the message in the $body variable, strip the slashes with "stripslashes($body)" then attach $body to $mail via "MsgHTML($body)". 首先,我将消息存储在$ body变量中,用“ stripslashes($ body)”去除斜杠,然后通过“ MsgHTML($ body)”将$ body附加到$ mail上。

Here is the revised PHP: 这是修改后的PHP:

<?php
require '../PHPMailer-5.2.9/PHPMailerAutoload.php';

if($_POST){
    $mail = new PHPMailer;
    $body = $_POST['message'];
    $body = stripslashes($body);

    $mail->From = $_POST['from'];
    $mail->FromName = $_POST['fromName'];
    $mail->addAddress($_POST['to']);     
    $mail->Subject = $_POST['subject'];
    $mail->MsgHTML($body);
    $mail->AltBody = "Alternate Content";   
    $mail->isHTML(true);

    $mail->send();
?>

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

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