Created
December 8, 2013 14:43
-
-
Save prakhar1989/7858451 to your computer and use it in GitHub Desktop.
Sending emails with files as attachments in python 2.4
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 MySQLdb as mdb | |
import sys | |
import csv | |
import smtplib | |
from datetime import datetime | |
from email.MIMEBase import MIMEBase | |
from email.MIMEMultipart import MIMEMultipart | |
from email import Encoders | |
sender = "[email protected]" | |
rcvr = "[email protected]" | |
def interact_with_db(state, today): | |
try: | |
conn = mdb.connect('<ip_addr>', 'prakhars', 'prakhars123', 'site') | |
curr = conn.cursor() | |
curr.execute("SELECT username, parent_category FROM product_save_logger \ | |
WHERE state = %s and DATE(DATE_ADD(create_time, INTERVAL 3 HOUR)) = %s", | |
(state, today)) | |
records = curr.fetchall() | |
return records | |
except mdb.Error, e: | |
print "Error %d: %s" % (e.args[0],e.args[1]) | |
sys.exit(1) | |
if conn: conn.close() | |
def get_all_data(): | |
state = 'Fully Website Ready' | |
today = datetime.now().strftime('%Y-%m-%d') | |
results = interact_with_db(state, today) | |
categories = list() | |
users = list() | |
for r in results: | |
# get a list of users and categories | |
if r[0] not in users: users.append(r[0]) | |
if r[1] not in categories: categories.append(r[1]) | |
# initialize mapping table | |
category_user_mapping = {} | |
for c in categories: | |
category_user_mapping[c] = {} | |
for u in users: | |
category_user_mapping[c][u] = 0 | |
# populate mapping table | |
for r in results: | |
(u, c) = r | |
if u in category_user_mapping[c]: | |
category_user_mapping[c][u] += 1 | |
else: | |
category_user_mapping[c][u] = 0 | |
# print mapping table | |
print_mapping(category_user_mapping, users, categories) | |
def print_mapping(mapping_table, users, categories): | |
filename = "content_user_mapping.csv" | |
f = open(filename, "wb") | |
csvwriter = csv.writer(f) | |
csvwriter.writerow(["Categories"] + users) | |
for c in mapping_table: | |
csvwriter.writerow([c] + [str(mapping_table[c][u]) for u in users]) | |
f.close() | |
send_email_attachment(filename) | |
# code for sending email attachment | |
def send_email_attachment(filename): | |
msg = MIMEMultipart() | |
file_content = open("/home/prakhars/content_user_mapping.csv", "rb").read() | |
part = MIMEBase("application", "octet-stream") | |
part.set_payload(file_content) | |
Encoders.encode_base64(part) | |
today = datetime.now().strftime('%Y-%m-%d') | |
part.add_header("Content-Disposition", "attachment; filename=content-%s.csv" % today) | |
msg.attach(part) | |
msg['Subject'] = "Fully Website Ready report [%s]" % today | |
msg['From'] = sender | |
msg['To'] = rcvr | |
try: | |
smtpObj = smtplib.SMTP('localhost') | |
smtpObj.sendmail(sender, rcvr, msg.as_string()) | |
smtpObj.close() | |
except smtplib.SMTPException: | |
print "Failed to send email" | |
if __name__ == "__main__": | |
get_all_data() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment