PYTHON: Sending email with an attachment that updates every 15 minutes - python

Below is a section of my code, it is in a while true loop to collect CO2 and Temperature readings from sensors every 15 minutes. It exports the data to a new CSV file every 15 minutes. The file name changes every 15 minutes as there is a date and time stamp required in the file name.
Can python be used to send an email with the CSV file attached if the file name is constantly changing?
I found the example below the dashed line online but the file location would be changing every 15 minutes in my case
for device in list_of_devices:
print (device)
for url_CO2 in list_of_urls_CO2:
headers = CaseInsensitiveDict()
headers['Accept'] = 'application/json'
headers['Authorization'] = bearer_token
resp_CO2 = requests.get(url_CO2, headers=headers)
response_CO2.append(resp_CO2.text)
print(resp_CO2.text)
for url_RT in list_of_urls_RT:
headers = CaseInsensitiveDict()
headers['Accept'] = 'application/json'
headers['Authorization'] = bearer_token
resp_RT = requests.get(url_RT, headers=headers)
response_RT.append(resp_RT.text)
print(resp_RT.text)
# importing pandas as pd
import pandas as pd
# dictionary of lists
myDict = {'Device': list_of_devices, 'CO2 Level': response_CO2, 'Room Temperature': response_RT}
df = pd.DataFrame(myDict)
# saving the dataframe
df.to_csv('test_{}.csv'.format(datetime.now().strftime("%Y-%m-%d %H.%M.%S")), index=False)
----------------------------------------------------------------------------------------
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
import os.path
email = 'myaddress#gmail.com'
password = 'password'
send_to_email = 'sentoaddreess#gmail.com'
subject = 'Town Farm Data Log'
message = 'Please see attached CO2 levels and Room Temperatures for Town Farm Classrooms'
file_location = 'C:\\Users\\You\\Desktop\\attach.txt'
msg = MIMEMultipart()
msg['From'] = email
msg['To'] = send_to_email
msg['Subject'] = subject
msg.attach(MIMEText(message, 'plain'))
filename = os.path.basename(file_location)
attachment = open(file_location, "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
msg.attach(part)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(email, password)
text = msg.as_string()
server.sendmail(email, send_to_email, text)
server.quit()

If you don't need to keep your files after send, it's easy to do like this:
Put your sending email code into the function named send_by_email for example
Then just collect all the files in the target dir, filter only csv-s and send them one by one (don't forget to delete file after send not to get into eternal loop on next iteration)
import os
mypath = '/tmp' # define path to dir with your files
filenames = next(os.walk(mypath), (None, None, []))[2] # list all files
csvs = [f for f in filenames if f.endswith('.csv')] # fileter only .scv files
for filename in csvs:
file_with_path = os.path.join(mypath, filename)
send_by_email(file_with_path)
os.remove(file_with_path)
If you need to keep files, you can just move them into another dir instead of deleting.

Related

Looping thru file types and attaching them into email

I have a list of text files and html files generated by two distinct functions. Each file is labeled signal1.txt, signal2, etc. and signal1.html, signal2.html, etc. I need to send an email with each file pair (signal1.txt and signal1.html, signal2.txt and signal.2.html, and so forth).
I've tried several different ways, but I keep getting just one file pair attached (the last file number whatever it is) over and over. I have no problem sending one file type, but it gets messy when I try with two different files. I'd like to give you as much info as possible and perhaps enough reproducible code for you to try it out on your end if you wish, so my apologies for the long question.
The data is collected from the server. The final result is sorted using the Counter module:
data = Counter({('A user account was locked out ', 47, 'medium', 25): 1, ('An attempt was made to reset an accounts password ', 73, 'high', 2): 1, ('PowerShell Keylogging Script', 73, 'high', 37): 1, ('PowerShell Suspicious Script with Audio Capture Capabilities', 47, 'medium', 36): 1})
I need the rule name to be used in the email subject, so everything else is junk. For instance, in ('A user account was locked out ', 47, 'medium', 25): 1, I only need A user account was locked out. So the following function takes care of all that:
def create_txt_files():
global regex
global count
count = 0
#Convert dict into string and remove unwanted chars
for signal in dict(event_dict).keys():
indiv_signal = (str(signal).replace(",",'').replace('(','').replace(')','')\
.replace("'",'').replace('[','').replace(']',''))
#Further removal of debris using regex
pattern = '^(\D*)'
regex = ''.join(re.findall(pattern,indiv_signal,re.MULTILINE))
count +=1
with open(f"signal{count}.txt", "w") as fh:
fh.write(str(regex))
create_txt_files()
I also need to create html files that will go in the body of the email as a Dataframe. In this case I need almost all the fields in the data file. The dataframe should look like this:
Alert Score Risk Severity Total
0 A user account was locked out 47 medium 26
The following function takes care of that:
#Create Individual HTML files
def create_indiv_html_files():
global html_file
global count
count = 0
#Turn rows into columns
for items in list(event_dict):
df = pd.DataFrame(items)
new_df = df.transpose()
new_df.columns = ['Alert','Score Risk','Severity','Total']
html_file = new_df.to_html()
print(new_df)
count +=1
with open(f'signal{count}.html','w') as wf:
wf.write(html_file)
create_indiv_html_files()
So, up to this point everything is fine and dandy, albeit not as pretty a code as I'd like. But it works, and that's all I'm worried about now. The problem is that when I send the email, I'm getting only one rule (the last one) sent over and over. It's not iterating over the txt and html files and attaching them as it should.
Here is the email function I'm using. Despite my several different attempts, I still have not been able to figure out what's wrong. Thank you for taking the time to help.
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email import encoders
import smtplib, ssl
import os
dirname = r'C:\Path\To\Files'
ext = ('.txt','html')
for files in os.scandir(dirname):
if files.path.endswith(ext):
def sendmail():
html_body = '''
<html>
<body>
<p style="font-size: 12;"> <strong>Alert</strong><br>{html_file}</p>
</body>
</html>
'''.format(html_file=html_file)
subject = f'Alert: {regex} '
senders_email = 'mail#mail.comt'
receiver_email = 'mail#mail.comt'
# Create a multipart message and set headers
message = MIMEMultipart('alternative')
message['From'] = senders_email
message['To'] = receiver_email
message['Subject'] = subject
#Attach email body
message.attach(MIMEText(html_body, 'html'))
# Name of the file to be attached
filename = f'signal{count}.html'
# Open file in binary mode
with open(filename, 'rb') as attachment:
# Add file as application/octet-stream
part = MIMEBase('application', 'octet-stream')
part.set_payload(attachment.read())
# Encodes file in ASCII characters to send via email
encoders.encode_base64(part)
# Add header as key/value pair to attachment part
part.add_header(
'Content-Disposition',
f"attachment; filename= {filename}",
)
# Add attachment to message and convert message to string
message.attach(part)
text = message.as_string()
# Log into server using secure connection
context = ssl.create_default_context()
with smtplib.SMTP("smtp.mail.com", 25) as server:
# server.starttls(context=context)
# server.login(senders_email, 'password')
server.sendmail(senders_email, receiver_email, text)
print("Email sent!")
sendmail()
I rewrote the code and removed the global variables. Below code should work let me know if you get any errors.
import pathlib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email import encoders
import smtplib, ssl
import os
def create_txt_files(event_dict):
regex = []
count = 0
#Convert dict into string and remove unwanted chars
for signal in dict(event_dict).keys():
indiv_signal = (str(signal).replace(",",'').replace('(','').replace(')','')\
.replace("'",'').replace('[','').replace(']',''))
#Further removal of debris using regex
pattern = '^(\D*)'
regex.append(''.join(re.findall(pattern,indiv_signal,re.MULTILINE)))
count +=1
with open(f"signal{count}.txt", "w") as fh:
fh.write(str(regex[0]))
return regex
def create_indiv_html_files(event_dict):
html_file = []
count = 0
#Turn rows into columns
for items in list(event_dict):
df = pd.DataFrame(items)
new_df = df.transpose()
new_df.columns = ['Alert','Score Risk','Severity','Total']
html_file.append(new_df.to_html())
print(new_df)
count +=1
with open(f'signal{count}.html','w') as wf:
wf.write(html_file[0])
return html_file
def sendmail(html_file, regex, path_html):
html_body = '''
<html>
<body>
<p style="font-size: 12;"> <strong>Alert</strong><br>{html_file}</p>
</body>
</html>
'''.format(html_file=html_file)
subject = f'Alert: {regex} '
senders_email = 'mail#mail.comt'
receiver_email = 'mail#mail.comt'
# Create a multipart message and set headers
message = MIMEMultipart('alternative')
message['From'] = senders_email
message['To'] = receiver_email
message['Subject'] = subject
#Attach email body
message.attach(MIMEText(html_body, 'html'))
# Name of the file to be attached
# filename = f'signal{count}.html'
# Open file in binary mode
with open(path_html, 'rb') as attachment:
# Add file as application/octet-stream
part = MIMEBase('application', 'octet-stream')
part.set_payload(attachment.read())
# Encodes file in ASCII characters to send via email
encoders.encode_base64(part)
# Add header as key/value pair to attachment part
part.add_header(
'Content-Disposition',
f"attachment; filename= {path_html.name}",
)
# Add attachment to message and convert message to string
message.attach(part)
text = message.as_string()
# Log into server using secure connection
context = ssl.create_default_context()
with smtplib.SMTP("smtp.mail.com", 25) as server:
# server.starttls(context=context)
# server.login(senders_email, 'password')
server.sendmail(senders_email, receiver_email, text)
print("Email sent!")
data = Counter({('A user account was locked out ', 47, 'medium', 25): 1, ('An attempt was made to reset an accounts password ', 73, 'high', 2): 1, ('PowerShell Keylogging Script', 73, 'high', 37): 1, ('PowerShell Suspicious Script with Audio Capture Capabilities', 47, 'medium', 36): 1})
regex = create_txt_files(data)
html_file = create_indiv_html_files(data)
signalfiles = sorted(list(pathlib.Path('C:\Path\To\Files').glob('*.txt')))
htmlfiles = sorted(list(pathlib.Path('C:\Path\To\Files').glob('*.html')))
for i, path_html_file in enumerate(htmlfiles):
sendmail(html_file[i], regex[i], path_html_file)
The create_txt_files and create_indiv_html_files takes input the Counter dictionary. Sendmail function will take in regex, html_file string and html_file path.

How to change actual location to temporary, add headers and remove space in csv file on Python Email table from MySQL?

I have a python code that run a MySQL query and send a email with csv attachment. I need to make following three changes. Can anybody please help me with this?
I need to change the actual location to a temporary location (I tired, it gaves me an error then I chnage it to the real location)
I have no headers on my result csv file. How do I add header on result csv?
It puts a empty raw between each raw, how do I remove that too.
Code -
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.base import MIMEBase
import mysql.connector
import csv
my_db = mysql.connector.connect(
host="localhost",
user="root",
passwd="admin",
database="simplymacstaging"
)
my_cursor = my_db.cursor()
my_cursor.execute("SELECT CONVERT(DateCreated, Date) 'Date', StoreName, ROUND(sum(TotalCost), 2) 'TotalCost' "
"FROM simplymacstaging.ajdustmenthistory WHERE ReasonCode = 'Negligence - Service' AND "
"CONVERT(DateCreated, Date) >= '2019-02-03' GROUP by StoreName, Date")
databases = my_cursor.fetchall()
fp = open('C:\#Emailproject/emailtest.csv', 'w')
attach_file = csv.writer(fp)
attach_file.writerows(databases)
fp.close()
# My email
fromaddr = "******************#gmail.com"
# EMAIL ADDRESS YOU SEND TO
toaddr = "**********#gmail.com"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "Test Email Data" # SUBJECT OF THE EMAIL
body = "Hello, Please see the attched report for week. Thank you, ****"
msg.attach(MIMEText(body, 'plain'))
filename = "Test Email.csv" # NAME OF THE FILE WITH ITS EXTENSION
attachment = open("C:\#Emailproject/emailtest.csv", "rb") # PATH OF THE FILE
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
msg.attach(part)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromaddr, "***********") # YOUR PASSWORD
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
server.quit()
sample result image -
Thank you so much
to avoid the empty rows try this
fp = open('C:\#Emailproject/emailtest.csv', 'w', newline='')
to add the header row
attach_file.writerow(["date", "address", "col3"])
attach_file.writerows(databases)

Sending single email with 3 different attachments python 3

I'm just looking to send an email with 3 csv file attachments. When i get it to run, trying a solution i found on here, it gives me a BIN file instead of the 3 files. Or, trying another solution, it will only send the last of the the 3 files. When i run the code below, its gives me TypeError: add.header() takes 3 positional arguments but 4 were given.
I understand that it can be done with a function, but i'm not sure how to have it pull all three files into it. I've spent numerous hours trying to figure it out.
Posting it on here is my last resort. I appreciate any help in finding a solution.
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import time
import os
msg = MIMEMultipart()
msg['From'] = EMAIL_FROM
msg['To'] = ", ".join(RECIPIENT_LIST)
msg['Subject'] = 'Louisiana Contractors List'
#email content
message = """<html>
<body>
Attached is the Louisiana Contractors Spreadsheet.
<br><br>
Let me know if you have any questions
</body>
</html>
"""
msg.attach(MIMEText(message, 'html'))
files = [
'C:/Users/rkrouse/Downloads/search-results.csv',
'C:/Users/rkrouse/Downloads/search-results(1).csv',
'C:/Users/rkrouse/Downloads/search-results(2).csv']
for a_file in files:
attachment = open(a_file, 'rb')
part = MIMEBase('application','octet-stream')
part.set_payload((attachment).read())
part.add_header('Content-Disposition', 'attachment', a_file = os.path.basename('C:/Users/rkrouse/Downloads/search-results.csv'))
encoders.encode_base64(part)
msg.attach(part)
for a_file in files:
attachment = open(a_file, 'rb')
part = MIMEBase('application','octet-stream')
part.set_payload((attachment).read())
part.add_header('Content-Disposition', 'attachment', a_file = os.path.basename('C:/Users/rkrouse/Downloads/search-results(1).csv'))
encoders.encode_base64(part)
msg.attach(part)
for a_file in files:
attachment = open(a_file, 'rb')
part = MIMEBase('application','octet-stream')
part.set_payload(attachment.read())
part.add_header('Content-Disposition', 'attachment', a_file = os.path.basename('C:/Users/rkrouse/Downloads/search-results(2).csv'))
encoders.encode_base64(part)
msg.attach(part)
#sends email
smtpserver = smtplib.SMTP(EMAIL_SERVER, EMAIL_PORT)
smtpserver.sendmail(EMAIL_FROM, RECIPIENT_LIST, msg.as_string())
smtpserver.quit()
Updated tested solution:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
import time
import os
msg = MIMEMultipart()
msg['From'] = EMAIL_FROM
msg['To'] = ", ".join(RECIPIENT_LIST)
msg['Subject'] = 'Louisiana Contractors List'
#email content
message = """<html>
<body>
Attached is the Louisiana Contractors Spreadsheet.
<br><br>
Let me know if you have any questions
</body>
</html>
"""
msg.attach(MIMEText(message, 'html'))
files = [
'C:/Users/rkrouse/Downloads/search-results.csv',
'C:/Users/rkrouse/Downloads/search-results(1).csv',
'C:/Users/rkrouse/Downloads/search-results(2).csv']
for a_file in files:
attachment = open(a_file, 'rb')
file_name = os.path.basename(a_file)
part = MIMEBase('application','octet-stream')
part.set_payload(attachment.read())
part.add_header('Content-Disposition',
'attachment',
filename=file_name)
encoders.encode_base64(part)
msg.attach(part)
#sends email
smtpserver = smtplib.SMTP(EMAIL_SERVER, EMAIL_PORT)
smtpserver.sendmail(EMAIL_FROM, RECIPIENT_LIST, msg.as_string())
smtpserver.quit()
As you can see you need to specify the filename in the base64 encoding.
Also you are iterating the files three time.
A for loop is used to go through a list, set, array, etc. One for loop (as all attachments are in one list) should be enough.
Following is the simple snippet that I used to send an email to multiple people with multiple file attachments.
# -*- coding: utf-8 -*-
import smtplib
import mimetypes
from email.mime.multipart import MIMEMultipart
from email import encoders
from email.mime.audio import MIMEAudio
from email.mime.base import MIMEBase
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
import ntpath
sender_address = 'vijay.anand#xxxx.com'
default_subject = 'Test email from {}'.format(sender_address)
smtp_server_address = '10.111.41.25'
smtp_port_number = 25
default_message = message = """<html>
<body>
Attached is the Louisiana Contractors Spreadsheet.
<br><br>
Let me know if you have any questions
</body>
</html>
"""
def send_by_smtp(to=None, cc=None, bcc=None, subject=None, attachments=None, attachment_type='plain'):
"""
Snippet to send an email to multiple people along with multiple attachments.
:param to: list of emails
:param cc: list of emails
:param bcc: list of emails
:param subject: Email Subject
:param attachments: list of file paths
:param attachment_type: 'plain' or 'html'
:return: None
"""
email_from = sender_address
email_to = list()
files_to_send = attachments
msg = MIMEMultipart()
msg["From"] = email_from
if to:
to = list(set(to))
email_to += to
msg["To"] = ', '.join(to)
if cc:
cc = list(set(cc))
email_to += cc
msg["Cc"] = ', '.join(cc)
if bcc:
bcc = list(set(bcc))
email_to += bcc
msg["Bcc"] = ', '.join(bcc)
if subject:
msg["Subject"] = subject
msg.preamble = subject
else:
msg["Subject"] = default_subject
msg.preamble = default_subject
body = default_message
msg.attach(MIMEText(body, attachment_type))
if files_to_send:
for file_to_send in files_to_send:
content_type, encoding = mimetypes.guess_type(file_to_send)
if content_type is None or encoding is not None:
content_type = "application/octet-stream"
maintype, subtype = content_type.split("/", 1)
if maintype == "text":
with open(file_to_send) as fp:
# Note: we should handle calculating the charset
attachment = MIMEText(fp.read(), _subtype=subtype)
elif maintype == "image":
with open(file_to_send, "rb") as fp:
attachment = MIMEImage(fp.read(), _subtype=subtype)
elif maintype == "audio":
with open(file_to_send, "rb")as fp:
attachment = MIMEAudio(fp.read(), _subtype=subtype)
else:
with open(file_to_send, "rb") as fp:
attachment = MIMEBase(maintype, subtype)
attachment.set_payload(fp.read())
encoders.encode_base64(attachment)
attachment.add_header("Content-Disposition", "attachment", filename=ntpath.basename(file_to_send))
msg.attach(attachment)
try:
smtp_obj = smtplib.SMTP(host=smtp_server_address, port=smtp_port_number, timeout=300)
smtp_obj.sendmail(from_addr=email_from, to_addrs=list(set([email_from] + email_to)), msg=msg.as_string())
print("Successfully sent email to {}".format(str(email_to)))
smtp_obj.quit()
return True
except smtplib.SMTPException:
print("Error: unable to send email")
return False
if __name__ == '__main__':
print('Send an email using Python')
result = send_by_smtp(to=['vijay#xxxx.com', 'tha#xxx.com'],
cc=['anandp#xxxx.com', 'nitha#xxxx.com'],
bcc=['vij#xxxx.com', 'Sun#xxxx.com'],
subject='Louisiana Contractors List',
attachments=['test.txt', '1.JPG', '2.PNG', '3.PNG', '4.PNG'],
attachment_type='html')
if result:
print('Email Sent Successfully')
else:
print('Email Sending Failed')

Send mail in Lotus Notes using python

i need help for send a mail in the Lotus Notes using python, appear that the win32com can do it, but i don't found any complete example or tutorial. My idea is a simple function like it:
from win32com.client import Dispatch
import smtplib
def SendMail(subject, text, user):
session = Dispatch('Lotus.NotesSession')
session.Initialize('???')
db = session.getDatabase("", "")
db.OpenMail();
Some suggestion? Thanks!
Below is some code that I have used for this purpose for several years:
from __future__ import division, print_function
import os, uuid
import itertools as it
from win32com.client import DispatchEx
import pywintypes # for exception
def send_mail(subject,body_text,sendto,copyto=None,blindcopyto=None,
attach=None):
session = DispatchEx('Lotus.NotesSession')
session.Initialize('your_password')
server_name = 'your/server'
db_name = 'your/database.nsf'
db = session.getDatabase(server_name, db_name)
if not db.IsOpen:
try:
db.Open()
except pywintypes.com_error:
print( 'could not open database: {}'.format(db_name) )
doc = db.CreateDocument()
doc.ReplaceItemValue("Form","Memo")
doc.ReplaceItemValue("Subject",subject)
# assign random uid because sometimes Lotus Notes tries to reuse the same one
uid = str(uuid.uuid4().hex)
doc.ReplaceItemValue('UNIVERSALID',uid)
# "SendTo" MUST be populated otherwise you get this error:
# 'No recipient list for Send operation'
doc.ReplaceItemValue("SendTo", sendto)
if copyto is not None:
doc.ReplaceItemValue("CopyTo", copyto)
if blindcopyto is not None:
doc.ReplaceItemValue("BlindCopyTo", blindcopyto)
# body
body = doc.CreateRichTextItem("Body")
body.AppendText(body_text)
# attachment
if attach is not None:
attachment = doc.CreateRichTextItem("Attachment")
for att in attach:
attachment.EmbedObject(1454, "", att, "Attachment")
# save in `Sent` view; default is False
doc.SaveMessageOnSend = True
doc.Send(False)
if __name__ == '__main__':
subject = "test subject"
body = "test body"
sendto = ['abc#def.com',]
files = ['/path/to/a/file.txt','/path/to/another/file.txt']
attachment = it.takewhile(lambda x: os.path.exists(x), files)
send_mail(subject, body, sendto, attach=attachment)
If your company is set up such a way that a host IP address and Port is sufficient to send an email, then use the following:
import smtplib
from email.mime.base import MIMEBase
from email import encoders
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def emailReport(attach_path,file_name,From,to,cc=None):
msg = MIMEMultipart()
msg['Subject'] = file_name
msg['From'] = From
msg['To'] = ", ".join(to)
msg['CC'] = ", ".join(cc)
msg.attach(MIMEText("****Body of your email****\n"))
#For multiple attachments repeat/loop the following:
part = MIMEBase('application', "octet-stream")
part.set_payload(open(attach_path.format(file_name), "rb").read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename=%s' % file_name)
msg.attach(part)
#Repeat stops here....
s = smtplib.SMTP(host='xxx.xxx.xx.x',port=xx) #Enter your IP and port here
s.sendmail(From,to+cc,msg.as_string())
s.quit()
to=['Person1#email.com', 'Person2#email.com']
cc=['Person3#email.com', 'Person4#email.com']
From='Your#email.com'
attach_path=r'C:\Users\Desktop\Temp'
file_name='Test.xlsx'
emailReport(attach_path,file_name,From,to,cc)

Python Email table from MYSQL

I am trying to get the results of a MYSQL query into the body of an email and also as a csv or xls attachment.
My code below works and sends the email only problem is if the results from the MYSQL query are more than a row only the first row shows up in the email.
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders
import os
import MySQLdb
import string
import datetime
import time
today = (time.strftime("%m/%d/%Y"))
#print today
db = MySQLdb.connect(host="-----.com", # your host, usually localhost
user="----", # your username
passwd="-------", # your password
db="dailies") # name of the data base
cursor49=db.cursor()
cursor49.execute("SELECT PLACEMENT_NAME FROM dailies.pub_cpm join placement ON placement.PLACEMENT_id = pub_cpm.PLACEMENT_ID where date(pub_cpm.created) = date(now())")
results49 = cursor49.fetchone()
# Commit your changes in the database
db.commit()
# disconnect from server
db.close()
results50 = "Latest Pub CPM Name(s): %s" % (results49)
gmail_user = "------#gmail.com"
gmail_pwd = "g---a"
mailServer = smtplib.SMTP("smtp.gmail.com", 587)
to = ['-----#g-----']
msg = MIMEMultipart()
msg['From'] = gmail_user
msg['To'] = ", ".join(to)
msg['Subject'] = "Database Alerts: %s" % (today)
body = results50
msg.attach(MIMEText(body, 'plain'))
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
text = msg.as_string()
mailServer.login(gmail_user, gmail_pwd)
mailServer.sendmail(gmail_user, to, text)
# Should be mailServer.quit(), but that crashes...
mailServer.close()
There are a couple changes you need to make. First, you need to change your fetchone() call to a fetchall() call. This will return all results from your SELECT query.
Next, you want to write these to a CSV file. Let's do that using the results from our query above:
results49 = cursor.fetchall()
fp = open('/tmp/file_name.csv', 'w') # You pick a name, it's temporary
attach_file = csv.writer(fp)
attach_file.writerows(results49)
fp.close()
At this point, you have a file in /tmp/file_name.csv (or what ever path and name you picked) that contains your CSV results. The final step is to attach this to an email.
msg = MIMEMultipart()
msg['From'] = gmail_user
msg['To'] = ", ".join(to)
msg['Subject'] = "Database Alerts: %s" % (today)
body = results50
part = MIMEBase('application', "octet-stream")
part.set_payload(open("/tmp/file_name.csv", "rb").read()) # This is the same file name from above
Encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="/tmp/file_name.csv"')
msg.attach(part)
I changed your msg.attach() function and utilized code from another question.
Once this is done, you still have a file in /tmp/file_name.csv. You can delete this safely at this point.

Categories

Resources