2017-05-29 36 views
0

我使用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); 

回答

0

它是与编码错误时,答案不相关的问题,如果有人问,为什么3D的到处回答我问题

quick fix:$ mailbody = str_replace(“=”,“= 3D”,$ mailbody);