Nat*_*ate 71 python email sendmail
如果我不是通过SMTP发送邮件,而是通过sendmail发送邮件,是否有一个用于封装此进程的python库?
更好的是,是否有一个好的库可以抽象出整个'sendmail -versus-smtp'的选择?
我将在一堆unix主机上运行这个脚本,其中只有一些正在监听localhost:25; 其中一些是嵌入式系统的一部分,无法设置为接受SMTP.
作为良好实践的一部分,我真的很想让库自己处理标题注入漏洞 - 所以只需要将字符串倾斜popen('/usr/bin/sendmail', 'w')到比我想要的金属更接近金属.
如果答案是'去写一个库',那就这样吧;-)
Jim*_*Jim 118
标题注入不是您发送邮件的因素,它是您构建邮件的一个因素.检查电子邮件包,使用它构建邮件,将其序列化,然后/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())
Pie*_*ter 35
这是一个简单的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
小智 12
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
Python 3.5+版本:
import subprocess
from email.message import EmailMessage
def sendEmail(from_addr, to_addrs, msg_subject, msg_body):
    msg = EmailMessage()
    msg.set_content(msg_body)
    msg['From'] = from_addr
    msg['To'] = to_addrs
    msg['Subject'] = msg_subject
    sendmail_location = "/usr/sbin/sendmail"
    subprocess.run([sendmail_location, "-t", "-oi"], input=msg.as_bytes())
这个问题很老了,但值得注意的是,有一个名为Marrow Mailer(以前称为TurboMail)的消息构建和电子邮件传递系统,在询问此消息之前就已经可用。
它现在被移植以支持 Python 3 并作为Marrow套件的一部分进行更新。
| 归档时间: | 
 | 
| 查看次数: | 75743 次 | 
| 最近记录: |