在另一个线程中以python发送电子邮件

动机 (Motivation)

Sending emails may seem to be a simple task. But there is some complicated stuff to consider:

发送电子邮件似乎是一个简单的任务。 但是有一些复杂的事情要考虑:

  1. An email should be sent in a parallel thread, so it does not pause the whole project

    电子邮件应在并行线程中发送,因此不会暂停整个项目
  2. An email should be beautiful and support different formats — Text and HTML.

    电子邮件应美观,并支持不同的格式-文本和HTML。
  3. Security issues. Does your email server require SSL? Sending from Gmail.

    安全问题。 您的电子邮件服务器是否需要SSL? 从Gmail发送。

简单的电子邮件发送 (Simple Email send)

Sending emails is no rocket science in Python. As usual, batteries are already included, so we need to insert them:

在Python中,发送电子邮件并不是科学。 和往常一样,电池已经包含在内,因此我们需要插入它们:

import smtplib, ssl
from email.mime.text
import MIMETextfrom email.mime.multipart
import MIMEMultipart

Now, as batteries are in, we only need to press the red button:

现在,随着电池的插入,我们只需要按下红色按钮:

server = smtplib.SMTP(email_smtp_server, email_port))
server.login(email_login, email_password)
server.sendmail(email_from, email_to, message.as_string())

Ooops, what are all these variables? Sure, we need to supply our code with the email properties, subject, and email text.

糟糕,这些变量都是什么? 当然,我们需要为我们的代码提供电子邮件属性,主题和电子邮件文本。

Email properties are the email_smtp_server, which is a simple string, and the email_port, which is an integer.

电子邮件属性包括email_smtp_server (这是一个简单的字符串)和email_port (这是一个整数)。

All the rest goes to the message variable, which is something more complex:

其余的所有信息都传递给message变量,该变量更为复杂:

body = "<html>"
body += "<h2>Guten Tag!</h2><br><br>"body += "<b>This is an automatic email.</b><br>"body += "Please do not reply.<br><br>"body += "Best regards,<br>"body += "Your server.<br>"body += '<img src="http://realitycheckbbs.org/images/fido.jpg">'body += "</html>"

We must be old email client-friendly, so let us create a plain text here:

我们必须是旧的电子邮件客户端友好型,因此让我们在此处创建纯文本:

text = "Guten Tag!\n\n"text += "This is an automatic email.\n\n"text += "Please do not reply.\n\n"text += "Best regards,\n"text += "Your server."

OK, now the body is ready, let us create the message itself:

好,现在主体已准备好,让我们创建消息本身:

message = MIMEMultipart("alternative")
message["Subject"] = "Here goes the subject"
message["From"] = "someemail@server.com"
message["To"] = "spam_victim@hisemail.com"part1 = MIMEText(text, "plain")
part2 = MIMEText(body, "html")
message.attach(part1)
message.attach(part2)

And all this stuff can now be supplied to the code above.

现在,所有这些东西都可以提供给上面的代码。

If you only need to send an email, then we are done. But for this, you could also use Microsoft Outlook.

如果您只需要发送电子邮件,那么我们就完成了。 但是,为此,您也可以使用Microsoft Outlook。

使用SSL发送电子邮件 (Sending Emails with SSL)

Sending emails that are secured with SSL, like Gmail, is almost as simple.

发送使用SSL保护的电子邮件(例如Gmail)几乎一样简单。

We only need to create a context for SSL and user the SMTP_SSL function, like here:

我们只需要为SSL创建一个上下文并使用SMTP_SSL函数,如下所示:

context = ssl.create_default_context()with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server:
server.login("youremail@gmail.com", password)
server.sendmail(
sender_email, receiver_email, message.as_string()
)

Warning! If you are sending emails with Gmail, before taking it to production, try to send around 50 emails first. Gmail will stop sending emails, this is not a bug, this is a feature. You have to allow Gmail to send emails “Less Secure Apps”, like yours. Here is more information: https://myaccount.google.com/lesssecureapps

警告! 如果您要通过Gmail发送电子邮件,请先尝试发送约50封电子邮件,然后再进行生产。 Gmail将停止发送电子邮件,这不是错误,这是一项功能。 您必须允许Gmail像您一样发送“ Less Secure Apps”电子邮件。 以下是更多信息: https : //myaccount.google.com/lesssecureapps

从其他线程发送电子邮件 (Sending Emails from a different thread)

If you are sending emails from Django, it is very important to send emails from another thread, since your users will not see anything in their web browsers until all emails are sent.

如果您是从Django发送电子邮件,那么从另一个线程发送电子邮件非常重要,因为在发送所有电子邮件之前,您的用户将不会在网络浏览器中看到任何内容。

Yep, if you are writing a really big project and expect tons of emails send all the time, you should use something like this: https://www.rabbitmq.com/tutorials/tutorial-one-python.html

是的,如果您正在编写一个非常大的项目,并且希望所有时间都在发送大量电子邮件,则应该使用类似以下的内容: https : //www.rabbitmq.com/tutorials/tutorial-one-python.html

But we are now talking about a simple second Thread inside our application.

但是我们现在正在谈论应用程序内部的一个简单的第二个线程。

We start with inserting the batteries:

我们从插入电池开始:

from threading import Thread

And now we can create a Class.

现在我们可以创建一个类。

class EmailThread(Thread):def __init__(self, email_to):
self.email_to = emal_to
Thread.__init__(self)def run (self):
body = "your html body"text = "your plain body"message = MIMEMultipart("alternative")
message["Subject"] = "subject"
message["From"] = "your@email.com"
message["To"] = email_to part1 = MIMEText(text, "plain")
part2 = MIMEText(body, "html")
message.attach(part1)
message.attach(part2)
server = smtplib.SMTP(smtp_server, email_port)
server.login(email_login, email_password)
server.sendmail(your_email, email_to, message.as_string())

Here we only pass one parameter: email_to. Sure it may be extended to whatever parameters you need: text body, server properties, etc.

在这里,我们仅传递一个参数:email_to。 当然,它可以扩展到所需的任何参数:文本正文,服务器属性等。

We are now ready to send emails from another thread:

现在,我们准备从另一个线程发送电子邮件:

EmailThread("spam_victim@gmail.com").start()

That’s it. Who said working with threads is complicated?

而已。 谁说处理线程很复杂?

Please have a look at my other articles:

请看看我的其他文章:

翻译自: https://levelup.gitconnected.com/sending-emails-in-python-in-a-different-thread-b779347a4a8b

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值