Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import sys
- import smtplib
- from email.mime.text import MIMEText
- email_user = "" # the FROM address (ex. user@gmail.com)
- email_pass = "" # plain-text password (ex. password123)
- email_host = "" # the smtp server to use (ex. smtp://gmail.com)
- email_port = 587 # the port to use
- helptext = "send_email <to_addr> <subj> [body] \n" + \
- "reads from stdin if body not provided \n"
- if len(sys.argv) < 3:
- print(helptext)
- sys.exit(-1)
- toaddr = sys.argv[1]
- subj = sys.argv[2]
- if len(sys.argv) > 3:
- body = sys.argv[3]
- else:
- body = sys.stdin.read()
- msg = MIMEText(body)
- msg['Subject'] = subj
- msg['To'] = toaddr
- msg['From'] = email_user
- try:
- server = smtplib.SMTP(email_host, email_port) # establish connection
- server.ehlo() # say hello
- server.starttls() # start tls ecryption
- server.login(email_user, email_pass) # verify sender
- server.sendmail(email_user, [toaddr], msg.as_string()) # send mail
- server.close() # close connection
- print('sent successfully')
- except Exception as e:
- print('failed to send', e)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement