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
#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


Messages In This Thread
smtplib send email has no timestamp - by Pedroski55 - Jun-10-2024, 07:17 AM
RE: smtplib send email has no timestamp - by IslaBrown - Jun-10-2024, 01:52 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Send email with smtp without using Mimetext mgallotti 0 805 Feb-01-2023, 04:43 AM
Last Post: mgallotti
  email Library: properly send message as quoted-printable malonn 3 1,546 Nov-14-2022, 09:31 PM
Last Post: malonn
  Unable to send email attachments cosmarchy 7 2,890 Mar-09-2022, 09:29 PM
Last Post: bowlofred
  How to make scraper send email notification just once themech25 0 1,476 Nov-08-2021, 01:51 PM
Last Post: themech25
  Sending Attachments via smtplib [SOLVED] AlphaInc 3 2,327 Oct-28-2021, 12:39 PM
Last Post: AlphaInc
  Sending random images via smtplib [SOLVED] AlphaInc 0 1,772 Oct-19-2021, 10:10 AM
Last Post: AlphaInc
  [UnicodeEncodeError from smtplib] yoohooos 0 3,537 Sep-25-2021, 04:27 AM
Last Post: yoohooos
  send email smtp rwahdan 0 1,883 Jun-19-2021, 01:28 AM
Last Post: rwahdan
  Need Outlook send email code using python srikanthpython 3 8,647 Feb-28-2021, 01:53 PM
Last Post: asyswow64
  Understanding The Arguments for SMTPlib - sendmail JoeDainton123 3 2,869 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