如何将 html 文件作为附件添加到 python 中?
how to add the html file as an attachment in python?
matches = []
for root, dirnames, filenames in os.walk('C:\Users\Desktop\ADI\New folder'):
for filename in fnmatch.filter(filenames, '*.html'):
matches.append(os.path.join(root, filename))
page = filename
#print filename
server.quit()
在上面的代码中:首先,我在目录中找到 *.html 文件。我正在找到它,它对我来说工作正常。稍后我想将该 html 文件作为电子邮件附件发送给某人。我在这方面失败了。有人可以建议我如何将文件附加到电子邮件并将其发送给相关人员吗?
上面的程序在将电子邮件发送给此人时运行良好,它只是在电子邮件中打印文件的名称,但无法附加该电子邮件并发送它。
错误:
Traceback (most recent call last):
File ".\task.py", line 39, in <module>
server.sendmail(fromaddress,toaddress,msg.as_string())
File "C:\Python27_3\lib\email\message.py", line 137, in as_string
g.flatten(self, unixfrom=unixfrom)
File "C:\Python27_3\lib\email\generator.py", line 83, in flatten
self._write(msg)
File "C:\Python27_3\lib\email\generator.py", line 115, in _write
self._write_headers(msg)
File "C:\Python27_3\lib\email\generator.py", line 164, in _write_headers
v, maxlinelen=self._maxheaderlen, header_name=h).encode()
File "C:\Python27_3\lib\email\header.py", line 410, in encode
value = self._encode_chunks(newchunks, maxlinelen)
File "C:\Python27_3\lib\email\header.py", line 370, in _encode_chunks
_max_append(chunks, s, maxlinelen, extra)
File "C:\Python27_3\lib\email\quoprimime.py", line 97, in _max_append
L.append(s.lstrip())
AttributeError: 'tuple' object has no attribute 'lstrip'
需要将文件内容附加到电子邮件数据。
例如
with open(fileToSend) as fp:
attachment = MIMEText(fp.read(), _subtype=subtype)
attachment.add_header("Content-Disposition", "attachment",\
filename=os.path.basename(filename))
msg.attach(attachment)
在您的代码中执行以下操作(将 msg.attach(MIMEText(text))
行替换为以下代码):
ctype, encoding = mimetypes.guess_type(filename)
if ctype is None or encoding is not None:
ctype = "application/octet-stream"
maintype, subtype = ctype.split("/", 1)
with open(filename) as fp:
attachment = MIMEText(fp.read(), _subtype=subtype)
attachment.add_header("Content-Disposition", "attachment",\
filename=os.path.basename(filename))
msg.attach(attachment)
注意: filename
应该是我们要在电子邮件中附加的文件的完整路径。例如/var/opt/html/report_email.html
如果我添加多个收件人电子邮件 ID,则它不起作用。
- if
toaddress
是字符串,其中每个电子邮件 ID 用逗号分隔,即 ,
例如"abc@gmail.com,xyz@gmail.com"
然后喜欢 server.sendmail(fromaddress,toaddress.split(','),msg.as_string())
matches = []
for root, dirnames, filenames in os.walk('C:\Users\Desktop\ADI\New folder'):
for filename in fnmatch.filter(filenames, '*.html'):
matches.append(os.path.join(root, filename))
page = filename
#print filename
server.quit()
在上面的代码中:首先,我在目录中找到 *.html 文件。我正在找到它,它对我来说工作正常。稍后我想将该 html 文件作为电子邮件附件发送给某人。我在这方面失败了。有人可以建议我如何将文件附加到电子邮件并将其发送给相关人员吗? 上面的程序在将电子邮件发送给此人时运行良好,它只是在电子邮件中打印文件的名称,但无法附加该电子邮件并发送它。
错误:
Traceback (most recent call last):
File ".\task.py", line 39, in <module>
server.sendmail(fromaddress,toaddress,msg.as_string())
File "C:\Python27_3\lib\email\message.py", line 137, in as_string
g.flatten(self, unixfrom=unixfrom)
File "C:\Python27_3\lib\email\generator.py", line 83, in flatten
self._write(msg)
File "C:\Python27_3\lib\email\generator.py", line 115, in _write
self._write_headers(msg)
File "C:\Python27_3\lib\email\generator.py", line 164, in _write_headers
v, maxlinelen=self._maxheaderlen, header_name=h).encode()
File "C:\Python27_3\lib\email\header.py", line 410, in encode
value = self._encode_chunks(newchunks, maxlinelen)
File "C:\Python27_3\lib\email\header.py", line 370, in _encode_chunks
_max_append(chunks, s, maxlinelen, extra)
File "C:\Python27_3\lib\email\quoprimime.py", line 97, in _max_append
L.append(s.lstrip())
AttributeError: 'tuple' object has no attribute 'lstrip'
需要将文件内容附加到电子邮件数据。
例如
with open(fileToSend) as fp:
attachment = MIMEText(fp.read(), _subtype=subtype)
attachment.add_header("Content-Disposition", "attachment",\
filename=os.path.basename(filename))
msg.attach(attachment)
在您的代码中执行以下操作(将 msg.attach(MIMEText(text))
行替换为以下代码):
ctype, encoding = mimetypes.guess_type(filename)
if ctype is None or encoding is not None:
ctype = "application/octet-stream"
maintype, subtype = ctype.split("/", 1)
with open(filename) as fp:
attachment = MIMEText(fp.read(), _subtype=subtype)
attachment.add_header("Content-Disposition", "attachment",\
filename=os.path.basename(filename))
msg.attach(attachment)
注意: filename
应该是我们要在电子邮件中附加的文件的完整路径。例如/var/opt/html/report_email.html
如果我添加多个收件人电子邮件 ID,则它不起作用。
- if
toaddress
是字符串,其中每个电子邮件 ID 用逗号分隔,即,
例如"abc@gmail.com,xyz@gmail.com"
然后喜欢server.sendmail(fromaddress,toaddress.split(','),msg.as_string())