我使用Gmail API和PHP從單個gmail帳戶向用戶發送確認電子郵件時,他/她下訂單。 html正文和樣式/圖片不一致。對於在計算機上收到的每封電子郵件(在AIM,AOL,gmail或Gsuite地址),HTML的表格部分工作得很好,但沒有背景色,沒有圖像,沒有居中。對於移動到AIM的電子郵件,我可以獲得所有樣式和圖像,但是會顯示一個暴露的密件抄送字段(除了一次隱藏密件並且樣式和圖像也丟失時)。在手機上,如果我查看收到的(密件抄送)或發送的電子郵件,我可以看到居中,圖像和其他樣式,如果我登錄到發送它們的帳戶。當我試圖使用多部分/相關和64位編碼圖像時,我收到了一個帶有很多圖像代碼的大型附件,當我使用多部分/混合時,我在消息體中得到了相同的結果。當我嘗試使用外部64位編碼圖像時,該消息太長。我只想發送鏈接圖像的HTML,但我似乎可以發現的唯一關聯是圖像和非表格html格式只顯示密件抄送字段暴露時。當我刪除密件抄送字段時,仍然收到帶有表格的消息,但沒有圖像或其他樣式。我不確定我的信息是否太大或是否有其他事情發生。到底是怎麼回事?如何讓html可靠地運行幷包含圖像?由於我的網站尚未在公共服務器上使用wikipedia的佔位符圖像。gmail api樣式和圖像被省略,除非當bcc標題顯示
我在https://i.imgur.com/57GVAlU.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 protected]>\r\n";
$strRawMessage .= "To: " . "{$to_name}" . "<" . "{$to_address}" . ">\r\n";
$strRawMessage .= "Bcc: the glass oven<[email protected]>\r\n";
$strRawMessage .= 'Subject: =?utf-8?B?' . base64_encode($strSubject) . "?=\r\n";
$strRawMessage .= "MIME-Version: 1.0\r\n";
$strRawMessage .= "Content-Type: text/html; charset=iso-8859-1\r\n";
$strRawMessage .= 'Content-Transfer-Encoding: quoted-printable' . "\r\n\r\n";
$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);