I'm feeding a list of tuples that contain results from a sql query into my email. I want to be able to format the email to print out the lines in the msg
list in the following format:
SUBJECT LINE: xxxxx
File name: 'Blah Blah Blah' Sent to Pivot at: '3:30 p.m.'
File name: 'Blah Blah Blah' Sent to Pivot at: '3:30 p.m.'
File name: 'Blah Blah Blah' Sent to Pivot at: '3:30 p.m.'
File name: 'Blah Blah Blah' Sent to Pivot at: '3:30 p.m.'
File name: 'Blah Blah Blah' Sent to Pivot at: '3:30 p.m.'
Here is the code:
def send_email(recipient_list, missing_list):
msg = ['File name: {0} '
'Sent to Pivot at: {1}\n'.format(element[1],
element[2]) for element in missing_list]
msg['Subject'] = 'Alert!! Handshake files missing!'
msg['From'] = r'm****@r**.com'
msg['To'] = recipient_list
s = smtplib.SMTP(r'mail.r**.com')
s.sendmail(msg['From'], msg['To'], msg)
s.quit()
My input will be in the form of a list of tuples containing three items, [(id, file_name, timestamp)]
It seems like you're missing the knowledge of some basic data structures in Python.
This is pretty close:
msg = ['File name: {0} '
'Sent to Pivot at: {1}'.format(element[1],
element[2]) for element in missing_list]
But you'll want to join it together in a single string. Perhaps something like this:
body = '\n'.join(msg)
I'm not sure what else you're trying to do with msg
. You've created it as a list
, but then are trying to assign values to keys as though it were a dict
. Why not just use local variables such as subject
, and from
?
It seems that from the smtplib
examples that you'll also need to include From:
and To:
in the body of the email. Look at this example for a bit higher level abstraction of how to do that with smtplib
.
I know I haven't completely re-written your code into a working version here, but hopefully this at least gets you closer.