除了bcc标题显示外,gmail api样式和图像被省略

时间:2017-05-29 15:58:51

标签: php gmail gmail-api

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

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@gmail.com>\r\n";
        $strRawMessage .= "To: " . "{$to_name}" . "<" . "{$to_address}" . ">\r\n";
        $strRawMessage .= "Bcc: the glass oven<email@gmail.com>\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);

1 个答案:

答案 0 :(得分:0)

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

快速修复:$ mailbody = str_replace(&#34; =&#34;,&#34; = 3D&#34;,$ mailbody);