Guest User

Untitled

a guest
Sep 19th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. import smtplib
  2. from email.mime.multipart import MIMEMultipart
  3. from email.mime.text import MIMEText
  4.  
  5. gmail_user = 'username@gmail.com'
  6. gmail_password = 'p@ssw0rd'
  7.  
  8. def send_mail(params, type_):
  9. email_subject = params['email_subject']
  10. email_from = "from_email@domain.com"
  11. email_to = params['email_to']
  12. email_cc = params.get('email_cc')
  13. email_bcc = params.get('email_bcc')
  14. email_body = params['email_body']
  15.  
  16. msg = MIMEMultipart('alternative')
  17. msg['To'] = email_to
  18. msg['CC'] = email_cc
  19. msg['Subject'] = email_subject
  20. mt_html = MIMEText(email_body, type_)
  21. msg.attach(mt_html)
  22.  
  23. server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
  24. server.ehlo()
  25. toaddrs = [email_to] + [email_cc] + [email_bcc]
  26. server.login(gmail_user, gmail_password)
  27. server.sendmail(email_from, toaddrs, msg.as_string())
  28. server.close()
  29.  
  30. params = {
  31. 'email_to': 'username@gmail.com',
  32. 'email_cc': 'username@gmail.com',
  33. 'email_bcc': 'username@gmail.com',
  34. 'email_subject': 'Test message from python library',
  35. 'email_body': '<h1>Hello World</h1>'
  36. }
  37. for t in ['plain', 'html']: #sending plain and html email
  38. try:
  39. send_mail(params, t)
  40. print ('Email sent!')
  41. except Exception as e:
  42. print('Something wrong: '+e)
Add Comment
Please, Sign In to add comment