0

I used server.sendmail(fromaddr, toaddrs, msg) to send automated emails using python.

How can I add an email subject to the mail? I tried to look in SMTP documentation and didn't find any documentation for that.

guyd
  • 693
  • 2
  • 14
  • 32
  • 2
    Check this - https://stackoverflow.com/questions/23552229/adding-subject-to-the-email-python – Dinesh Pundkar Jun 27 '17 at 09:47
  • 1
    Possible duplicate of [Python: "subject" not shown when sending email using smtplib module](https://stackoverflow.com/questions/7232088/python-subject-not-shown-when-sending-email-using-smtplib-module) – Billy Jun 27 '17 at 10:54

1 Answers1

2

From How to send an email with Gmail as provider using Python? :

def send_email(user, pwd, recipient, subject, body):
import smtplib

gmail_user = user
gmail_pwd = pwd
FROM = user
TO = recipient if type(recipient) is list else [recipient]
SUBJECT = subject
TEXT = body

# Prepare actual message
message = """From: %s\nTo: %s\nSubject: %s\n\n%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
try:
    server = smtplib.SMTP("smtp.gmail.com", 587)
    server.ehlo()
    server.starttls()
    server.login(gmail_user, gmail_pwd)
    server.sendmail(FROM, TO, message)
    server.close()
    print 'successfully sent the mail'
except:
    print "failed to send mail"
mikep
  • 3,841
  • 8
  • 21