附件html是什么,以附件形式发送html文件时出现问题

我试图用python以.pdf或.html文件作为附件发送电子邮件。

def send_mail(app, sub, body, filename=None):

FROM = Config.Email.FROM_ADDRESS

TO = Config.Email.FAIL_RECIPIENTS

sender = Mailer(Config.Email.SMTP_SERVER)

message = Message(From = FROM, To = TO)

message.Subject = sub

message.Html = body

if filename is not None:

if not os.path.exists(filename):

message.Subject = sub + " " + '[Attachment is missing as the file path in invalid.]'

sender.send(message)

return

else:

message.attach(filename)

try:

sender.send(message)

return

except AttributeError:

ctype, encoding = mimetypes.guess_type(filename)

maintype, subtype = ctype.split('/', 1)

if maintype == 'text':

fp = open(filename)

attach = MIMEText(fp.read(), _subtype=subtype)

fp.close()

attach.add_header('Content-Disposition', 'attachment', filename=filename)

message.attach(filename)

sender.send(message)

return

sender.send(message)

if __name__ == "__main__":

send_mail()

Traceback (most recent call last):

File "C:/git_workspace/proj/core/common_utils.py", line 65, in send_mail

sender.send(message)

File "C:\Python_VEnv\automation\lib\site-packages\mailer.py", line 124, in send

self._send(server, m)

File "C:\Python_VEnv\automation\lib\site-packages\mailer.py", line 161, in _send

server.sendmail(me, you, msg.as_string())

File "C:\Python_VEnv\automation\lib\site-packages\mailer.py", line 244, in as_string

return self._multipart()

File "C:\Python_VEnv\automation\lib\site-packages\mailer.py", line 349, in _multipart

self._add_attachment(msg, filename, cid, mimetype, content, charset)

File "C:\Python_VEnv\automation\lib\site-packages\mailer.py", line 375, in _add_attachment

msg = MIMEText(content, _subtype=subtype, _charset=charset)

File "C:\Python3\lib\email\mime\text.py", line 34, in __init__

_text.encode('us-ascii')

AttributeError: 'bytes' object has no attribute 'encode'

我错过了什么?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值