Last active
February 18, 2018 10:25
-
-
Save Mausy5043/0f7fa43ba35cf68e2756b7bfe1419146 to your computer and use it in GitHub Desktop.
A simple Python script to send email
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2.7 | |
import sys | |
import getopt | |
import smtplib | |
import email.mime.text | |
import syslog | |
import os,time | |
from email.mime.text import MIMEText | |
# This is the Gmail address to send emails from | |
GMAIL_ADDRESS = 'senderemail@gmail.com' | |
# This is the app-specific password:(https://support.google.com/accounts/answer/185833?hl=en) | |
GMAIL_PASSWORD = 'xxxxxxxxx' | |
# This is the address where the mail is sent to: | |
RECIPIENT_ADDRESS = 'recipient@example.com' | |
def getargus(argv): | |
""" | |
function getargus() extracts the supported commandline options to variables | |
""" | |
inputfile = '' | |
subject = '' | |
recipient = '' | |
usage = 'Usage: simplemail.py -s <subject> -f <filename>' | |
try: | |
opts, args = getopt.getopt(argv,"hs:a:f:") | |
except getopt.GetoptError: | |
print usage | |
sys.exit(2) | |
if (opts == []): | |
print usage | |
sys.exit(2) | |
for opt, arg in opts: | |
if (opt == '-h') or (opts == ""): | |
print usage | |
sys.exit() | |
elif opt == '-f': | |
inputfile = arg | |
elif opt == '-s': | |
subject = arg | |
return (inputfile, subject) | |
if __name__ == "__main__": | |
inputfile = '' | |
subject = '' | |
inputfile, subject = getargus(sys.argv[1:]) | |
from_email = GMAIL_ADDRESS | |
to_email = RECIPIENT_ADDRESS | |
msg_text = "Test text" | |
with open(inputfile, 'rb') as fp: | |
# Create a text/plain message | |
msg = MIMEText(fp.read()) | |
msg['Subject'] = subject | |
msg['From'] = from_email | |
msg['To'] = to_email | |
s = smtplib.SMTP_SSL('smtp.gmail.com', '465') | |
s.login(GMAIL_ADDRESS, GMAIL_PASSWORD) | |
s.sendmail(from_email, to_email, msg.as_string()) | |
s.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment