![]() |
smtplib send email has no timestamp - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: smtplib send email has no timestamp (/thread-42278.html) |
smtplib send email has no timestamp - Pedroski55 - Jun-10-2024 I want to send emails to customers automatically. So I am testing with just 5 email addresses to see how it works I import these import smtplib, ssl import json # for a list of email addresses from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipartOne test email did not get sent, the problem was: Quote:No Mx Record Found Must be a wrong email, because even when I tried to send from my email it bounced back! But i noticed this: the time of the email which I sent from this laptop is the start of the computer epoch time (well, not quite): Quote:时 间:1970-01-01 07:59:59 My laptop shows the current time OK! How can I get smtplib to put the current timestamp? This sends the emails from my laptop via my QQ email OK, except for the timestamp: with smtplib.SMTP_SSL(smtp_server, port, context=context) as server: server.login(sender_email, password) for e in email_address(customer_emails): print(f'email is {e} ') server.sendmail(sender_email, e, message) print(f'email sent to {r}')I don't know why the timestamp is 1970! RE: smtplib send email has no timestamp - IslaBrown - Jun-10-2024 I found out that the issue maybe related to the email headers. Specifically, the Date header wasn't being set correctly in email message, which is why it defaulted to the Unix epoch time. Here's the updated code that can solve the issue: python Copy code import smtplib, ssl from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart from email.utils import formatdate # Load email addresses from JSON or other source customer_emails = ["[email protected]", "[email protected]", "[email protected]", "[email protected]", "[email protected]"] smtp_server = "smtp.qq.com" port = 465 sender_email = "[email protected]" password = "your_password" # Create the email message message = MIMEMultipart() message["From"] = sender_email message["Subject"] = "Test Email" message.attach(MIMEText("This is a test email.", "plain")) message["Date"] = formatdate(localtime=True) context = ssl.create_default_context() with smtplib.SMTP_SSL(smtp_server, port, context=context) as server: server.login(sender_email, password) for e in customer_emails: print(f'email is {e}') server.sendmail(sender_email, e, message.as_string()) print(f'email sent to {e}')Adding the Date header with formatdate(localtime=True) ensures that the current local time is correctly set in the email header. RE: smtplib send email has no timestamp - Pedroski55 - Jun-11-2024 message["Date"] = formatdate(localtime=True)Thanks! I slowly figured out that I needed to set the date, but I did not know how. I used datetime to get the date and put that in dt = datetime.now() date = dt.strftime('%Y-%m-%d %H:%M:%S') message["Date"] = dateI find sending email like this is very sensitive. I have 5 emails for testing. 2 are gmail. They got blocked. Not RFC 5322 compliant. I was sending the emails from a list of 5 emails. I noticed that the header got bigger, for each email, the header was added again, and I saw a message that there were multiple To. I think, using message = MIMEMultipart()the message persists. I put the code to assemble the email in a function, so that, when the function ends, everything is gone: context = ssl.create_default_context() server = smtplib.SMTP_SSL(smtp_server, port, context=context) server.login(sender_email, password) # later put a loop here to loop over the list of emails send_email(filedata, fname, sender_email, password, to_email, subject, body_text, server) # end loop server.quit()Just using 1 email, this is now accepted by gmail, but for my 2 yahoo mail addresses I get this message from the QQ Postmaster: Quote:No Mx Record Found The email is coming from QQ, which is Chinese, so maybe Yahoo does not like QQ! Today I thought I would try sending from via Yahoo, see it that works! All just to keep the girlfriend happy! ![]() ![]() ![]() |