{"id":10092,"date":"2019-10-17T09:02:07","date_gmt":"2019-10-17T09:02:07","guid":{"rendered":"https:\/\/www.gmass.co\/blog\/?p=10092"},"modified":"2021-04-27T07:49:52","modified_gmt":"2021-04-27T07:49:52","slug":"gmail-api-html-plain-text-messages","status":"publish","type":"post","link":"https:\/\/www.gmass.co\/blog\/gmail-api-html-plain-text-messages\/","title":{"rendered":"The Gmail API&#8217;s quirky handling of HTML and Plain Text messages parts"},"content":{"rendered":"<p>If you&#8217;re using the Gmail API to send email, specifically using <a href=\"https:\/\/developers.google.com\/gmail\/api\/v1\/reference\/users\/messages\/send\">messages.send<\/a>, you will likely notice some <strong>unexpected behavior<\/strong> with respect to <strong>HTML<\/strong> and <strong>Plain Text<\/strong> message parts. Like all Gmail API methods that deal with an actual email message, emails must be <strong>Base64 encoded<\/strong> before being submitted to the API, so my examples below will show the readable RFC-compliant message and the Base64 versions.<\/p>\n<p>If you&#8217;re not interested in the details, here are the important things to know regarding sending emails with the Gmail API:<\/p>\n<ol>\n<li>You <em><strong>can<\/strong><\/em> send just a <strong>plain text email<\/strong> with the Gmail API, meaning there&#8217;s just one &#8220;text\/plain&#8221; MIME part, and that&#8217;s it.<\/li>\n<li>You <strong><em>cannot<\/em><\/strong> send <strong>just an HTML email<\/strong> with the Gmail API, meaning you cannot send a &#8220;text\/html&#8221; part <strong><em>without<\/em><\/strong> a corresponding &#8220;text\/plain&#8221; part. If you try to do this, the API will accept the message, but behind the scenes, it will convert your &#8220;text\/html&#8221; part to a &#8220;text\/plain&#8221; part and add that to the message before it sends the email.<\/li>\n<li>\u00a0Setting individual <strong>HTML and Plain Text parts<\/strong> when constructing your RFC message is <em><strong>useless<\/strong><\/em>\u00a0because the <strong>Gmail API will override the Plain Text part with its own Plain Text part<\/strong>, using the HTML part as its basis. Let&#8217;s say you construct your email with an <strong>HTML part<\/strong> that looks like &#8220;&lt;P&gt;hello there&lt;\/P&gt;&#8221; and a <strong>Plain Text part<\/strong> that looks like &#8220;hi there.&#8221; (Notice that one part says &#8220;hello,&#8221; and the other says &#8220;hi.&#8221;) When the Gmail API sends your email, your Plain Text part will be wiped out and replaced with a part that is converted from your HTML part. The actual Plain Text part that gets sent will say, &#8220;hello there.&#8221;<\/li>\n<li>Lastly, it&#8217;s important to know that the message that appears in your account&#8217;s &#8220;Sent Mail&#8221; folder is <strong><em>not the same message that is sent<\/em><\/strong> to the receiver. In example #2 above, if you try to do this, the &#8220;Sent Mail&#8221; email will show just an HTML part, but the <strong><em>actual<\/em><\/strong> received email will have both HTML and Plain Text parts. In example #3 above, if you set your own Plain Text part, the &#8220;Sent Mail&#8221; email will show your HTML parts and Plain Text parts just as you constructed them for the Gmail API, but again, the actual received email will have a different Plain Text part that was auto-generated by Gmail from your HTML part. This can fool even the most experienced developer, so it&#8217;s important that you don&#8217;t rely on what&#8217;s in your &#8220;Sent Mail&#8221; folder as proof of the email that was actually sent. This is likely a flaw in the API, where it mistakenly writes the email to the user&#8217;s &#8220;Sent Mail&#8221; folder before sanitizing and sending the email. The better approach would be to sanitize the email, then write it to the user&#8217;s &#8220;Sent Mail&#8221; folder, and simultaneously transmit the email.<\/li>\n<li>If you choose not to &#8220;send&#8221; an email, but instead to &#8220;insert&#8221; the email into someone&#8217;s Inbox via the <a href=\"https:\/\/developers.google.com\/gmail\/api\/v1\/reference\/users\/messages\/insert\">Users.messages.insert<\/a> API method, then you <strong>can<\/strong> set completely different HTML and Text parts and Gmail won&#8217;t alter them at all. You can only do this though, if you have OAuth access to the recipient&#8217;s Gmail account.<\/li>\n<li>Behavior with DRAFTs is similar. If you call <a href=\"https:\/\/developers.google.com\/gmail\/api\/reference\/rest\/v1\/users.drafts\/create\">users.drafts.create<\/a> and you pass in just a &#8220;plain text&#8221; part or just an HTML part, then the DRAFT will get created properly with just the part you specified. Meaning, you can have a DRAFT containing just a plain text part, or you can have a DRAFT containing just an HTML part, or you can have a DRAFT containing both HTML and plain text parts. If you then call <a href=\"https:\/\/developers.google.com\/gmail\/api\/reference\/rest\/v1\/users.drafts\/send\">users.drafts.send<\/a> on your DRAFT object, that&#8217;s where things get funky. If your DRAFT object has just a plain text part or both HTML and plain text parts, the email will be sent and received as expected. If, however, the DRAFT object contains <em>only<\/em> an HTML part, then your Sent folder will show the email as having sent with just the HTML part, but in actuality, the email will be received with both HTML and plain text parts, with the plain text part being auto-generated by Gmail.<\/li>\n<\/ol>\n<h3>The proof is in the pudding<\/h3>\n<p>Now that you know the quirks of sending email with the Gmail API, let me show you some examples as proof. In code, I use Jeffrey Stedfast&#8217;s MimeKit framework to assemble and parse messages, but for the examples below, I&#8217;ll skip showing off the code that generated each message in favor of showing the end result message and Base64 encoded versions. Additionally, if you&#8217;re going to use an online Base64 encoder\/decoder to test the API yourself, I recommend <a href=\"https:\/\/base64.guru\/converter\/decode\">this one<\/a>. It&#8217;s the only one that gets the encoding right so that it works with the Gmail API, and it&#8217;s the only one that properly decodes the Base64 string created by MimeKit.<\/p>\n<p>In these examples, I&#8217;m sending email from my G Suite account, ajay@wordzen.com, to a test account, gmass100@silicomm.com, that receives mail on a server that I control.<\/p>\n<ol>\n<li><strong>Sending just a plain text email<br \/>\n<\/strong><br \/>\nThe RFC message:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-10103\" src=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/RFC-message-plain-text-email-42k.png\" alt=\"\" width=\"1224\" height=\"432\" srcset=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/RFC-message-plain-text-email-42k.png 1224w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/RFC-message-plain-text-email-42k-300x106.png 300w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/RFC-message-plain-text-email-42k-768x271.png 768w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/RFC-message-plain-text-email-42k-1024x361.png 1024w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/RFC-message-plain-text-email-42k-24x8.png 24w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/RFC-message-plain-text-email-42k-36x13.png 36w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/RFC-message-plain-text-email-42k-48x17.png 48w\" sizes=\"auto, (max-width: 1224px) 100vw, 1224px\" \/>The Base64 encoded version:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-10104\" src=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/Base64-encoded-plain-text-email-48k.png\" alt=\"\" width=\"1224\" height=\"220\" srcset=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/Base64-encoded-plain-text-email-48k.png 1224w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/Base64-encoded-plain-text-email-48k-300x54.png 300w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/Base64-encoded-plain-text-email-48k-768x138.png 768w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/Base64-encoded-plain-text-email-48k-1024x184.png 1024w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/Base64-encoded-plain-text-email-48k-24x4.png 24w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/Base64-encoded-plain-text-email-48k-36x6.png 36w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/Base64-encoded-plain-text-email-48k-48x9.png 48w\" sizes=\"auto, (max-width: 1224px) 100vw, 1224px\" \/>Submission to the Gmail API:<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-10106\" src=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/Subm-to-Gmail-API-plain-text-email-120k.png\" alt=\"\" width=\"300\" height=\"360\" srcset=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/Subm-to-Gmail-API-plain-text-email-120k.png 756w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/Subm-to-Gmail-API-plain-text-email-120k-250x300.png 250w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/Subm-to-Gmail-API-plain-text-email-120k-20x24.png 20w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/Subm-to-Gmail-API-plain-text-email-120k-30x36.png 30w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/Subm-to-Gmail-API-plain-text-email-120k-40x48.png 40w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/>What appears in the &#8220;Sent Mail&#8221; folder of ajay@wordzen.com:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-10105\" src=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/Sent-mail-folder-plain-text-email-53K.png\" alt=\"\" width=\"1224\" height=\"788\" srcset=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/Sent-mail-folder-plain-text-email-53K.png 1224w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/Sent-mail-folder-plain-text-email-53K-300x193.png 300w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/Sent-mail-folder-plain-text-email-53K-768x494.png 768w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/Sent-mail-folder-plain-text-email-53K-1024x659.png 1024w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/Sent-mail-folder-plain-text-email-53K-24x15.png 24w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/Sent-mail-folder-plain-text-email-53K-36x23.png 36w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/Sent-mail-folder-plain-text-email-53K-48x31.png 48w\" sizes=\"auto, (max-width: 1224px) 100vw, 1224px\" \/>What is received by the server for gmass100@silicomm.com:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-10107\" src=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/GMass-server-plain-text-email-187k.png\" alt=\"\" width=\"1224\" height=\"1000\" srcset=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/GMass-server-plain-text-email-187k.png 1224w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/GMass-server-plain-text-email-187k-300x245.png 300w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/GMass-server-plain-text-email-187k-768x627.png 768w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/GMass-server-plain-text-email-187k-1024x837.png 1024w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/GMass-server-plain-text-email-187k-24x20.png 24w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/GMass-server-plain-text-email-187k-36x29.png 36w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/GMass-server-plain-text-email-187k-48x39.png 48w\" sizes=\"auto, (max-width: 1224px) 100vw, 1224px\" \/>Conclusion: it works as expected!<\/li>\n<li><strong>Sending an email with just an HTML part and NO plain text part<\/strong>The RFC message:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-10111\" src=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/1-RFC-message-just-html-50k.png\" alt=\"\" width=\"1224\" height=\"484\" srcset=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/1-RFC-message-just-html-50k.png 1224w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/1-RFC-message-just-html-50k-300x119.png 300w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/1-RFC-message-just-html-50k-768x304.png 768w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/1-RFC-message-just-html-50k-1024x405.png 1024w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/1-RFC-message-just-html-50k-24x9.png 24w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/1-RFC-message-just-html-50k-36x14.png 36w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/1-RFC-message-just-html-50k-48x19.png 48w\" sizes=\"auto, (max-width: 1224px) 100vw, 1224px\" \/>The Base64 encoded version:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-10112\" src=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/2-Base64-encoded-just-html-73k.png\" alt=\"\" width=\"1224\" height=\"220\" srcset=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/2-Base64-encoded-just-html-73k.png 1224w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/2-Base64-encoded-just-html-73k-300x54.png 300w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/2-Base64-encoded-just-html-73k-768x138.png 768w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/2-Base64-encoded-just-html-73k-1024x184.png 1024w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/2-Base64-encoded-just-html-73k-24x4.png 24w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/2-Base64-encoded-just-html-73k-36x6.png 36w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/2-Base64-encoded-just-html-73k-48x9.png 48w\" sizes=\"auto, (max-width: 1224px) 100vw, 1224px\" \/>Submission to the Gmail API:<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-10113\" src=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/3-Subm-to-Gmail-API-just-html-121k.png\" alt=\"\" width=\"304\" height=\"366\" srcset=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/3-Subm-to-Gmail-API-just-html-121k.png 756w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/3-Subm-to-Gmail-API-just-html-121k-250x300.png 250w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/3-Subm-to-Gmail-API-just-html-121k-20x24.png 20w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/3-Subm-to-Gmail-API-just-html-121k-30x36.png 30w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/3-Subm-to-Gmail-API-just-html-121k-40x48.png 40w\" sizes=\"auto, (max-width: 304px) 100vw, 304px\" \/><br \/>\nWhat appears in &#8220;Sent Mail&#8221;:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-10114\" src=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/4-Sent-mail-folder-just-html-58k.png\" alt=\"\" width=\"1224\" height=\"788\" srcset=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/4-Sent-mail-folder-just-html-58k.png 1224w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/4-Sent-mail-folder-just-html-58k-300x193.png 300w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/4-Sent-mail-folder-just-html-58k-768x494.png 768w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/4-Sent-mail-folder-just-html-58k-1024x659.png 1024w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/4-Sent-mail-folder-just-html-58k-24x15.png 24w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/4-Sent-mail-folder-just-html-58k-36x23.png 36w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/4-Sent-mail-folder-just-html-58k-48x31.png 48w\" sizes=\"auto, (max-width: 1224px) 100vw, 1224px\" \/>What is received by the server:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-10115\" src=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/5-GMass-server-just-html-220k.png\" alt=\"\" width=\"1224\" height=\"1200\" srcset=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/5-GMass-server-just-html-220k.png 1224w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/5-GMass-server-just-html-220k-300x294.png 300w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/5-GMass-server-just-html-220k-768x753.png 768w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/5-GMass-server-just-html-220k-1024x1004.png 1024w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/5-GMass-server-just-html-220k-24x24.png 24w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/5-GMass-server-just-html-220k-36x36.png 36w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/5-GMass-server-just-html-220k-48x48.png 48w\" sizes=\"auto, (max-width: 1224px) 100vw, 1224px\" \/><\/p>\n<p>Conclusion: I submit the email with just an HTML part, but the Gmail API forces a plain text part. Additionally, the &#8220;Sent Mail&#8221; folder deceptively shows that the email was sent with just an HTML part when it was actually sent with both parts.<\/li>\n<li>Sending an email with <strong>both HTML and plain text parts<\/strong>, but where we set our own plain text partThe RFC Message:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-10120\" src=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/1-RFC-message-html-and-plain-text-72k.png\" alt=\"\" width=\"1224\" height=\"585\" srcset=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/1-RFC-message-html-and-plain-text-72k.png 1224w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/1-RFC-message-html-and-plain-text-72k-300x143.png 300w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/1-RFC-message-html-and-plain-text-72k-768x367.png 768w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/1-RFC-message-html-and-plain-text-72k-1024x489.png 1024w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/1-RFC-message-html-and-plain-text-72k-24x11.png 24w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/1-RFC-message-html-and-plain-text-72k-36x17.png 36w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/1-RFC-message-html-and-plain-text-72k-48x23.png 48w\" sizes=\"auto, (max-width: 1224px) 100vw, 1224px\" \/>Base64 encoded:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-10121\" src=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/2-Base64-encoded-html-and-plain-text-106k.png\" alt=\"\" width=\"1224\" height=\"304\" srcset=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/2-Base64-encoded-html-and-plain-text-106k.png 1224w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/2-Base64-encoded-html-and-plain-text-106k-300x75.png 300w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/2-Base64-encoded-html-and-plain-text-106k-768x191.png 768w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/2-Base64-encoded-html-and-plain-text-106k-1024x254.png 1024w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/2-Base64-encoded-html-and-plain-text-106k-24x6.png 24w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/2-Base64-encoded-html-and-plain-text-106k-36x9.png 36w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/2-Base64-encoded-html-and-plain-text-106k-48x12.png 48w\" sizes=\"auto, (max-width: 1224px) 100vw, 1224px\" \/>Submission to the API:<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-10122\" src=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/3-Subm-to-Gmail-API-html-and-plain-text-121k.png\" alt=\"\" width=\"338\" height=\"396\" srcset=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/3-Subm-to-Gmail-API-html-and-plain-text-121k.png 756w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/3-Subm-to-Gmail-API-html-and-plain-text-121k-256x300.png 256w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/3-Subm-to-Gmail-API-html-and-plain-text-121k-20x24.png 20w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/3-Subm-to-Gmail-API-html-and-plain-text-121k-31x36.png 31w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/3-Subm-to-Gmail-API-html-and-plain-text-121k-41x48.png 41w\" sizes=\"auto, (max-width: 338px) 100vw, 338px\" \/><br \/>\nWhat appears in &#8220;Sent Mail&#8221;:<a href=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/4-Sent-mail-folder-html-and-plain-text-71k.png\" data-rel=\"lightbox-image-0\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-10123 size-full\" src=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/4-Sent-mail-folder-html-and-plain-text-71k.png\" alt=\"\" width=\"1224\" height=\"891\" srcset=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/4-Sent-mail-folder-html-and-plain-text-71k.png 1224w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/4-Sent-mail-folder-html-and-plain-text-71k-300x218.png 300w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/4-Sent-mail-folder-html-and-plain-text-71k-768x559.png 768w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/4-Sent-mail-folder-html-and-plain-text-71k-1024x745.png 1024w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/4-Sent-mail-folder-html-and-plain-text-71k-24x17.png 24w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/4-Sent-mail-folder-html-and-plain-text-71k-36x26.png 36w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/4-Sent-mail-folder-html-and-plain-text-71k-48x35.png 48w\" sizes=\"auto, (max-width: 1224px) 100vw, 1224px\" \/><\/a>What is actually received:<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-10124\" src=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/5-GMass-server-html-and-plain-text-200k.png\" alt=\"\" width=\"1224\" height=\"1200\" srcset=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/5-GMass-server-html-and-plain-text-200k.png 1224w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/5-GMass-server-html-and-plain-text-200k-300x294.png 300w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/5-GMass-server-html-and-plain-text-200k-768x753.png 768w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/5-GMass-server-html-and-plain-text-200k-1024x1004.png 1024w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/5-GMass-server-html-and-plain-text-200k-24x24.png 24w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/5-GMass-server-html-and-plain-text-200k-36x36.png 36w, https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/5-GMass-server-html-and-plain-text-200k-48x48.png 48w\" sizes=\"auto, (max-width: 1224px) 100vw, 1224px\" \/>Conclusion: The Gmail API replaces the plain text part that I&#8217;ve set with its own that is converted from the HTML part.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;re using the Gmail API to send email, specifically using messages.send, you will likely notice some unexpected behavior with respect to HTML and Plain Text message parts.\u2026<\/p>\n","protected":false},"author":2,"featured_media":10101,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[3462,5916],"tags":[],"class_list":["post-10092","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","category-gmail-api"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\r\n<title>The Gmail API&#039;s quirky handling of HTML and Plain Text messages parts<\/title>\r\n<meta name=\"description\" content=\"The Gmail API is tricky with plain text messages. It forces a plain text MIME part when you don&#039;t want one, and overwrites your plain text with its own.\" \/>\r\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/www.gmass.co\/blog\/gmail-api-html-plain-text-messages\/\" \/>\r\n<meta property=\"og:locale\" content=\"en_US\" \/>\r\n<meta property=\"og:type\" content=\"article\" \/>\r\n<meta property=\"og:title\" content=\"The Gmail API&#039;s quirky handling of HTML and Plain Text messages parts\" \/>\r\n<meta property=\"og:description\" content=\"The Gmail API is tricky with plain text messages. It forces a plain text MIME part when you don&#039;t want one, and overwrites your plain text with its own.\" \/>\r\n<meta property=\"og:url\" content=\"https:\/\/www.gmass.co\/blog\/gmail-api-html-plain-text-messages\/\" \/>\r\n<meta property=\"og:site_name\" content=\"GMass Blog\" \/>\r\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/GmailMailMerge\/\" \/>\r\n<meta property=\"article:published_time\" content=\"2019-10-17T09:02:07+00:00\" \/>\r\n<meta property=\"article:modified_time\" content=\"2021-04-27T07:49:52+00:00\" \/>\r\n<meta property=\"og:image\" content=\"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/Gmail-quirky-hi-hello-45k.png\" \/>\r\n\t<meta property=\"og:image:width\" content=\"1001\" \/>\r\n\t<meta property=\"og:image:height\" content=\"467\" \/>\r\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\r\n<meta name=\"author\" content=\"Ajay Goel\" \/>\r\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\r\n<meta name=\"twitter:creator\" content=\"@PartTimeSnob\" \/>\r\n<meta name=\"twitter:site\" content=\"@GMassForGmail\" \/>\r\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ajay Goel\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\r\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/gmail-api-html-plain-text-messages\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/gmail-api-html-plain-text-messages\\\/\"},\"author\":{\"name\":\"Ajay Goel\",\"@id\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/#\\\/schema\\\/person\\\/b5fa74f8765b860701158fd77162fff8\"},\"headline\":\"The Gmail API&#8217;s quirky handling of HTML and Plain Text messages parts\",\"datePublished\":\"2019-10-17T09:02:07+00:00\",\"dateModified\":\"2021-04-27T07:49:52+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/gmail-api-html-plain-text-messages\\\/\"},\"wordCount\":1074,\"commentCount\":3,\"image\":{\"@id\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/gmail-api-html-plain-text-messages\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/10\\\/Gmail-quirky-hi-hello-45k.png\",\"articleSection\":[\"Developers\",\"Gmail API\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.gmass.co\\\/blog\\\/gmail-api-html-plain-text-messages\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/gmail-api-html-plain-text-messages\\\/\",\"url\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/gmail-api-html-plain-text-messages\\\/\",\"name\":\"The Gmail API's quirky handling of HTML and Plain Text messages parts\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/gmail-api-html-plain-text-messages\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/gmail-api-html-plain-text-messages\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/10\\\/Gmail-quirky-hi-hello-45k.png\",\"datePublished\":\"2019-10-17T09:02:07+00:00\",\"dateModified\":\"2021-04-27T07:49:52+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/#\\\/schema\\\/person\\\/b5fa74f8765b860701158fd77162fff8\"},\"description\":\"The Gmail API is tricky with plain text messages. It forces a plain text MIME part when you don't want one, and overwrites your plain text with its own.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/gmail-api-html-plain-text-messages\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.gmass.co\\\/blog\\\/gmail-api-html-plain-text-messages\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/gmail-api-html-plain-text-messages\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/10\\\/Gmail-quirky-hi-hello-45k.png\",\"contentUrl\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/10\\\/Gmail-quirky-hi-hello-45k.png\",\"width\":1001,\"height\":467},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/gmail-api-html-plain-text-messages\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"The Gmail API&#8217;s quirky handling of HTML and Plain Text messages parts\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/\",\"name\":\"GMass Blog\",\"description\":\"Tips and tricks for sending mail merge and mass email campaigns directly from Gmail\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/#\\\/schema\\\/person\\\/b5fa74f8765b860701158fd77162fff8\",\"name\":\"Ajay Goel\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f3a70af05bbabe47925150d5a1320540e801b78055a74da20658fc97cd0706a2?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f3a70af05bbabe47925150d5a1320540e801b78055a74da20658fc97cd0706a2?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f3a70af05bbabe47925150d5a1320540e801b78055a74da20658fc97cd0706a2?s=96&d=mm&r=g\",\"caption\":\"Ajay Goel\"},\"description\":\"Ajay is the founder of GMass and has been developing email sending software for 20 years.\",\"sameAs\":[\"https:\\\/\\\/twitter.com\\\/PartTimeSnob\",\"https:\\\/\\\/x.com\\\/PartTimeSnob\"],\"url\":\"https:\\\/\\\/www.gmass.co\\\/blog\\\/author\\\/ajay-goel\\\/\"}]}<\/script>\r\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"The Gmail API's quirky handling of HTML and Plain Text messages parts","description":"The Gmail API is tricky with plain text messages. It forces a plain text MIME part when you don't want one, and overwrites your plain text with its own.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.gmass.co\/blog\/gmail-api-html-plain-text-messages\/","og_locale":"en_US","og_type":"article","og_title":"The Gmail API's quirky handling of HTML and Plain Text messages parts","og_description":"The Gmail API is tricky with plain text messages. It forces a plain text MIME part when you don't want one, and overwrites your plain text with its own.","og_url":"https:\/\/www.gmass.co\/blog\/gmail-api-html-plain-text-messages\/","og_site_name":"GMass Blog","article_publisher":"https:\/\/www.facebook.com\/GmailMailMerge\/","article_published_time":"2019-10-17T09:02:07+00:00","article_modified_time":"2021-04-27T07:49:52+00:00","og_image":[{"width":1001,"height":467,"url":"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/Gmail-quirky-hi-hello-45k.png","type":"image\/png"}],"author":"Ajay Goel","twitter_card":"summary_large_image","twitter_creator":"@PartTimeSnob","twitter_site":"@GMassForGmail","twitter_misc":{"Written by":"Ajay Goel","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.gmass.co\/blog\/gmail-api-html-plain-text-messages\/#article","isPartOf":{"@id":"https:\/\/www.gmass.co\/blog\/gmail-api-html-plain-text-messages\/"},"author":{"name":"Ajay Goel","@id":"https:\/\/www.gmass.co\/blog\/#\/schema\/person\/b5fa74f8765b860701158fd77162fff8"},"headline":"The Gmail API&#8217;s quirky handling of HTML and Plain Text messages parts","datePublished":"2019-10-17T09:02:07+00:00","dateModified":"2021-04-27T07:49:52+00:00","mainEntityOfPage":{"@id":"https:\/\/www.gmass.co\/blog\/gmail-api-html-plain-text-messages\/"},"wordCount":1074,"commentCount":3,"image":{"@id":"https:\/\/www.gmass.co\/blog\/gmail-api-html-plain-text-messages\/#primaryimage"},"thumbnailUrl":"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/Gmail-quirky-hi-hello-45k.png","articleSection":["Developers","Gmail API"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.gmass.co\/blog\/gmail-api-html-plain-text-messages\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.gmass.co\/blog\/gmail-api-html-plain-text-messages\/","url":"https:\/\/www.gmass.co\/blog\/gmail-api-html-plain-text-messages\/","name":"The Gmail API's quirky handling of HTML and Plain Text messages parts","isPartOf":{"@id":"https:\/\/www.gmass.co\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.gmass.co\/blog\/gmail-api-html-plain-text-messages\/#primaryimage"},"image":{"@id":"https:\/\/www.gmass.co\/blog\/gmail-api-html-plain-text-messages\/#primaryimage"},"thumbnailUrl":"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/Gmail-quirky-hi-hello-45k.png","datePublished":"2019-10-17T09:02:07+00:00","dateModified":"2021-04-27T07:49:52+00:00","author":{"@id":"https:\/\/www.gmass.co\/blog\/#\/schema\/person\/b5fa74f8765b860701158fd77162fff8"},"description":"The Gmail API is tricky with plain text messages. It forces a plain text MIME part when you don't want one, and overwrites your plain text with its own.","breadcrumb":{"@id":"https:\/\/www.gmass.co\/blog\/gmail-api-html-plain-text-messages\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.gmass.co\/blog\/gmail-api-html-plain-text-messages\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.gmass.co\/blog\/gmail-api-html-plain-text-messages\/#primaryimage","url":"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/Gmail-quirky-hi-hello-45k.png","contentUrl":"https:\/\/www.gmass.co\/blog\/wp-content\/uploads\/2019\/10\/Gmail-quirky-hi-hello-45k.png","width":1001,"height":467},{"@type":"BreadcrumbList","@id":"https:\/\/www.gmass.co\/blog\/gmail-api-html-plain-text-messages\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.gmass.co\/blog\/"},{"@type":"ListItem","position":2,"name":"The Gmail API&#8217;s quirky handling of HTML and Plain Text messages parts"}]},{"@type":"WebSite","@id":"https:\/\/www.gmass.co\/blog\/#website","url":"https:\/\/www.gmass.co\/blog\/","name":"GMass Blog","description":"Tips and tricks for sending mail merge and mass email campaigns directly from Gmail","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.gmass.co\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.gmass.co\/blog\/#\/schema\/person\/b5fa74f8765b860701158fd77162fff8","name":"Ajay Goel","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/f3a70af05bbabe47925150d5a1320540e801b78055a74da20658fc97cd0706a2?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/f3a70af05bbabe47925150d5a1320540e801b78055a74da20658fc97cd0706a2?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f3a70af05bbabe47925150d5a1320540e801b78055a74da20658fc97cd0706a2?s=96&d=mm&r=g","caption":"Ajay Goel"},"description":"Ajay is the founder of GMass and has been developing email sending software for 20 years.","sameAs":["https:\/\/twitter.com\/PartTimeSnob","https:\/\/x.com\/PartTimeSnob"],"url":"https:\/\/www.gmass.co\/blog\/author\/ajay-goel\/"}]}},"_links":{"self":[{"href":"https:\/\/www.gmass.co\/blog\/wp-json\/wp\/v2\/posts\/10092","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gmass.co\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gmass.co\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gmass.co\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gmass.co\/blog\/wp-json\/wp\/v2\/comments?post=10092"}],"version-history":[{"count":46,"href":"https:\/\/www.gmass.co\/blog\/wp-json\/wp\/v2\/posts\/10092\/revisions"}],"predecessor-version":[{"id":16293,"href":"https:\/\/www.gmass.co\/blog\/wp-json\/wp\/v2\/posts\/10092\/revisions\/16293"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gmass.co\/blog\/wp-json\/wp\/v2\/media\/10101"}],"wp:attachment":[{"href":"https:\/\/www.gmass.co\/blog\/wp-json\/wp\/v2\/media?parent=10092"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gmass.co\/blog\/wp-json\/wp\/v2\/categories?post=10092"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gmass.co\/blog\/wp-json\/wp\/v2\/tags?post=10092"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}