我花了很多时间在这上面,我仍然难过!
目标:将样式(内联)应用于使用PHPMailer发送的Gmail的html电子邮件中的table,td,th,p元素。
问题:内联样式未显示
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代码:
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代码:
<?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();
}
?>
我确实收到Gmail中包含所有数据的电子邮件。但是,html中的内联样式完全被忽略。
提前感谢所有花时间回应的人!
答案 0 :(得分:0)
好的,我设法搞清楚了!
我的PHP代码出现问题。需要删除反斜杠的HTML字符串。首先,我将消息存储在$ body变量中,用&#34; stripslashes($ body)&#34;去掉斜杠。然后通过&#34; MsgHTML($ body)&#34;将$ body附加到$ mail。
这是经过修改的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();
?>