Python Forum
smtplib send email has no timestamp
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
smtplib send email has no timestamp
#1
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 MIMEMultipart
One 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
Time: 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!
Reply
#2
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.
deanhystad write Jun-10-2024, 02:28 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Pedroski55 likes this post
Reply
#3
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"] = date
I 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! Big Grin Big Grin Big Grin
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Send email with smtp without using Mimetext mgallotti 0 794 Feb-01-2023, 04:43 AM
Last Post: mgallotti
  email Library: properly send message as quoted-printable malonn 3 1,506 Nov-14-2022, 09:31 PM
Last Post: malonn
  Unable to send email attachments cosmarchy 7 2,827 Mar-09-2022, 09:29 PM
Last Post: bowlofred
  How to make scraper send email notification just once themech25 0 1,468 Nov-08-2021, 01:51 PM
Last Post: themech25
  Sending Attachments via smtplib [SOLVED] AlphaInc 3 2,292 Oct-28-2021, 12:39 PM
Last Post: AlphaInc
  Sending random images via smtplib [SOLVED] AlphaInc 0 1,760 Oct-19-2021, 10:10 AM
Last Post: AlphaInc
  [UnicodeEncodeError from smtplib] yoohooos 0 3,514 Sep-25-2021, 04:27 AM
Last Post: yoohooos
  send email smtp rwahdan 0 1,867 Jun-19-2021, 01:28 AM
Last Post: rwahdan
  Need Outlook send email code using python srikanthpython 3 8,597 Feb-28-2021, 01:53 PM
Last Post: asyswow64
  Understanding The Arguments for SMTPlib - sendmail JoeDainton123 3 2,836 Aug-03-2020, 08:34 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020