2008-09-16 94 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