Advertisement
Guest User

Untitled

a guest
Aug 10th, 2017
535
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import sys
  3. import smtplib
  4. from email.mime.text import MIMEText
  5.  
  6. email_user = "" # the FROM address (ex. user@gmail.com)
  7. email_pass = "" # plain-text password (ex. password123)
  8. email_host = "" # the smtp server to use (ex. smtp://gmail.com)
  9. email_port = 587 # the port to use
  10.  
  11.  
  12. helptext = "send_email <to_addr> <subj> [body] \n" + \
  13. "reads from stdin if body not provided \n"
  14.  
  15. if len(sys.argv) < 3:
  16. print(helptext)
  17. sys.exit(-1)
  18.  
  19. toaddr = sys.argv[1]
  20. subj = sys.argv[2]
  21.  
  22.  
  23. if len(sys.argv) > 3:
  24. body = sys.argv[3]
  25. else:
  26. body = sys.stdin.read()
  27.  
  28.  
  29. msg = MIMEText(body)
  30. msg['Subject'] = subj
  31. msg['To'] = toaddr
  32. msg['From'] = email_user
  33. try:
  34. server = smtplib.SMTP(email_host, email_port) # establish connection
  35. server.ehlo() # say hello
  36. server.starttls() # start tls ecryption
  37. server.login(email_user, email_pass) # verify sender
  38. server.sendmail(email_user, [toaddr], msg.as_string()) # send mail
  39. server.close() # close connection
  40. print('sent successfully')
  41. except Exception as e:
  42. print('failed to send', e)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement