Python被宣传为“包含电池”的语言。因此,我想知道为什么它的标准库不包括对电子邮件的高级支持:
我发现,您需要了解许多关于MIME的知识,才能创建与典型电子邮件客户端相同的电子邮件消息,例如处理HTML内容、嵌入式图像和文件附件。
要实现这一点,您需要执行一个消息的低级程序集,例如:
MIMEMultipart部分,了解related、alternate等。base64如果你只是在学习MIME来组装这样的电子邮件,那么很容易陷入陷阱,比如糟糕的部分嵌套,并创建一些电子邮件客户端可能无法正确查看的消息。
我不应该知道关于MIME才能正确发送电子邮件。高级库支持应该是封装所有这些MIME逻辑,允许您编写如下内容:
m = Email("mailserver.mydomain.com")
m.setFrom("Test User <test@mydomain.com>")
m.addRecipient("you@yourdomain.com")
m.setSubject("Hello there!")
m.setHtmlBody("The following should be <b>bold</b>")
m.addAttachment("/home/user/image.png")
m.send()非标准库解决方案是pyzmail
import pyzmail
sender=(u'Me', 'me@foo.com')
recipients=[(u'Him', 'him@bar.com'), 'just@me.com']
subject=u'the subject'
text_content=u'Bonjour aux Fran\xe7ais'
prefered_encoding='iso-8859-1'
text_encoding='iso-8859-1'
pyzmail.compose_mail(
sender, recipients,
subject, prefered_encoding, (text_content, text_encoding),
html=None,
attachments=[('attached content', 'text', 'plain', 'text.txt',
'us-ascii')])是否有任何原因不包括在“电池包括”标准图书馆?
发布于 2013-01-30 09:06:01
我认为问题更多的是电子邮件结构的复杂性,而不是Python中的smpt的弱点。
PHP中的这个例子看起来并不比您的Python示例简单。
发布于 2013-01-30 08:50:29
import smtplib
smtp = smtplib.SMTP()
smtp.connect()
smtp.sendmail("me@somewhere.com", ["you@elsewhere.com"],
"""Subject: This is a message sent with very little configuration.
It will assume that the mailserver is on localhost on the default port
(25) and also assume a message type of text/plain.
Of course, if you want more complex message types and/or server
configuration, it kind of stands to reason that you'd need to do
more complex message assembly. Email (especially with embedded content)
is actually a rather complex area with many possible options.
"""
smtp.quit()https://stackoverflow.com/questions/14599796
复制相似问题