Oscar C. Y. Chan
Oscar C. Y. Chan

Reputation: 31

Gmail API subject max character length?

Seems like most Email clients limit the subject line to 255 max characters. I am trying to save the Subject to a Postgres DB and the unpredictable line length of the subject field throws a wrench in my schema.

Upvotes: 2

Views: 2694

Answers (1)

Jacques-Guzel Heron
Jacques-Guzel Heron

Reputation: 2598

Communication over email follows RFC 2822. In its declaration you can read:

There are two limits that this standard places on the number of characters in a line. Each line of characters MUST be no more than 998 characters, and SHOULD be no more than 78 characters, excluding the CRLF.

This limit the subject to no more than 998 characters*, as Gmail allows. In your case, you can choose to only save the first 78 characters as most mails won't cross that threshold. As an alternative, you can save all 998 characters.

Both cases can be managed by setting up a column as VARCHAR() datatype with whatever length you choose to save. That format will automatically truncate every subject into the chosen length. If you plan to save the complete subject, you can opt for the TEXT (described in the previous link too) datatype; it will keep the complete string.

Keep in mind that both of these methods have an upper limit of 1 GB of data and, as is stated on the docs, they have no difference performance wise. I hope to have helped you, but feel free to ask in a comment if you want further guidance.

*In fact you can build a longer subject by unfolding it in two or more lines (each being 998 characters long as maximum), but many email providers including Gmail don't support that option so it doesn't apply to our scenario here.

Upvotes: 1

Related Questions