Last Updated: April 22, 2016
·
836
· btruhand

Sending mail with over 1000 characters in a single line

SMTP protocol (the protocol to communicate with mail servers when sending emails from user client) mandates that all email are limited to 1000 7-bit characters per line. Thus SMTP will try to truncate your lines and line-wrap lines that are too long. Depending on your needs this might cause you awful problems... like trying to send long Japanese text, resulting in garbled output known as "mojibake" in Japanese.

To ensure this doesn't happen you can encode your text in base64 or quoted-printable first. Here is a sample code for sending mail in PHP:

$text = "some long text insert here";
// base64 example
$headers = "From: example@from.com\r\nContent-Type: Text/Plain; charset=UTF-8\r\nContent-Transfer-Encoding:base64\r\n"; 
mail("example@to.com", "protip wow", base64_encode($text), $headers);

// quoted-printable example
$headers = "From: example@from.com\r\nContent-Type: Text/Plain; charset=UTF-8\r\nContent-Transfer-Encoding:quoted-printable\r\n"; 
mail("example@to.com", "protip wow", quoted_printable_encode($text), $headers);

For text messages it is recommended to use quoted-printable encoding