Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BCC header missing when using method getSentMIMEMessage() #2979

Open
mschering opened this issue Nov 21, 2023 · 3 comments
Open

BCC header missing when using method getSentMIMEMessage() #2979

mschering opened this issue Nov 21, 2023 · 3 comments

Comments

@mschering
Copy link

Hi,

We're using PHPMailer in our software Group-Office and there we want to get the full MIME to save into the sent items folder. We noticed that the BCC header is missing in our sent item.

I made an override which adds the BCC header in when calling getSentMimeMessage();

You can view the code here:

Intermesh/groupoffice@a416826

    /**
     * Returns the whole MIME message.
     * Includes complete headers and body.
     * Only valid post preSend().
     *
     * @see PHPMailer::preSend()
     *
     * @return string
     */
    public function getSentMIMEMessage()
    {
        $header = $this->MIMEHeader;

        // PHPMailer leaves BCC out of headers when using SMTP. We want this header for our sent items
        // source. So we append it here.
        if (
            (
                'sendmail' !== $this->Mailer && 'qmail' !== $this->Mailer && 'mail' !== $this->Mailer
            )
            && count($this->bcc) > 0
        ) {
            $header .= $this->addrAppend('Bcc', $this->bcc);
        }

        return static::stripTrailingWSP($header . $this->mailHeader) .
            static::$LE . static::$LE . $this->MIMEBody;
    }

While this works for me, I hope you can make a fix in the core package.

Best regards,
Merijn

@Synchro
Copy link
Member

Synchro commented Nov 21, 2023

This doesn't make much sense to me – the whole point of BCC is that addresses are not listed in headers. It leaves them in for the mail() function because the mail function uses that as a way of setting BCC addresses (it doesn't have any other way to do it), and then strips them from the message. I can see why you're wanting to do that for historical purposes and saving to IMAP, but for that scenario I think you'd be best off doing this in your own child class.

If anything I'd implement this as an explicit param on the getSentMIMEMessage method rather than relying on the Mailer value; it shouldn't be dependent on the transport since you're not using one at this point.

@mschering
Copy link
Author

If you're using sendmail, qmail or mail. The BCC value is already in the $this->MIMEHeader value. That's why I check if you're not using that mailer.

I understand that BCC should not be in the headers at the recipient, but you do want it in your sent items folder for reference. Mail clients store it like that.

@pmjbailly
Copy link

Hi,
Just to let you know that on my project I had the same concern.
I added a parameter to modify the use only when needed as suggested.

It took this form, if it can be of help to anybody :

  /**
     * Returns the whole MIME message.
     * Includes complete headers and body.
     * Only valid post preSend().
     *
     * @see PHPMailer::preSend()
     * @param bool $appendBcc //Pass to true to include Bcc address to the header, needed for storing the sent message 
     * @return string
     */
    public function getSentMIMEMessage($appendBcc = false)
    {
        return static::stripTrailingWSP($this->MIMEHeader .(($appendBcc && $this->Mailer=== "smtp" && count($this->bcc) > 0)?$this->addrAppend('Bcc', $this->bcc):''). $this->mailHeader) .
            static::$LE . static::$LE . $this->MIMEBody;
    }

And the usage, just adding true as the parameter in the gmail sample :

                $result = \imap_append($imapStream, $pathAppend, $mail->getSentMIMEMessage(true));

Hopefully it'll be added some day in a similar way, because now I can be assured that I'll have a real trace of what's leaving my app where it should be.

PS: thank you for this wonderful tool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants