2008-09-16 96 views
58

如果我不想通过SMTP发送邮件,而是通过sendmail发送邮件,是否有一个用于封装此过程的python库?从python通过sendmail发送邮件

更好的是,有没有一个很好的图书馆能够抽象出整个'sendmail -versus-smtp'的选择?

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

作为良好实践的一部分,我真的很想让图书馆自己处理标头注入漏洞 - 所以只是倾销字符串popen('/usr/bin/sendmail', 'w')比我想要的更接近金属。

如果答案是“去写一库”,所以不论是;-)

回答

97

标题注入不是发送邮件的方式的一个因素,它是构建邮件的一个因素。检查email包,构建邮件与,连载它,并使用subprocess模块发送到/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"] = "[email protected]" 
msg["To"] = "[email protected]" 
msg["Subject"] = "This is the subject." 
p = Popen(["/usr/sbin/sendmail", "-t", "-oi"], stdin=PIPE) 
p.communicate(msg.as_string()) 
3

是很常见的,只是使用来自Python的使用os.popen

个人sendmail命令,脚本的我没写自己,我认为只是使用SMTP协议更好,因为它不需要安装sendmail克隆来在Windows上运行。

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

+0

SMTP不使用像[DMA](https使用上〜nix中框:// github上。 com/corecode/dma),它提供了sendmail,但不在端口25上收听... – 2017-12-11 13:31:40

-6

最简单的答案是的smtplib,你可以在上面找到here文档。

您只需将本地sendmail配置为接受本地主机的连接,默认情况下它可能已经完成。当然,您仍然使用SMTP进行传输,但它是本地sendmail,与使用命令行工具基本相同。

+7

问题说:“如果我想通过SMTP发送邮件,而不是通过sendmail发送邮件......” – S19N 2012-05-31 10:41:59

+1

您的程序会延迟SMTP服务器没有连接或滞后。使用sendmail将消息传递给MTA,并继续执行程序。 – 2012-10-12 13:20:47

30

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

def sendMail(): 
    sendmail_location = "/usr/sbin/sendmail" # sendmail location 
    p = os.popen("%s -t" % sendmail_location, "w") 
    p.write("From: %s\n" % "[email protected]") 
    p.write("To: %s\n" % "[email protected]ewhereelse.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 
+0

真不错的python函数! – aphex 2012-06-19 20:36:42

+0

+1 - 我正在寻找的! – 2012-06-24 10:31:03

+0

卓越,简单的例子+1 – 2012-06-28 14:47:19

3

这个问题已经很老了,但它是值得的注意,消息结构和e邮件传送系统称为Marrow Mailer(以前为TurboMail),自此消息被询问之前已经可用。

它现在被移植到支持Python 3并作为Marrow套件的一部分进行更新。

-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的正确设置接受“本地主机”上的连接。这在默认情况下适用于我的Mac,Ubuntu和Redhat服务器,但您可能需要仔细检查是否遇到任何问题。

6

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"] = "[email protected]" 
msg["To"] = "[email protected]" 
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