Masoko

send html emails with smtplib and gmail

Dec 15th, 2017
716
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.61 KB | None | 0 0
  1. import smtplib, email.message
  2.  
  3. # configure smtp server for sending mail
  4. smtp_host = 'smtp.gmail.com'
  5. smtp_port = 587
  6. smtp_user = '[email protected]'
  7. smtp_password = 'password'
  8.  
  9. def send_email(message, subject, recipient):
  10.     msg = email.message.Message()
  11.     msg['Subject'] = subject
  12.     msg['From'] = smtp_user
  13.     msg['To'] = recipient
  14.     msg.add_header('Content-Type','text/html')
  15.     msg.set_payload(message)
  16.    
  17.     server = smtplib.SMTP(smtp_host, smtp_port)
  18.     server.ehlo()
  19.     server.starttls()
  20.     server.login(smtp_user, smtp_password)
  21.     server.sendmail(msg['From'], msg['To'], msg.as_string().encode('utf-8'))
  22.     server.quit()
Advertisement
Add Comment
Please, Sign In to add comment