Search code examples
pythonemailsmtplib

Send email with sender and recipient header in python


After failing to send email with Mailgun API, I have been sending email with SMTP successfully using smtplib, with the following code.

def send_simple_message(mailtext, mailhtml):
    print("Mailhtml is:" + mailhtml)
    logging.basicConfig(level=logging.DEBUG)
    import smtplib
    from email.mime.text import MIMEText
    from email.mime.multipart import MIMEMultipart
    msg = MIMEMultipart('alternative')
    msg['Subject'] = "Summary of Credit Card payments due"
    msg['From']    = "creditcards@domain.in"
    msg['To']      = "me@domain.in"
    s = smtplib.SMTP('smtp.mailgun.org', 587)
    part1 = MIMEText(mailtext, 'plain')
    part2 = MIMEText(mailhtml, 'html')
    msg.attach(part1)
    msg.attach(part2)
    s.login('postmaster@domain.in', 'password')
    s.sendmail(msg['From'], msg['To'], msg.as_string())
    s.quit()

However this displays the sender field as creditcards@domain.in. How can I add a sender header field so the sender is shown as Someone important (creditcards@domain.in) instead?


Solution

  • I solved it by using Header from email.header and formataddr from email.utils. My complete working script looks like this:

    def send_simple_message(mailtext, mailhtml, month, files=None):
        # print("file is:" + filename)
        logging.basicConfig(level=logging.DEBUG)
        import smtplib
        from os.path import basename
        from email.mime.application import MIMEApplication
        from email.mime.text import MIMEText
        from email.mime.multipart import MIMEMultipart
        from email.header import Header
        from email.utils import formataddr
        Recipient = "me@domain.com"
        msg = MIMEMultipart('alternative')
        msg['Subject'] = "Summary of Credit Card payments due in " + month
        msg['From'] = formataddr((str(Header('Finance Manager', 'utf-8')), 'creditcards@domain.in'))
        msg['To']      = Recipient
        part1 = MIMEText(mailtext, 'plain')
        part2 = MIMEText(mailhtml, 'html')
        msg.attach(part1)
        msg.attach(part2)
        for f in files or []:
            with open(f, "rb") as fil:
                part = MIMEApplication(
                    fil.read(),
                    Name=basename(f)
                )
            # After the file is closed
            part['Content-Disposition'] = 'attachment; filename="%s"' % basename(f)
            msg.attach(part)
    
        s = smtplib.SMTP('smtp.mailgun.org', 587)
        s.login('postmaster@domain.in', 'pass')
        s.sendmail(msg['From'], msg['To'], msg.as_string())
        s.quit()