Search code examples
pythonpython-3.xemailsmtplib

Emails do not display recipients in To & Cc


I am trying to send email using smtplib in python 3.8 with my organisation exchange mailbox (Office 265), it works and emails do get delivered.

However, the emails do not display the To or CC with email address; email get delivered like user is sending it using Bcc. Please help


EMAIL_ADDRESS = 'mydept_help@myorg.com'
EMAIL_PASS = None

to = 'myemail@myorg.com'

with smtplib.SMTP('outbound.myorg.com', 25) as smtp:
        smtp.ehlo()
        smtp.starttls()
        smtp.ehlo()

        # smtp.login(EMAIL_ADDRESS,EMAIL_PASS)

        subject = 'Test email'
        body = 'Hello, sending email via python'

        msg = f'Subject: {subject}\n\n{body}'

        smtp.sendmail(EMAIL_ADDRESS, to, msg)

print(msg)```

Please note, our organisation lets us configure dummy emails and there is no authentication required; hence skipped *smtp.login* step

Solution

  • You need to include these details as header lines in the message body:

    msg = (f'From: {fromaddr}\r\nTo: {toaddr}\r\nsubject: {subject}\r\n\r\n{body}')