Phpmailer: BCC Breaks Message Content

Created on 24 Mar 2017  路  15Comments  路  Source: PHPMailer/PHPMailer

Hello,

I am using version 5.2.22, installed via composer.

This version was working great up until the point I added a bcc to the mix, using the following code.

$this->mailer = new PHPMailer();
$this->mailer->isSMTP(true);
$this->mailer->Host = $GLOBALS["config"]["email"]["host"];
$this->mailer->SMTPAuth = $GLOBALS["config"]["email"]["smtpAuth"];
$this->mailer->Username = $GLOBALS["config"]["email"]["username"];
$this->mailer->Password = $GLOBALS["config"]["email"]["password"];
foreach($GLOBALS["config"]["email"]["bcc"] as $bcc){
    if(isset($bcc["email"], $bcc["name"])){
        $this->mailer->addBCC($bcc["email"], $bcc["name"]);
    }else if($bcc["email"]){
        $this->mailer->addBCC($bcc["email"]);
    }
}

When I have no bcc's in for the email, it works perfectly. HTML sends just fine. But, when I add a bcc to the email, the headers seem to be changed and the email is sent as plain text (HTML included). The bcc contacts and the people I send it to receive it like this. I am using SMTP to send this email.

All 15 comments

It's very hard to read that code. Could you reformat it please?

Can you generate an example message (the message source, not a screen shot) - I can't see how it's going astray with only the code.

Yeah.. My markdown skills aren't the best. Trying to fix it.

The code has been reformatted. Also, when I go to send the message, I use the following. Also, attached is a copy paste from outlook.
message.txt

$this->mailer->setFrom("[email protected]", "Support");
$this->mailer->addAddress("[email protected]", "John Doe");
$this->mailer->MsgHTML("<strong>This is an example</strong>");
$this->mailer->send();

To generate a complete message, do this instead of calling send():

$this->mailer->preSend();
echo $this->mailer->getSentMIMEMessage();

BCC should have no effect on headers, because BCC addresses do not appear in headers at all (especially when using SMTP), so I'm not sure what's amiss here.

Attached is the output from getSentMIMEMessage();
message2.txt

That's strange. The BCC header should only be there if you're not sending with SMTP - you can see the check here. The isSMTP() method sets Mailer to smtp (note that it doesn't take a parameter), so it should never get past that check.

It's a bit strange to me, I've used older versions of PHPMailer in the past and never had this issue... Matter of fact this is the same exact class I built for an older version.

Can you set a breakpoint in that area and see how it manages to get in there? It's the only code that can add BCC headers. The unit tests (using SMTP) do not result in adding BCC headers. Incidentally, the message you attached renders perfectly as an HTML message for me, though it does include a BCC header.

I can't reproduce this at all. Here is my test code using current master:

require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 3;
$mail->Host = '127.0.0.1';
$mail->Port = 2500;
$mail->isHTML(true);
$mail->Subject = "test";
$mail->setFrom('[email protected]', 'Noddy');
$mail->addAddress('[email protected]');
$mail->addBCC('[email protected]');
$mail->AltBody = 'hello';
$mail->Body = '<strong>hello</strong>';
$mail->send();

And here's the SMTP transcript:

2017-05-16 06:39:23 Connection: opening to 127.0.0.1:2500, timeout=300, options=array (
                                      )
2017-05-16 06:39:23 Connection: opened
2017-05-16 06:39:23 SERVER -> CLIENT: 220 oc ESMTP SubEthaSMTP null
2017-05-16 06:39:23 CLIENT -> SERVER: EHLO Oc
2017-05-16 06:39:23 SERVER -> CLIENT: 250-oc
                                      250-8BITMIME
                                      250-AUTH LOGIN
                                      250 Ok
2017-05-16 06:39:23 CLIENT -> SERVER: MAIL FROM:<[email protected]>
2017-05-16 06:39:23 SERVER -> CLIENT: 250 Ok
2017-05-16 06:39:23 CLIENT -> SERVER: RCPT TO:<[email protected]>
2017-05-16 06:39:23 SERVER -> CLIENT: 250 Ok
2017-05-16 06:39:23 CLIENT -> SERVER: RCPT TO:<[email protected]>
2017-05-16 06:39:23 SERVER -> CLIENT: 250 Ok
2017-05-16 06:39:23 CLIENT -> SERVER: DATA
2017-05-16 06:39:23 SERVER -> CLIENT: 354 End data with <CR><LF>.<CR><LF>
2017-05-16 06:39:23 CLIENT -> SERVER: Date: Tue, 16 May 2017 06:39:23 +0000
2017-05-16 06:39:23 CLIENT -> SERVER: To: [email protected]
2017-05-16 06:39:23 CLIENT -> SERVER: From: Noddy <[email protected]>
2017-05-16 06:39:23 CLIENT -> SERVER: Subject: test
2017-05-16 06:39:23 CLIENT -> SERVER: Message-ID: <fa2dead1270abb20d472b7c95d5290d9@Oc>
2017-05-16 06:39:23 CLIENT -> SERVER: X-Mailer: PHPMailer 5.2.23 (https://github.com/PHPMailer/PHPMailer)
2017-05-16 06:39:23 CLIENT -> SERVER: MIME-Version: 1.0
2017-05-16 06:39:23 CLIENT -> SERVER: Content-Type: multipart/alternative;
2017-05-16 06:39:23 CLIENT -> SERVER:   boundary="b1_fa2dead1270abb20d472b7c95d5290d9"
2017-05-16 06:39:23 CLIENT -> SERVER: Content-Transfer-Encoding: 8bit
2017-05-16 06:39:23 CLIENT -> SERVER:
2017-05-16 06:39:23 CLIENT -> SERVER: This is a multi-part message in MIME format.
2017-05-16 06:39:23 CLIENT -> SERVER:
2017-05-16 06:39:23 CLIENT -> SERVER: --b1_fa2dead1270abb20d472b7c95d5290d9
2017-05-16 06:39:23 CLIENT -> SERVER: Content-Type: text/plain; charset=us-ascii
2017-05-16 06:39:23 CLIENT -> SERVER:
2017-05-16 06:39:23 CLIENT -> SERVER: hello
2017-05-16 06:39:23 CLIENT -> SERVER:
2017-05-16 06:39:23 CLIENT -> SERVER:
2017-05-16 06:39:23 CLIENT -> SERVER: --b1_fa2dead1270abb20d472b7c95d5290d9
2017-05-16 06:39:23 CLIENT -> SERVER: Content-Type: text/html; charset=us-ascii
2017-05-16 06:39:23 CLIENT -> SERVER:
2017-05-16 06:39:23 CLIENT -> SERVER: <strong>hello</strong>
2017-05-16 06:39:23 CLIENT -> SERVER:
2017-05-16 06:39:23 CLIENT -> SERVER:
2017-05-16 06:39:23 CLIENT -> SERVER:
2017-05-16 06:39:23 CLIENT -> SERVER: --b1_fa2dead1270abb20d472b7c95d5290d9--
2017-05-16 06:39:23 CLIENT -> SERVER:
2017-05-16 06:39:23 CLIENT -> SERVER: .
2017-05-16 06:39:23 SERVER -> CLIENT: 250 Ok
2017-05-16 06:39:23 CLIENT -> SERVER: QUIT
2017-05-16 06:39:23 SERVER -> CLIENT: 221 Bye
2017-05-16 06:39:23 Connection: closed

You can see that it's using the BCC address in an RCPT TO command, but it does not appear in the headers, exactly as expected. 6.0 has slightly cleaner output, but it's basically the same.

Can you make this do anything different?

That's strange. The BCC header should only be there if you're not sending with SMTP - you can see the check here. The isSMTP() method sets Mailer to smtp (note that it doesn't take a parameter), so it should never get past that check.

@Synchro Can you explain why BCC is ignored when using SMTP-Mailing with PHPMailer? I'm not very familiar with mailing in general. I just debugged an issue where our BCC are ignored and the cause of this is the mentioned check. But why would that be? The BCC recipient does not get the mail, so I'm a bit confused what the underlying cause for the check is.

As I showed in the test example, BCC behaves as expected - can you provide code to reproduce your problem? This ticket was originally for 5.2, and things have changed a lot since, so it would be better if you tested with 6 and created a new issue if it鈥檚 still a problem.

@Synchro I'm using the SMTP-Plugin for Drupal with Drupal 8, which uses PHPMailer 5.1 as a dependency. So, I can't really provide example-code, as the construct around PHPMailer is very complex.

The check looks like this in V5.1:

// sendmail and mail() extract Bcc from the header before sending
    if ((($this->Mailer == 'sendmail') || ($this->Mailer == 'mail')) && (count($this->bcc) > 0)) {
      $result .= $this->AddrAppend('Bcc', $this->bcc);
    }

In my case, the BCC does not get appended, due to the reason that ($this->Mailer == 'mail')) evaluates to false, as I am using isSMTP() and thus, $this->Mailer is smtp.

I'm just wondering about the reason, why smtp is not allowed to append the BCC address.

This might not be the correct place to discuss my issue, but you seem to understand _why_ the check does not allow SMTP-Mailer to use BCC. There might be a valid design reason or similar that I am not aware of. I'm just looking for the reason why this check is performed at all.
However, I attached (censored) screenshots of debug-data from within the CreateHeader() method.

screen shot 2018-11-09 at 22 03 19

I didn't quite get what you meant originally - it's not that SMTP ignores BCC, but that BCC addresses should not appear as a header when sending via SMTP - otherwise it would not be BCC (hence the name, "Blind Carbon Copy"); the BCC addresses are only used in RCPT TO commands in SMTP, not as a header within the message. When sending via the mail() function, PHP itself strips out the BCC header and converts it to an SMTP recipient when passing it to the sendmail binary; that's why the check makes the distinction.

This makes perfectly sense, thank you for this information. So, in my case, I will not get the instance of PHPMailer, I guess, due to the abstraction of Drupal and the Modules. What is the _correct_ way of adding the BCC when using SMTP-Mode? I've seen people using the addCustomHeader() method for this as well? Do you have a hint?

Thank you for your help. 馃憤馃徎

No, just use addBCC(). You should then see the addresses in the SMTP commands but not the message headers.

Was this page helpful?
0 / 5 - 0 ratings