Created
December 9, 2010 17:35
-
-
Save supersighs/735031 to your computer and use it in GitHub Desktop.
Quick and Dirty html email sender for testing
This file contains hidden or 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/python | |
import smtplib, getpass, os, sys | |
import shelve | |
from email.mime.multipart import MIMEMultipart | |
from email.mime.text import MIMEText | |
def build_information(): | |
d = shelve.open('/tmp/mailer.data') | |
if d.get('gmail_user'): | |
gmail_user = raw_input('What is your gmail address? (Press enter to use %s)' % d.get('gmail_user')) | |
if len(gmail_user) == 0: | |
gmail_user = d.get('gmail_user') | |
else: | |
gmail_user = raw_input('What is your gmail address? ') | |
gmail_password = getpass.getpass('What is your gmail password? ') | |
recipient = raw_input('Where would you like this email to go? (Press enter to use %s)' % gmail_user) | |
d['gmail_user'] = gmail_user | |
if len(recipient) == 0: | |
recipient = gmail_user | |
return recipient, gmail_user, gmail_password | |
def get_file_contents(path): | |
file = open(os.path.join(os.getcwd(), path), 'r') | |
contents = file.read() | |
file.close() | |
return contents | |
def send_email(content): | |
recipient, gmail_user, gmail_password = build_information() | |
msg = MIMEMultipart('alternative') | |
msg['Subject'] = "HTML Test Email" | |
msg['From'] = gmail_user | |
msg['To'] = recipient | |
msg.attach(MIMEText(content, 'html')) | |
server = smtplib.SMTP('smtp.gmail.com',587) #port 465 or 587 | |
server.ehlo() | |
server.starttls() | |
server.ehlo() | |
server.login(gmail_user,gmail_password) | |
server.sendmail(gmail_user,recipient, msg.as_string()) | |
server.close() | |
if __name__ == "__main__": | |
if len(sys.argv) == 1: | |
print "Please specify an html file to send." | |
exit() | |
send_email(get_file_contents(sys.argv[1])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment