Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gmail API - plaintext word wrapping

When sending emails using the Gmail API, it places hard line breaks in the body at around 78 characters per line. A similar question about this can be found here.

How can I make this stop? I simply want to send plaintext emails through the API without line breaks. The current formatting looks terrible, especially on mobile clients (tested on Gmail and iOS Mail apps).

I've tried the following headers:

Content-Type: text/plain; charset=UTF-8

Content-Transfer-Encoding: quoted-printable

Am I missing anything?

EDIT: As per Mr.Rebot's suggestion, I've also tried this with no luck:

Content-Type: mixed/alternative

EDIT 2: Here's the exact format of the message I'm sending (attempted with and without the quoted-printable header:

From: Example Account <[email protected]>
To: <[email protected]>
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Subject: This is a test!
Date: Tue, 18 Oct 2016 10:46:57 -GMT-07:00

Here is a long test message that will probably cause some words to wrap in strange places.

I take this full message and Base64-encode it, then POST it to /gmail/v1/users/{my_account}/drafts/send?fields=id with the following JSON body:

{
    "id": MSG_ID,
    "message": {
        "raw": BASE64_DATA
    }
}
like image 936
Hundley Avatar asked Oct 17 '16 03:10

Hundley


People also ask

How do I fix text wrapping in Gmail?

LOOK ON THE BOTTOM RIGHT HAND SIDE OF THE COMPOSE BOX THAT YOU'RE GONNA TYPE YOUR EMAIL ON. THEN CLICK THE 3 DOTS WHICH WILL OPEN A BOX WITH CHOICES. IF IT'S CHECKED OFF WITH OR ON PLAIN TEXT.. UNCHECK IT AND CHECK OFF THE FIRST CHOICE WHICH SAYS DEFAULT FULL SCREEN.

How do I send HTML formatted emails through the Gmail API?

message = MIMEText(message_text, 'html') <-- add the 'html' as the second parameter of the MIMEText object constructor.

How do I automate Gmail API?

Setup the Gmail API The MFA service check will access the Gmail account to get the MFA code requested from the web page. First, we have to enable the API and authenticate to Gmail. This can be done by following their Quickstart guide. Click the “ENABLE THE GMAIL API” form button to generate the credentials.


2 Answers

Are you running the content through a quoted printable encoder and sending the encoded content value along with the header or expecting the API to encode it for you?

Per wikipedia it seems like if you add soft line breaks with = less than 76 characters apart as the last character on arbitrary lines, they should get decoded out of the result restoring your original text.

UPDATE

Try sending with this content whose message has been quoted-printable encoded (base64 it):

From: Example Account <[email protected]>
To: <[email protected]>
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Subject: This is a test!
Date: Tue, 18 Oct 2016 10:46:57 -GMT-07:00

Here is a long test message that will probably cause some words to wrap in =
strange places.
like image 78
cchamberlain Avatar answered Nov 12 '22 08:11

cchamberlain


I'm assuming you have a function similar to this:

1. def create_message(sender, to, cc, subject, message_body):
2.     message = MIMEText(message_body, 'html')
3.     message['to'] = to
4.     message['from'] = sender
5.     message['subject'] = subject
6.     message['cc'] = cc
7.     return {'raw': base64.urlsafe_b64encode(message.as_string())}

The one trick that finally worked for me, after all the attempts to modify the header values and payload dict (which is a member of the message object), was to set (line 2):

  • message = MIMEText(message_body, 'html') <-- add the 'html' as the second parameter of the MIMEText object constructor

The default code supplied by Google for their gmail API only tells you how to send plain text emails, but they hide how they're doing that. ala... message = MIMEText(message_body)

I had to look up the python class email.mime.text.MIMEText object. That's where you'll see this definition of the constructor for the MIMEText object:

  • class email.mime.text.MIMEText(_text[, _subtype[, _charset]]) We want to explicitly pass it a value to the _subtype. In this case, we want to pass: 'html' as the _subtype.

Now, you won't have anymore unexpected word wrapping applied to your messages by Google, or the Python mime.text.MIMEText object

like image 37
Andrew Avatar answered Nov 12 '22 07:11

Andrew