title: gmail api styling and images omitted except when bcc header shows
body: <p>I'm using Gmail API and PHP to send a confirmation email from a single gmail account to the user when a he/she places an order. The html body and style/image are being inconsistent. For each received email (on AIM, AOL, gmail, or Gsuite addresses) on a computer the table part of the HTML works just fine, but no background-color, no images, no centering. For emails on mobile to AIM, I get all the styling and images but an exposed Bcc field (except for one time when the Bcc was hidden and the styling and image were also missing). On mobile, if i look at either the received (Bcc) or sent emails, I can see the centering, images, and other styling if I am logged into the account from which they were sent. When I tried to use multipart/related and 64-bit encoded image, I received one large attachment with a lot the image code, when I used multipart/mixed, I got the same but in the message body. When I tried using an external 64-bit coded image, the message was too long. I would like to just send html with linked images, but the only correlation I can seem to find is that the images and non-table html formatting only show when the Bcc field is exposed. When I removed Bcc field, I still get the message with table but no images or other styling. I'm not sure if maybe my messages are too large or if something else is happening. What is going on? How do I get the html to function reliably and include an image? As my website is not yet on a public server I used placeholder images from wikipedia.</p>
  
  <p>some examples of my problem at <a href="https://i.imgur.com/57GVAlU.jpg" rel="nofollow noreferrer">https://i.imgur.com/57GVAlU.jpg</a> don't have enough rep to post img</p>
  
  <pre><code>        $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 = '&lt;i&gt;special instructions:&lt;/i&gt;' . ' ' . 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 = "&lt;i&gt;The pickup location is a home located in Sunset, FL near SW 72nd street and 107th ave.&lt;/i&gt;";
              $pd_line3 = "&lt;i&gt;The detailed home address will be included in the email containing your specific pickup window.&lt;/i&gt;";
              $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"]) &gt; 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 .= "&lt;tr&gt;&lt;td&gt;" . $order_det_row['price_cat'] . "&lt;/td&gt;&lt;td&gt;" . $order_det_row['flavor_1'] . "&lt;hr&gt;" . $order_det_row['flavor_2'] . "&lt;/td&gt;&lt;td&gt;" . $order_det_row['quantity'] . "&lt;br/&gt;" . $order_det_row['quantity2'] . "&lt;/td&gt;&lt;td&gt;$" . $order_det_row['price'] . "&lt;/td&gt;&lt;/tr&gt;";
                  }
                  else
                  {
                      $detail_table .= "&lt;tr&gt;&lt;td&gt;" . $order_det_row['price_cat'] . "&lt;/td&gt;&lt;td&gt;" . $order_det_row['flavor_1'] . "&lt;/td&gt;&lt;td&gt;" . $order_det_row['quantity'] . "&lt;/td&gt;&lt;td&gt;$" . $order_det_row['price'] . "&lt;/td&gt;&lt;td&gt;&lt;/tr&gt;";
                  }
              }
          //extras_added
          $extras_added_table = "";
          $ex_added = "no";
              foreach ($order_det_rows as $order_det_row)
              {
                  if ($order_det_row['extra'] != "-")
                  {
                      $extras_added_table .= "&lt;table&gt;&lt;tr&gt;&lt;td&gt;" . $order_det_row['price_cat'] . "&lt;/td&gt;&lt;td&gt; " . $order_det_row['flavor_1'] . " &lt;/td&gt;&lt;td&gt; + &lt;/td&gt;&lt;td&gt;" . $order_det_row['extra'] . "&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;" ;
                      $ex_added = "yes";
                  }
              }
              if ($ex_added == "no")
              {
                  $extras_added_table = "&lt;p&gt;You did not add any extras.&lt;/p&gt;";
              }
          //optional note
          $note = "";
          if($order_row[0]["note"])
          {
              $note .= '&lt;h5&gt; note: &lt;/h5&gt;&lt;i&gt;' . htmlspecialchars($order_row[0]["note"]). '&lt;/i&gt;&lt;hr&gt;';
          }
  
          // Message
          $mailbody = '
          &lt;html&gt;
          &lt;head&gt;
            &lt;title&gt;THANKS FOR YOUR ORDER&lt;/title&gt;
          &lt;/head&gt;
          &lt;body style = "background-color:gray"&gt;
          &lt;div align = "center" style = "background-color:white"&gt;
          &lt;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" &gt;
          &lt;h2&gt;THANKS FOR YOUR ORDER&lt;/h2&gt;
              &lt;p&gt;ORDER NO. ' . $order_row[0]['order_number'] . '&lt;br/&gt;placed on: ' . $order_row[0]['time_of_order'] .'&lt;/p&gt;
              &lt;h4&gt;**ORDER FOR ' . strtoupper($order_row[0]["method"]) .' ON '. $order_row[0]["order_date"] .'**&lt;/h4&gt;
              &lt;hr&gt;
              &lt;h4&gt;YOUR INFORMATION&lt;/h4&gt;
              &lt;h5&gt;DETAILS&lt;h5/&gt;
              &lt;strong&gt;name: &lt;/strong&gt;
              ' . ' ' . htmlspecialchars($order_row[0]["client_name"]) . '&lt;br/&gt;
              &lt;strong&gt;phone number: &lt;/strong&gt;
              ' . ' ' . $form_phone . '&lt;br/&gt;                
              &lt;strong&gt;email address: &lt;/strong&gt;
              ' . ' ' . htmlspecialchars($order_row[0]["client_email"]) . '
              &lt;br/&gt;
              &lt;h5&gt;' . $pd_meth . '&lt;h5/&gt;
              ' . $pd_line1 .'&lt;br/&gt;
              ' . $pd_line2 . '&lt;br/&gt;
              ' . $pd_line3 . '&lt;br/&gt;
              ' . $pd_line4 . '&lt;br/&gt;
  
              &lt;hr&gt;
              &lt;h4&gt;YOU PURCHASED:&lt;/h4&gt;
              &lt;table&gt;
                  &lt;tr&gt;
                      &lt;th&gt;ITEM&lt;/th&gt;
                      &lt;th&gt;FLAVOR&lt;/th&gt; 
                      &lt;th&gt;QTY&lt;/th&gt;
                      &lt;th&gt;PRICE&lt;/th&gt;
                  &lt;/tr&gt;
                  ' . $detail_table . '
              &lt;/table&gt;
                  &lt;h5&gt;subtotal: $' . $order_row[0]['cake_subtotal'] . '&lt;/h5&gt;
              &lt;hr&gt;
              &lt;h4&gt;EXTRAS ADDED ?&lt;/h4&gt;' . 
                  $extras_added_table
              . ' &lt;h5&gt;add: $' . $order_row[0]['extra_total'] . '&lt;/h5&gt;
              &lt;hr&gt;
              &lt;h4&gt;DELIVERY ?&lt;/h4&gt;
              &lt;p&gt;' . $pd_message . '&lt;/p&gt;
              &lt;h5&gt;add: $' . $order_row[0]['delivery_total'] . '&lt;/h5&gt;
              &lt;hr&gt;
              &lt;h4&gt;TOTAL: $' . $order_row[0]['order_total'] . '&lt;/h4&gt;
              &lt;hr&gt;' .
              $note
              .'
          &lt;br/&gt;&lt;i&gt;As the date of your order approaches, you will recieve an email at &lt;?=$order_row[0]["client_email"]?&gt; with your delivery/pickup window.&lt;/i&gt;
          &lt;br/&gt;&lt;br/&gt;&lt;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"   /&gt;
          &lt;br/&gt;
          &lt;/div&gt;
          &lt;/body&gt;
          &lt;/html&gt;
          ';
  
          $strSubject = "{$subject}";
          $strRawMessage = "From: The Glass Oven &lt;email@gmail.com&gt;\r\n";
          $strRawMessage .= "To: " . "{$to_name}" . "&lt;" . "{$to_address}" . "&gt;\r\n";
          $strRawMessage .= "Bcc: the glass oven&lt;email@gmail.com&gt;\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-&gt;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-&gt;setApplicationName(APPLICATION_NAME);
          $client-&gt;setScopes(SCOPES);
          $client-&gt;setAuthConfig(CLIENT_SECRET_PATH);
          $client-&gt;setAccessType('offline');
          $accessToken = json_decode(file_get_contents('../gmail-php-quickstart.json'), true);
          $client-&gt;setAccessToken($accessToken);
          if ($client-&gt;isAccessTokenExpired()) {
              $client-&gt;fetchAccessTokenWithRefreshToken($client-&gt;getRefreshToken());
              file_put_contents('../gmail-php-quickstart.json', json_encode($client-&gt;getAccessToken()));
            }
          return $client;
      }
  
      $userId = "me";
      $client = getClient();
      $service = new Google_Service_Gmail($client);
  
      function sendMessage($service, $userId, $message) {
          try {
              $message = $service-&gt;users_messages-&gt;send($userId, $message);
              //print 'Message with ID: ' . $message-&gt;getId() . ' sent.';
              return $message;
            } catch (Exception $e) {
              print 'An error occurred: ' . $e-&gt;getMessage();
            }
          }
  
      sendMessage($service, $userId, $message);
  </code></pre>