How do I schedule an email to send at a certain time using cron and smtp, in python?

17,408

Assuming you already have your send_email() function working I would do:

import datetime as dt
import time
import smtplib

def send_email():
    email_user = '[email protected]'
    server = smtplib.SMTP ('smtp.gmail.com', 587)
    server.starttls()
    server.login(email_user, 'email pass')

    #EMAIL
    message = 'sending this from python!'
    server.sendmail(email_user, email_user, message)
    server.quit()

send_time = dt.datetime(2018,8,26,3,0,0) # set your sending time in UTC
time.sleep(send_time.timestamp() - time.time())
send_email()
print('email sent')

If you want to send the email regularly, you can do:

import datetime as dt
import time
import smtplib

def send_email():
    email_user = '[email protected]'
    server = smtplib.SMTP ('smtp.gmail.com', 587)
    server.starttls()
    server.login(email_user, 'email pass')

    #EMAIL
    message = 'sending this from python!'
    server.sendmail(email_user, email_user, message)
    server.quit()

def send_email_at(send_time):
    time.sleep(send_time.timestamp() - time.time())
    send_email()
    print('email sent')

first_email_time = dt.datetime(2018,8,26,3,0,0) # set your sending time in UTC
interval = dt.timedelta(minutes=2*60) # set the interval for sending the email

send_time = first_email_time
while True:
    send_email_at(send_time)
    send_time = send_time + interval

You can also spawn a thread and leave this thread handle the sending of the email.

Share:
17,408
Here
Author by

Here

Updated on June 05, 2022

Comments

  • Here
    Here almost 2 years

    So far I have only been able to send emails. Here's my code:

    import smtplib
    
    email_user = '[email protected]'
    server = smtplib.SMTP ('smtp.gmail.com', 587)
    server.starttls()
    server.login(email_user, 'email pass')
    
    #SET TIME HERE?
    from crontab import CronTab
    
    #EMAIL
    message = 'sending this from python!'
    server.sendmail(email_user, email_user, message)
    server.quit()
    

    I'm struggling to set a time to send the email. If someone can also help me figure out how to add attachments, that would be great!

    • T.Woody
      T.Woody over 5 years
      What is your os?
    • Here
      Here over 5 years
      Windows is my os
  • Here
    Here over 5 years
    I'm sorry I'm really new at this. Can you explain what the values mean inside the parentheses in line 4?
  • Pedro
    Pedro over 5 years
    @Here the format is dt.datetime(year, month, day, hour, minute, second)
  • Here
    Here over 5 years
    I'm getting the error that send_email is not defined.
  • Pedro
    Pedro over 5 years
    That's why I wrote "assuming you already have your send_email() function working", just put your code to send the email under a send_email() function.
  • Pedro
    Pedro over 5 years
    @Here great, please accept my answer so I can have enough points to comment on other people's questions :P