简体   繁体   中英

Cannot send datetime via SMTPLIB Python

I am trying to make a program that'd email me the date and time whenever it is run. I am using smtlib and verified it already using some string as message and it is working fine. But whenever I add datetime variable and convert it to string it sends an empty email.

import smtplib
import datetime

b=datetime.datetime.time(datetime.datetime.now())

print b

svr = smtplib.SMTP("smtp.gmail.com:587")
svr.starttls()
svr.login("******@*****", "*********") 

msg = str(b)


svr.sendmail("******@*****", "******@*****", msg)
print ("Terminate")

svr.quit()

You should add headers to your msg :

headers  = "From: From Person \r\n"
headers += "To: To Person \r\n"
headers += "Subject: \r\n"
headers += "\r\n"
msg = headers + msg

This is kinda messy but it works, what I have have done is but b in a list called c and sent the list instead, unfortunately I have no idea why what you where doing doesn't work... Here's the code:

import smtplib
import datetime
b = datetime.datetime.time(datetime.datetime.now())
c = []
c.insert(0, b)
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login("***********", "***********")

msg = '' + str(c) + ''
server.sendmail("**********", "*********", msg)
server.quit()
raise SystemExit   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM