Here
Here

Reputation: 57

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

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!

Upvotes: 3

Views: 28001

Answers (2)

Devesh Sharma
Devesh Sharma

Reputation: 975

Best way to send an email using CRON is to use Postfix and mailutils. Follow below steps to send email with cron job results/errors.

Step 1 — Installing Postfix

First, update the package database:

sudo apt update

Next, install mailtuils:

sudo apt install mailutils

Finally, install postfix:

sudo apt install postfix

Near the end of the installation process, you will be presented with a window that looks like the one in the image below. The default option is Internet Site. That’s the recommended option for this tutorial, so press TAB, then ENTER

Select No Configuration

Select No Configuration

Step 2 — Configuring Postfix

sudo nano /etc/postfix/main.cf

Then paste below code on the empty file.

mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = loopback-only

mydestination = $myhostname, localhost.$your_domain, $your_domain

Save and close the file. Finally, restart Postfix.

sudo systemctl restart postfix

Step 3 — Testing the SMTP Server

echo "This is the body of the email" | mail -s "This is the subject line" your_email_address

Upvotes: 0

Pedro
Pedro

Reputation: 383

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.

Upvotes: 5

Related Questions