aiosmtplib icon indicating copy to clipboard operation
aiosmtplib copied to clipboard

Do not receive emails using Bcc, Cc headers

Open macartur opened this issue 3 years ago • 1 comments

Hello @cole, I am working with aiosmtplib and I realize an error using Bcc and Cc headers. Bcc, Cc emails are not receiving a message.

My SMTPNotifier was implemented as follow:

class SMTPNotifier:  # pylint: disable=too-few-public-methods
    """Class to connect to SMTP and send Email messages."""

    host: str = AWS_SMTP_SETTINGS['HOST']
    port: int = int(AWS_SMTP_SETTINGS['PORT'])
    user: str = AWS_SMTP_SETTINGS['USER']
    password: str = AWS_SMTP_SETTINGS['PASSWORD']
    sender_email: str = AWS_SMTP_SETTINGS['SENDER_EMAIL']
    sender_name: str = AWS_SMTP_SETTINGS['SENDER_NAME']                    

    @classmethod
    def make_buffer_message(cls, email):
        """Create a message content from email instance.

        Args:
            email: Email instance

        Returns:
            MIMEMultipart instance create from email.
        """
        msg = MIMEMultipart('alternative')
        msg['Subject'] = email.get('subject')
        msg['From'] = formataddr((cls.sender_name, cls.sender_email))
        msg['To'] = ",".join(email.get('recipients', []))
        msg['Cc'] = ",".join(email.get('copy', []))
        msg['Bcc'] = ','.join(email.get('hidden_copy', []))
        msg.attach(MIMEText(email.get('body'), 'plain'))
        msg.attach(MIMEText(email.get('body'), 'html'))
        return msg

    @classmethod
    def make_recipients_list(cls, email):
        """Create a recipient list and return it."""
        recipients = []
        recipients.extend(email.get('recipients', []))
        recipients.extend(email.get('copy', []))
        recipients.extend(email.get('hidden_copy', []))
        return recipients

    @classmethod
    async def send_email(cls, email):
        """Create a smtp connection and send the email given."""
        msg = cls.make_buffer_message(email)
        recipients = cls.make_recipients_list(email)
        try:
            await aiosmtplib.send(msg, hostname=cls.host, port=cls.port,
                                  username=cls.user, password=cls.password,
                                  start_tls=True,
                                  recipients=recipients)
        except ValueError as exception:
            raise_error(exception, http_status=HTTPStatus.BAD_GATEWAY)

        return (email, HTTPStatus.OK)
                                                                           

The email message is displaying:

Content-Type: multipart/alternative; boundary="===============0417934914657218713=="
MIME-Version: 1.0
Subject: Examplo de titulo
From: Nexu <[email protected]>
To: [email protected]
Cc: [email protected]
Bcc: [email protected]

--===============0417934914657218713==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

Teste BCC
--===============0417934914657218713==
Content-Type: text/html; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

Teste BCC
--===============0417934914657218713==--

And the receipts are: ['[email protected]', '[email protected]', '[email protected]']

Can someone help me ?

macartur avatar Mar 02 '22 20:03 macartur

Sorry, I missed this - does it work if you omit the recipients keyword argument from aiosmtplib.send?

cole avatar Jun 02 '22 22:06 cole

Closing for no activity as similar tests work for me. Please re-open if needed.

cole avatar Oct 19 '22 05:10 cole