附加文件像对象到电子邮件蟒蛇3 [英] attach file like object to email python 3
问题描述
我已经找到了很多关于如何将本地文件附加到电子邮件的例子。我想要做的是将一个像对象的文件附加到电子邮件。你为什么问?所以我不必处理清理文件。下面是我的代码和我的错误。经过大量的搜索,我仍然没有设法得到它的工作,任何帮助将不胜感激:)
pre $ def email_sup_teams(team_name ,contact_list,file_attachemnt):
$ p
message_list = []
for file_attachemnt中的作业:
for k,v in jobs.items():
message_list.append(v +', ')
attachment_text =\\\
.join(message_list)
print(type(attachment_text))
msg = MIMEText('失败的工作列表,请参阅附件' )
msg ['Subject'] ='未运行'+ team_name
msg ['From'] ='a@b.com'
msg ['To'] ='' c@d.com'
$ bf = io.StringIO(attachment_text)
attachment = MIMEText(f.read())
attachment.add_header('Content-Disposition','附件',文件名='test_attach')
msg.attach(附件)
s = smtplib.SMTP('smlsmtp')
s.sendmail(msg ['''] ,msg ['To'] ,msg.as_string())
s.quit()
print('\\\
'+ team_name +'Email Sent')
错误:
< class'str'>
Traceback(最近一次调用最后一次):
在< module>文件中,第85行的queue_cleaner_main.py
sys.exit(main())
在
queue_cleaner_functions.email_sup_teams(t,team_members_emails,attachment_file_of_jobs)
文件中的queue_cleaner_main.py \ popssup \ old_job\queue_cleaner_functions.py,第179行,在email_sup_teams
msg.attach(附件)
文件C:\Python34\lib\email\mime\ nonmultipart.py,第22行,附加
'不能附加到非多部分/ *'的子部分)
email.errors.MultipartConversionError:无法将附加的子部分附加到非多部分/ *
原来我应该读
https://docs.python.org/3/ library / email-examples.html
更紧密。我敢肯定,因为我只使用1个MIME类型对象来建立我的电子邮件,但试图添加多个MIME对象。基本上得到它的工作,我用下面的代码。快乐的日子!
$ pre $ def $ email $ s $作业在file_attachemnt中:
for k,v in jobs.items():
message_list.append(v +',')
attachment_text =\\\
.join(message_list)
print(type(attachment_text))
#创建容器(外部)电子邮件。
msg = MIMEMultipart()
#msg = MIMEText('失败的工作列表,请参阅附件')
msg ['Subject'] ='未运行'+ team_name
msg ['From'] ='a@b.com'
msg ['To'] ='c@d.com'
msg.preamble ='失败的作业列表。请参阅附件'
f = io.StringIO(attachment_text)
attachment = MIMEText(f.getvalue())
attachment.add_header('Content-Disposition','attachment',filename =' ('''','msg'','''')
msg.attach(附件)
$ bs = smtplib.SMTP('smlsmtp')
s.sendmail '',msg.as_string())
s.quit()
print('\\\
'+ team_name +'Email Sent')
I have found a lot of examples on the net of how to attach local files to an email. What I want to do is attach a file like object to an email. Why you ask? so I don't have to deal with cleaning up files. Below is my code and my error. After much googling I still have not managed to get it to work, any help would be greatly appreciated :)
def email_sup_teams(team_name, contact_list, file_attachemnt):
message_list = []
for jobs in file_attachemnt:
for k, v in jobs.items():
message_list.append(v + ',')
attachment_text = "\n".join(message_list)
print(type(attachment_text))
msg = MIMEText(' Failed jobs list. Please see attachment')
msg['Subject'] = 'Not run Jobs for ' + team_name
msg['From'] = 'a@b.com'
msg['To'] = 'c@d.com'
f = io.StringIO(attachment_text)
attachment = MIMEText(f.read())
attachment.add_header('Content-Disposition', 'attachment', filename='test_attach')
msg.attach(attachment)
s = smtplib.SMTP('smlsmtp')
s.sendmail(msg['From'], msg['To'], msg.as_string())
s.quit()
print('\n' + team_name + ' Email Sent')
error:
<class 'str'>
Traceback (most recent call last):
File "queue_cleaner_main.py", line 85, in <module>
sys.exit(main())
File "queue_cleaner_main.py", line 82, in main
queue_cleaner_functions.email_sup_teams(t, team_members_emails, attachment_file_of_jobs)
File "D:\oppssup\old_job\queue_cleaner_functions.py", line 179, in email_sup_teams
msg.attach(attachment)
File "C:\Python34\lib\email\mime\nonmultipart.py", line 22, in attach
'Cannot attach additional subparts to non-multipart/*')
email.errors.MultipartConversionError: Cannot attach additional subparts to non-multipart/*
Turns out I should have read
https://docs.python.org/3/library/email-examples.html
more closely. Im pretty sure its because I was using only 1 MIME type object to build my email but trying to add multiple MIME objects. Basically to get it to work I used the below code. Happy days!
def email_sup_teams(team_name, contact_list, file_attachemnt):
message_list = []
for jobs in file_attachemnt:
for k, v in jobs.items():
message_list.append(v + ',')
attachment_text = "\n".join(message_list)
print(type(attachment_text))
# Create the container (outer) email message.
msg = MIMEMultipart()
#msg = MIMEText(' Failed jobs list. Please see attachment')
msg['Subject'] = 'Not run Jobs for ' + team_name
msg['From'] = 'a@b.com'
msg['To'] = 'c@d.com'
msg.preamble = 'Failed jobs list. Please see attachment'
f = io.StringIO(attachment_text)
attachment = MIMEText(f.getvalue())
attachment.add_header('Content-Disposition', 'attachment', filename='jobs_not_run.xls')
msg.attach(attachment)
s = smtplib.SMTP('smlsmtp')
s.sendmail(msg['From'], msg['To'], msg.as_string())
s.quit()
print('\n' + team_name + ' Email Sent')
这篇关于附加文件像对象到电子邮件蟒蛇3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!