Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/python
- import sys
- import os
- import re
- SMTPserver = 'smtp.gmail.com'
- SMPTport = 587
- sender = 'SENDER'
- if len(sys.argv) == 1:
- destination = sender
- subject = "No subject."
- content = """\
- No message content.
- """
- if len(sys.argv) == 2:
- destination = sys.argv[1]
- subject = "No subject."
- content = """\
- No message content.
- """
- if len(sys.argv) == 3:
- destination = sys.argv[1]
- subject = sys.argv[2]
- content = """\
- No message content.
- """
- if len(sys.argv) == 4:
- destination = sys.argv[1]
- subject = sys.argv[2]
- content = sys.argv[3]
- if len(sys.argv) == 5:
- destination = sys.argv[1]
- subject = sys.argv[2]
- content = sys.argv[3]
- filename = sys.argv[4]
- USERNAME = "USERNAME"
- PASSWORD = "PASSWORD"
- # typical values for text_subtype are plain, html, xml
- text_subtype = 'plain'
- #from smtplib import SMTP_SSL as SMTP # this invokes the secure SMTP protocol (port 465, uses SSL)
- from smtplib import SMTP # use this for standard SMTP protocol (port 25, no encryption)
- # old version
- # from email.MIMEText import MIMEText
- from email.mime.text import MIMEText
- from email.mime.multipart import MIMEMultipart
- try:
- msg = MIMEMultipart(content, text_subtype)
- msg['Subject']= subject
- msg['From'] = sender # some SMTP servers will do this automatically, not all
- conn = SMTP(SMTPserver, SMPTport)
- conn.ehlo()
- conn.starttls()
- conn.set_debuglevel(False)
- conn.login(USERNAME, PASSWORD)
- try:
- if len(sys.argv) == 5:
- f = open(filename)
- attachment = MIMEText(f.read())
- attachment.add_header('Content-Disposition', 'attachment', filename=filename)
- msg.attach(attachment)
- body = MIMEText(content, 'plain')
- msg.attach(body)
- conn.sendmail(sender, destination, msg.as_string())
- finally:
- conn.quit()
- print ("Message sent to", destination, "with subject", subject,".")
- except Exception as e:
- sys.exit( "mail failed; %s" % str(e) ) # give a error message
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement