0

通知モジュールを含む web.py プロジェクトがあります。システムは HTML 形式の電子メールを送信してユーザーに通知します。

私はPythonでHTML形式の電子メールを送信する方法を知っています.(このQ sendmail with 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 !')

私が期待する :

web.sendmail('goooooooooooooogle@gmail.com',['1361877@gmail.com'],'Hello nodexy', '<html><img src="hello.png"/></html>')

web.py でこれを修正するにはどうすればよいですか? HTML 文字列を sendmail() 関数に設定できないのは確かです。

4

2 に答える 2

1

HTMLメールを送信し、ヘッダーにキーを追加します。

web.sendmail(from_address, to_address, subject, msg, headers={'Content-Type':'text/html;charset=utf-8'})

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 = {}
于 2012-06-21T02:31:26.673 に答える
0

web.pyのhelp(web.sendmail)から取得します!

>>> import web
>>> help(web.sendmail)

モジュールweb.utilsの関数sendmailに関するヘルプ:

sendmail(from_address, to_address, subject, message, headers=None, **kw)

fromからtowithまでのmessageメールとエンベロープヘッダーを含む電子メールメッセージを送信します。追加の電子メールヘッダーは、辞書`headersで指定できます。from_address_to_addresssubject

オプションで、cc、bcc、および添付ファイルをキーワード引数として指定できます。添付ファイルは反復可能である必要があり、各添付ファイルは、ファイル名またはファイルオブジェクト、あるいはファイル名、コンテンツ、およびオプションでcontent_typeキーを持つ辞書のいずれかです。

@ number23_cnの答えとして、このキー「Content-Type」をヘッダーに追加するだけです。

于 2012-06-21T06:04:23.127 に答える