省略 Gmail API 样式和图片,除非显示密件抄送标头



我正在使用Gmail API和PHP在用户下订单时从单个Gmail帐户向用户发送确认电子邮件。html 正文和样式/图像不一致。对于计算机上收到的每封电子邮件(在AIM,AOL,gmail或Gsuite地址上),HTML的表格部分工作正常,但没有背景色,没有图像,没有居中。对于移动设备上发送到AIM的电子邮件,我得到了所有的样式和图像,但暴露了密件抄送字段(除了一次密件抄送被隐藏并且样式和图像也丢失了)。在移动设备上,如果我查看收到的 (密件抄送) 或发送的电子邮件,如果我登录到发送它们的帐户,我可以看到居中、图像和其他样式。当我尝试使用多部分/相关和 64 位编码图像时,我收到了一个带有大量图像代码的大附件,当我使用多部分/混合时,我得到了相同的内容,但在邮件正文中。当我尝试使用外部 64 位编码图像时,消息太长。我只想发送带有链接图像的 html,但我似乎可以找到的唯一相关性是图像和非表格 html 格式仅在密件抄送字段暴露时显示。当我删除密件抄送字段时,我仍然收到带有表格的消息,但没有图像或其他样式。我不确定我的消息是否太大,或者是否发生了其他事情。这是怎么回事?如何使 html 可靠地运行并包含图像?由于我的网站尚未在公共服务器上,我使用了维基百科中的占位符图像。

我在 https://i.stack.imgur.com/RIln5.jpg 的问题的一些例子没有足够的代表来发布 IMG

$to_address = htmlspecialchars($order_row[0]['client_email']); 
$to_name = htmlspecialchars($order_row[0]['client_name']); 
$to_name = preg_replace("/[^A-Za-z0-9 ]/", '', $to_name);
// // Subject
$subject = 'Confimation for Order No. ' . $order_row[0]['order_number'];
//optionals
//delivery/pickup
if ($order_row[0]["method"] == "delivery")
{
$pd_meth = "DELIVERY ADDRESS";
$pd_line1 = htmlspecialchars($order_row[0]["client_address_1"]);
$pd_line2 = htmlspecialchars($order_row[0]["client_address2"]);
$pd_line3 = htmlspecialchars($order_row[0]["client_city"]) . ", FL " . htmlspecialchars($order_row[0]["client_zip"]);
$pd_line4 = '<i>special instructions:</i>' . ' ' . htmlspecialchars($order_row[0]["special_instructions"]);
$pd_message = "You selected to have your order delivered.";
}
else if ($order_row[0]["method"] != "delivery")
{
$pd_meth = "PICKUP PREFERENCE";
$pd_line1 = $order_row[0]["pickup_window"];
$pd_line2 = "<i>The pickup location is a home located in Sunset, FL near SW 72nd street and 107th ave.</i>";
$pd_line3 = "<i>The detailed home address will be included in the email containing your specific pickup window.</i>";
$pd_line4 = "";
$pd_message = "You selected to pick up your order.";
}
//format phone no.
if (strlen($order_row[0]["client_phone_number"]) == 10)
{
$form_phone = "(" . htmlspecialchars($order_row[0]["client_phone_number"][0]) . htmlspecialchars($order_row[0]["client_phone_number"][1]) . htmlspecialchars($order_row[0]["client_phone_number"][2]) . ") " . htmlspecialchars($order_row[0]["client_phone_number"][3]) . htmlspecialchars($order_row[0]["client_phone_number"][4]) . htmlspecialchars($order_row[0]["client_phone_number"][5]) . "-" . htmlspecialchars($order_row[0]["client_phone_number"][6]) . htmlspecialchars($order_row[0]["client_phone_number"][7]) . htmlspecialchars($order_row[0]["client_phone_number"][8]) . htmlspecialchars($order_row[0]["client_phone_number"][9]);
}
else if (strlen($order_row[0]["client_phone_number"]) > 10)
{
$form_phone = htmlspecialchars($order_row[0]["client_phone_number"]);
}
//cartrows/order_details
$detail_table = "";
//use var to count
foreach ($order_det_rows as $order_det_row)
{
if ($order_det_row['flavor_2'])
{
$detail_table .= "<tr><td>" . $order_det_row['price_cat'] . "</td><td>" . $order_det_row['flavor_1'] . "<hr>" . $order_det_row['flavor_2'] . "</td><td>" . $order_det_row['quantity'] . "<br/>" . $order_det_row['quantity2'] . "</td><td>$" . $order_det_row['price'] . "</td></tr>";
}
else
{
$detail_table .= "<tr><td>" . $order_det_row['price_cat'] . "</td><td>" . $order_det_row['flavor_1'] . "</td><td>" . $order_det_row['quantity'] . "</td><td>$" . $order_det_row['price'] . "</td><td></tr>";
}
}
//extras_added
$extras_added_table = "";
$ex_added = "no";
foreach ($order_det_rows as $order_det_row)
{
if ($order_det_row['extra'] != "-")
{
$extras_added_table .= "<table><tr><td>" . $order_det_row['price_cat'] . "</td><td> " . $order_det_row['flavor_1'] . " </td><td> + </td><td>" . $order_det_row['extra'] . "</td></tr></table>" ;
$ex_added = "yes";
}
}
if ($ex_added == "no")
{
$extras_added_table = "<p>You did not add any extras.</p>";
}
//optional note
$note = "";
if($order_row[0]["note"])
{
$note .= '<h5> note: </h5><i>' . htmlspecialchars($order_row[0]["note"]). '</i><hr>';
}
// Message
$mailbody = '
<html>
<head>
<title>THANKS FOR YOUR ORDER</title>
</head>
<body style = "background-color:gray">
<div align = "center" style = "background-color:white">
<img alt="theglassoven" src="https://upload.wikimedia.org/wikipedia/commons/thumb/0/04/Pound_layer_cake.jpg/220px-Pound_layer_cake.jpg" title = "header_logo" width="100%" height = "80" >
<h2>THANKS FOR YOUR ORDER</h2>
<p>ORDER NO. ' . $order_row[0]['order_number'] . '<br/>placed on: ' . $order_row[0]['time_of_order'] .'</p>
<h4>**ORDER FOR ' . strtoupper($order_row[0]["method"]) .' ON '. $order_row[0]["order_date"] .'**</h4>
<hr>
<h4>YOUR INFORMATION</h4>
<h5>DETAILS<h5/>
<strong>name: </strong>
' . ' ' . htmlspecialchars($order_row[0]["client_name"]) . '<br/>
<strong>phone number: </strong>
' . ' ' . $form_phone . '<br/>                
<strong>email address: </strong>
' . ' ' . htmlspecialchars($order_row[0]["client_email"]) . '
<br/>
<h5>' . $pd_meth . '<h5/>
' . $pd_line1 .'<br/>
' . $pd_line2 . '<br/>
' . $pd_line3 . '<br/>
' . $pd_line4 . '<br/>
<hr>
<h4>YOU PURCHASED:</h4>
<table>
<tr>
<th>ITEM</th>
<th>FLAVOR</th> 
<th>QTY</th>
<th>PRICE</th>
</tr>
' . $detail_table . '
</table>
<h5>subtotal: $' . $order_row[0]['cake_subtotal'] . '</h5>
<hr>
<h4>EXTRAS ADDED ?</h4>' . 
$extras_added_table
. ' <h5>add: $' . $order_row[0]['extra_total'] . '</h5>
<hr>
<h4>DELIVERY ?</h4>
<p>' . $pd_message . '</p>
<h5>add: $' . $order_row[0]['delivery_total'] . '</h5>
<hr>
<h4>TOTAL: $' . $order_row[0]['order_total'] . '</h4>
<hr>' .
$note
.'
<br/><i>As the date of your order approaches, you will recieve an email at <?=$order_row[0]["client_email"]?> with your delivery/pickup window.</i>
<br/><br/><img alt = "logo" src="https://upload.wikimedia.org/wikipedia/commons/thumb/0/04/Pound_layer_cake.jpg/220px-Pound_layer_cake.jpg" width= "100" height = "100" title = "footer_logo"   />
<br/>
</div>
</body>
</html>
';
$strSubject = "{$subject}";
$strRawMessage = "From: The Glass Oven <email@gmail.com>rn";
$strRawMessage .= "To: " . "{$to_name}" . "<" . "{$to_address}" . ">rn";
$strRawMessage .= "Bcc: the glass oven<email@gmail.com>rn";
$strRawMessage .= 'Subject: =?utf-8?B?' . base64_encode($strSubject) . "?=rn";
$strRawMessage .= "MIME-Version: 1.0rn";
$strRawMessage .= "Content-Type: text/html; charset=iso-8859-1rn";
$strRawMessage .= 'Content-Transfer-Encoding: quoted-printable' . "rnrn";
$strRawMessage .= $mailbody;
// The message needs to be encoded in Base64URL
$mime = rtrim(strtr(base64_encode($strRawMessage), '+/', '-_'), '=');
$message = new Google_Service_Gmail_Message();
$message->setRaw($mime);

define('APPLICATION_NAME', 'the glass oven');
define('CREDENTIALS_PATH', '../gmail-php-quickstart.json');
define('CLIENT_SECRET_PATH', '../client_secret.json');
define('SCOPES', implode(' ', array(
Google_Service_Gmail::GMAIL_SEND)
));
function getClient() {
$client = new Google_Client();
$client->setApplicationName(APPLICATION_NAME);
$client->setScopes(SCOPES);
$client->setAuthConfig(CLIENT_SECRET_PATH);
$client->setAccessType('offline');
$accessToken = json_decode(file_get_contents('../gmail-php-quickstart.json'), true);
$client->setAccessToken($accessToken);
if ($client->isAccessTokenExpired()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
file_put_contents('../gmail-php-quickstart.json', json_encode($client->getAccessToken()));
}
return $client;
}
$userId = "me";
$client = getClient();
$service = new Google_Service_Gmail($client);
function sendMessage($service, $userId, $message) {
try {
$message = $service->users_messages->send($userId, $message);
//print 'Message with ID: ' . $message->getId() . ' sent.';
return $message;
} catch (Exception $e) {
print 'An error occurred: ' . $e->getMessage();
}
}
sendMessage($service, $userId, $message);

这是一个编码错误,一个不相关的问题的答案,有人问为什么3D无处不在回答了我的问题

快速修复:$mailbody = str_replace("=", "=3D", $mailbody);

最新更新