简体   繁体   中英

PHPMailer Encoding 8 bit

I have a weird problem with the Content-Transfer-Encoding of the emails I sent.

In add the following line:

$mail->Encoding = '8bit';

In my code, but the emails sent with quoted-printable...

In the source:

Content-Transfer-Encoding: quoted-printable

I don't know how to do for sending my emails with a Content-Transfer-Encoding in 8bit...

$mail->Encoding = '8bit'; seems to doesn't work...

All the code:

$mail             = new PHPMailer;

$mail->SMTPDebug  = 3;

$mail->isSMTP();
$mail->setLanguage('fr', './PHPMailer/language/');

$mail->CharSet    = 'UTF-8';
$mail->Encoding   = '8bit';
$mail->Host       = 'my_domain.com';
$mail->SMTPAuth   = true;
$mail->Username   = 'news@my_domain.com';
$mail->Password   = 'my_password';
$mail->SMTPSecure = '';
$mail->Port       = 25;
$mail->From       = 'news@my_domain.com';
$mail->FromName   = 'bla';
$mail->addAddress('my@mail.net');
$mail->addReplyTo('news@my_domain.com', 'bla');
$mail->AddCustomHeader("Content-Transfer-Encoding: 8bit\r\n");
$mail->WordWrap   = 10000;
$mail->isHTML(true);
$mail->Subject    = 'Subject';
$mail->Body       = 'bla';
$mail->AltBody    = "bla";

$mail->send();

Big thanks for help

Yep, I wrote that code. RFC5322 section 2.1.1 imposes a hard line length limit in email messages (it's a MUST requirement in the spec). If you've specified an encoding that does not allow variation in line length without altering the content (such as 8bit ), PHPMailer automatically switches to quoted-printable encoding. This approach means that your exact content is preserved through the encoding, while allowing long lines to be wrapped.

If you want to avoid this, wrap (or otherwise reduce) your line lengths so they are less than 998 chars.

Just because you will often be able to get away with this, you shouldn't, if only on the basis of Postel's law .

I suspect it's intentional. You're possibly hitting this line of code inside \\PHPMailer::createBody() :

//If lines are too long, and we're not already using an encoding that will shorten them,
//change to quoted-printable transfer encoding
if ('base64' != $this->Encoding and self::hasLineLongerThanMax($this->Body)) {
    $this->Encoding = 'quoted-printable';
    $bodyEncoding = 'quoted-printable';
}

... where the line length limit is hard-coded to 1000:

/**
 * Detect if a string contains a line longer than the maximum line length allowed.
 * @param string $str
 * @return boolean
 * @static
 */
public static function hasLineLongerThanMax($str)
{
    //+2 to include CRLF line break for a 1000 total
    return (boolean)preg_match('/^(.{'.(self::MAX_LINE_LENGTH + 2).',})/m', $str);
}

I can't tell you for sure but it's possible that mail specs simply do not allow long lines (even though in practice they often seem to work).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM