Created
August 1, 2014 18:32
-
-
Save rgjr/05c7fdb64303d9947f69 to your computer and use it in GitHub Desktop.
Sending an email via python while reading a text file
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/python -tt | |
from email.mime.text import MIMEText | |
from datetime import date | |
import smtplib, sys, csv | |
#Set up the SMTP server info | |
#TODO add response for username password and store info locally (obviously don't want you to have my info :] ) | |
SMTP_SERVER = #mail server, example: "smtp.gmail.com" | |
SMTP_PORT = #example: gmail "587" | |
SMTP_USERNAME = #username | |
SMTP_PASSWORD = #password | |
#Ask user for device number to append to email subject | |
DevNum = raw_input("Enter device number: ") | |
#Fill out To/From/Subject fields | |
EMAIL_TO = #recipient email address | |
EMAIL_FROM = #your email address | |
EMAIL_SUBJECT = #Subject, and at work used <subject> + DevNum to increment which device was being recorded | |
#TODO automatically increment DevNum based on previously accessed text file | |
#Date format to use in subject | |
DATE_FORMAT = "%m/%d/%Y" | |
#TODO remember what the hell this line does | |
EMAIL_SPACE = ", " | |
#Open log file and pull info | |
#TODO truncate output so we only get the necessary info from the log file, currently pulls ENTIRE log into email | |
LOG = #open('../doc.txt') | |
DATA = LOG.read() | |
#Send email function using smtplib | |
def send_email(): | |
msg = MIMEText(DATA) | |
LOG.close() | |
msg['Subject'] = EMAIL_SUBJECT + " %s" % (date.today().strftime(DATE_FORMAT)) | |
msg['To'] = EMAIL_SPACE.join(EMAIL_TO) | |
msg['From'] = EMAIL_FROM | |
mail = smtplib.SMTP(SMTP_SERVER, SMTP_PORT) | |
mail.starttls() | |
mail.login(SMTP_USERNAME, SMTP_PASSWORD) | |
mail.sendmail(EMAIL_FROM, EMAIL_TO, msg.as_string()) | |
mail.quit() | |
#main | |
if __name__=='__main__': | |
send_email() |
I was trying to run this program but it keeps on running with no console output :(
If you are using gmail, you probably need to activate less secure apps access from your google account settings to allow the script to access the specified account (EMAIL_FROM)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I was trying to run this program but it keeps on running with no console output :(