电子邮件-通过python通过sendmail发送邮件
如果我不想通过SMTP而是通过sendmail发送邮件,是否有用于封装此过程的python库?
更好的是,是否有一个好的库可以抽象整个“ sendmail -versus- smtp”选择?
我将在大量的Unix主机上运行此脚本,其中只有一些在localhost:25上侦听; 其中一些是嵌入式系统的一部分,不能设置为接受SMTP。
作为优良作法的一部分,我真的很想让库自己解决标头注入漏洞-因此,仅将字符串转储到popen('/usr/bin/sendmail', 'w')就比我想要的更接近金属了。
如果答案是“去写一个库”,那就去吧;-)
Nate asked 2019-09-27T05:51:59Z
7个解决方案
112 votes
标头注入不是发送邮件的方式,而是构建邮件的方式。 检查电子邮件程序包,使用该程序包构造邮件,对其进行序列化,然后使用子过程模块将其发送至/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())
Jim answered 2019-09-27T05:52:14Z
32 votes
这是一个简单的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
Pieter answered 2019-09-27T05:52:39Z
10 votes
Jim的答案在Python 3.4中对我不起作用。 我必须在subrocess.Popen()中添加一个额外的universal_newlines=True参数
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
MEI answered 2019-09-27T05:53:11Z
3 votes
仅使用os.popen从Python使用sendmail命令是很常见的
就个人而言,对于我自己没有编写的脚本,我认为仅使用SMTP协议会更好,因为它不需要安装说sendmail克隆即可在Windows上运行。
[https://docs.python.org/library/smtplib.html]
tovare answered 2019-09-27T05:53:50Z
3 votes
这个问题很老,但是值得一提的是,自从问这个消息开始,就有一个称为Marrow Mailer(以前称为TurboMail)的消息构建和电子邮件传递系统。
现在正在移植以支持Python 3,并已作为Marrow套件的一部分进行了更新。
amcgregor answered 2019-09-27T05:54:22Z
-3 votes
我只是在寻找同样的东西,并在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服务器,但是您可能要仔细检查是否遇到任何问题。
elec3647 answered 2019-09-27T05:55:02Z
-6 votes
最简单的答案是smtplib,您可以在此处找到相关文档。
您需要做的就是将本地sendmail配置为接受来自localhost的连接,默认情况下它可能已经这样做。 当然,您仍在使用SMTP进行传输,但这是本地sendmail,与使用命令行工具基本相同。
Frank Wiles answered 2019-09-27T05:55:35Z