如果我想不通过 SMTP 发送邮件,而是通过 sendmail 发送邮件,是否有一个 python 库封装了这个过程?

更好的是,是否有一个好的库可以抽象出整个“sendmail - 与 smtp”选择?

我将在一堆 UNIX 主机上运行此脚本,其中只有一些主机正在侦听 localhost:25;其中一些是嵌入式系统的一部分,无法设置为接受 SMTP。

作为良好实践的一部分,我真的想让库本身处理标头注入漏洞——所以只需将字符串转储到 popen('/usr/bin/sendmail', 'w') 比我想要的更接近金属。

如果答案是“去写一个库”,那就这样吧;-)

有帮助吗?

解决方案

标头注入不是影响您如何发送邮件的因素,而是影响您如何构建邮件的因素。检查 电子邮件 打包,用它构建邮件,序列化它,并将其发送到 /usr/sbin/sendmail 使用 子流程 模块:

from email.mime.text import MIMEText
from subprocess import Popen, PIPE

msg = MIMEText("Here is the body of my message")
msg["From"] = "me@example.com"
msg["To"] = "you@example.com"
msg["Subject"] = "This is the subject."
p = Popen(["/usr/sbin/sendmail", "-t", "-oi"], stdin=PIPE)
p.communicate(msg.as_string())

其他提示

这是一个简单的 python 函数,使用 unix sendmail 来发送邮件。

def sendMail():
    sendmail_location = "/usr/sbin/sendmail" # sendmail location
    p = os.popen("%s -t" % sendmail_location, "w")
    p.write("From: %s\n" % "from@somewhere.com")
    p.write("To: %s\n" % "to@somewhereelse.com")
    p.write("Subject: thesubject\n")
    p.write("\n") # blank line separating headers from body
    p.write("body of the mail")
    status = p.close()
    if status != 0:
           print "Sendmail exit status", status

Jim 的答案在 Python 3.4 中对我不起作用。我必须添加一个额外的 universal_newlines=True 论证 subrocess.Popen()

from email.mime.text import MIMEText
from subprocess import Popen, PIPE

msg = MIMEText("Here is the body of my message")
msg["From"] = "me@example.com"
msg["To"] = "you@example.com"
msg["Subject"] = "This is the subject."
p = Popen(["/usr/sbin/sendmail", "-t", "-oi"], stdin=PIPE, universal_newlines=True)
p.communicate(msg.as_string())

如果没有 universal_newlines=True 我明白了

TypeError: 'str' does not support the buffer interface

使用 os.popen 从 Python 中使用 sendmail 命令是很常见的

就我个人而言,对于我自己没有编写的脚本,我认为仅使用 SMTP 协议更好,因为它不需要安装例如 sendmail 克隆即可在 Windows 上运行。

https://docs.python.org/library/smtplib.html

这个问题很老了,但值得注意的是,有一个消息构造和电子邮件传递系统称为 骨髓邮递员 (以前的 TurboMail)在询问此消息之前就已经可用。

它现在正在移植以支持 Python 3,并作为 骨髓 套房。

我只是在寻找同样的东西,并在 Python 网站上找到了一个很好的例子: http://docs.python.org/2/library/email-examples.html

从提到的网站:

# Import smtplib for the actual sending function
import smtplib

# Import the email modules we'll need
from email.mime.text import MIMEText

# Open a plain text file for reading.  For this example, assume that
# the text file contains only ASCII characters.
fp = open(textfile, 'rb')
# Create a text/plain message
msg = MIMEText(fp.read())
fp.close()

# me == the sender's email address
# you == the recipient's email address
msg['Subject'] = 'The contents of %s' % textfile
msg['From'] = me
msg['To'] = you

# Send the message via our own SMTP server, but don't include the
# envelope header.
s = smtplib.SMTP('localhost')
s.sendmail(me, [you], msg.as_string())
s.quit()

请注意,这要求您正确设置 sendmail/mailx 以接受“localhost”上的连接。默认情况下,这适用于我的 Mac、Ubuntu 和 Redhat 服务器,但您可能需要仔细检查是否遇到任何问题。

最简单的答案是 smtplib,你可以在上面找到文档 这里.

您需要做的就是配置本地 sendmail 以接受来自本地主机的连接,默认情况下它可能已经这样做了。当然,您仍然使用 SMTP 进行传输,但它是本地 sendmail,这与使用命令行工具基本相同。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top