I am trying to construct an email from selecting data from a database, this is working I am saving the data out to a file, what I am doing it constructing the body of the email, I have the first test in a varable and the closing of the email in a variable.
When I am trying to get the data from a text file as part of the body, I can print the text when adding to the email, I am getting it in a list format i.e [name, dob, etc] or I can get it to just show the last line of the text.
Can you please help to get these names listed, so the mail is all dynamic
Contents of the text file would be
Craig Richards 01/01/1970
Barny Rubble 02/02/1971
Fred Flintsone 03/03/1972
It is showing the mail body as Can I please order the licenses for the following students please ['Craig Richards 01/01/1970','Barny Rubble 02/02/1971','Fred Flintsone 03/03/1972']
What I would like is like it is listed and showed in the text file. So it would say
Please can I order the licenses for the following students
Craig Richards 01/01/1970
Barny Rubble 02/02/1971
Fred Flintsone 03/03/1972
Regards
Craig
The code is
def sendMail(user,pwd,to,subject,text):
msg = MIMEText(text)
msg['From'] = 'craig@craig.com'
msg['To'] = to
msg['Subject'] = subject
try:
smtpServer = smtplib.SMTP('smtp.address','587')
print "[+] Connecting to the Mail Server [+]"
smtpServer.ehlo()
print "[+] Encrypting the session [+]"
smtpServer.starttls()
smtpServer.ehlo()
print "[+] Logging into the mail server [+]"
smtpServer.login(user, pwd)
print "[+] Sending your mail [+]"
smtpServer.sendmail(user, to, msg.as_string())
smtpServer.close()
print "[+] Your mail has been sent [+]"
except:
print "[-] Your message failed to send [-]"
def database():
conn = sqlite3.connect(master_db)
cursor = conn.cursor()
loc_stmt="SELECT first_name, surname,dob from student where license_ordered = 'N';"
cursor.execute(loc_stmt)
part1="Can I please order the licenses for the following students please \n\n"
part3="\n\nRegards\nCraig"
os.remove(scripts+'/output/license_order.txt')
while True:
row = cursor.fetchone()
if row == None:
break
print row[0].ljust(15), row[1].ljust(15),row[2]
f=open(scripts+'/output/license_order.txt','a')
f.write(row[0]+'\t'+row[1]+'\t'+row[2]+'\n')
f.flush()
f.close()
user = 'username'
pwd = 'password'
with open(scripts+'/output/license_order.txt','r') as myfile:
data = myfile.read()
mylist=data.splitlines()
for part2 in mylist:
print part2
#message = part1 + str(mylist) + part3
print part2
message = part1 + part2 + part3
sendMail(user, pwd, 'craig@craig.com','License Orders',message)
def main():
global tdate
tdate=strftime("%d-%m-%y")
database()
if __name__ == '__main__':
main()