简体   繁体   English

如何在 python 中使用 cron 和 smtp 安排电子邮件在特定时间发送?

[英]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 = 'myemail@gmail.com'
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!如果有人也能帮我弄清楚如何添加附件,那就太好了!

Assuming you already have your send_email() function working I would do:假设您已经使用send_email()函数,我会这样做:

import datetime as dt
import time
import smtplib

def send_email():
    email_user = 'myemail@gmail.com'
    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 = 'myemail@gmail.com'
    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.您还可以生成一个线程并让该线程处理电子邮件的发送。

Best way to send an email using CRON is to use Postfix and mailutils.使用 CRON 发送电子邮件的最佳方式是使用 Postfix 和 mailutils。 Follow below steps to send email with cron job results/errors.按照以下步骤发送包含 cron 作业结果/错误的电子邮件。

Step 1 — Installing Postfix步骤 1 — 安装 Postfix

First, update the package database:首先,更新包数据库:

sudo apt update

Next, install mailtuils:接下来,安装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.默认选项是 Internet 站点。 That's the recommended option for this tutorial, so press TAB, then ENTER这是本教程的推荐选项,因此请按 TAB,然后按 ENTER

Select No Configuration选择无配置

选择无配置

Step 2 — Configuring Postfix第 2 步 - 配置 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.最后,重启 Postfix。

sudo systemctl restart postfix

Step 3 — Testing the SMTP Server步骤 3 — 测试 SMTP 服务器

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM