[英]How to send mail only using web.py in python?
I have a web.py project with a notice module - the system will notice users by sending an email with HTML format . 我有一个带有通知模块的web.py项目-系统将通过发送HTML格式的电子邮件来通知用户。
I know how to send HTML format email in python .(Also desc in this Q sendmail with HTML message ) , and also know the sendmail() funciton in web.py (version 0.37). 我知道如何在python中发送HTML格式的电子邮件。(此Q sendmail中也包含HTML消息中的 desc),还知道web.py(版本0.37)中的sendmail()函数。
import web
web.config.smtp_server = 'smtp.gmail.com'
web.config.smtp_port = 587
web.config.smtp_username = 'goooooooooooooogle@gmail.com'
web.config.smtp_password = '*********'
web.config.smtp_starttls = True
web.sendmail('goooooooooooooogle@gmail.com',['1361877@gmail.com'],'Hello nodexy','This email is from web.py !')
I expect : 我预计 :
web.sendmail('goooooooooooooogle@gmail.com',['1361877@gmail.com'],'Hello nodexy', '<html><img src="hello.png"/></html>')
Now how can I fix this in web.py ? 现在如何在web.py中修复此问题? I'm sure I can not set the HTML string to the sendmail() function .
我确定我不能将HTML字符串设置为sendmail()函数。
send html mail, add a key to headers: 发送html邮件,向标题添加密钥:
web.sendmail(from_address, to_address, subject, msg, headers={'Content-Type':'text/html;charset=utf-8'})
in web.py utils.py, see _EmailMessage's prepare_message method: 在web.py utils.py中,请参见_EmailMessage的prepare_message方法:
def prepare_message(self):
for k, v in self.headers.iteritems():
if k.lower() == "content-type":
self.message.set_type(v)
else:
self.message.add_header(k, v)
self.headers = {}
I get it from help(web.sendmail) in web.py ! 我从web.py的help(web.sendmail)获得它!
>>> import web
>>> help(web.sendmail)
Help on function sendmail in module web.utils: web.utils模块中有关sendmail功能的帮助:
sendmail(from_address, to_address, subject, message, headers=None, **kw)
Sends the email message message
with mail and envelope headers for from from_address_
to to_address
with subject
. 发送电子邮件
message
与电子邮件和信封头从from_address_
到to_address
与subject
。 Additional email headers can be specified with the dictionary `headers. 附加的电子邮件标题可以用字典`headers指定。
Optionally cc, bcc and attachments can be specified as keyword arguments. 可以选择将cc,密件抄送和附件指定为关键字参数。 Attachments must be an iterable and each attachment can be either a filename or a file object or a dictionary with filename, content and optionally content_type keys.
附件必须是可迭代的,每个附件可以是文件名或文件对象,也可以是具有文件名,内容和可选的content_type键的字典。
As @number23_cn 's answer, just add this key 'Content-Type' to headers . 作为@ number23_cn的答案,只需将此键“ Content-Type”添加到headers中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.