python下载邮件附件
① 用python发邮件的时候,附件文件名如果是中文,接受到的总是乱码,如何解决谢大神解答!
'attachment; filename="中文.txt"'.decode('utf-8')
在每个python 的中文字符后面加.decode('utf-8')看看?
② 新手求助!python+testlink自动化,如何下载testlink的test case附件
你需要先安装这个东西
你可以从 SourceForge去下载一个稳定的版本,然后pip install TestLink-API-Python-client-0.8.0.zip
然后你需要在linux里面用下面的步骤来验证安装:
set TESTLINK_API_PYTHON_SERVER_URL=http://[YOURSERVER]/testlink/lib/api/xmlrpc/v1/xmlrpc.php
set TESTLINK_API_PYTHON_DEVKEY=[Users devKey generated by TestLink]
python
>>> import testlink
>>> tls = testlink.TestLinkHelper(testlink.TestlinkAPIClient)
>>> tls.about()
' Testlink API Version: 1.0 initially ....'
下面是一个完成的测试过程:
set TESTLINK_API_PYTHON_SERVER_URL=http://[YOURSERVER]/testlink/lib/api/xmlrpc/v1/xmlrpc.php
set TESTLINK_API_PYTHON_DEVKEY=[Users devKey generated by TestLink]
python
>>> import testlink
>>> tls = testlink.TestLinkHelper().connect(testlink.TestlinkAPIClient)
>>> tls.countProjects()
3
>>> tc_info = tls.getTestCase(None, testcaseexternalid='NPROAPI-3')
[{'full_tc_external_id': 'NPROAPI-3', ..., 'id': '5440', 'version': '2',
'testsuite_id': '5415', 'tc_external_id': '3','testcase_id': '5425', ...}]
>>> tls.TCnewTestCase(tc_info[0]['testcase_id'], testsuiteid=newSuiteID,
testcasename='a new test case name')
>>> print tls.whatArgs('createTestPlan')
createTestPlan(<testplanname>, <testprojectname>, [note=<note>], [active=<active>],
[public=<public>], [devKey=<devKey>])
create a test plan
这样的话,你就可以看到对应的testcase用例了
③ 用Python发送邮件,可以群发,带有多个附件
'''''
函数说明:Send_email_text()函数实现发送带有附件的邮件,可以群发,附件格式包括:xlsx,pdf,txt,jpg,mp3等
参数说明:
1.subject:邮件主题
2.content:邮件正文
3.filepath:附件的地址,输入格式为["","",...]
4.receive_email:收件人地址,输入格式为["","",...]
'''
defSend_email_text(subject,content,filepath,receive_email):
importsmtplib
fromemail.mime.multipartimportMIMEMultipart
fromemail.mime.textimportMIMEText
fromemail.mime.
sender="发送方邮箱"
passwd="填入发送方密码"
receivers=receive_email#收件人邮箱
msgRoot=MIMEMultipart()
msgRoot['Subject']=subject
msgRoot['From']=sender
iflen(receivers)>1:
msgRoot['To']=','.join(receivers)#群发邮件
else:
msgRoot['To']=receivers[0]
part=MIMEText(content)
msgRoot.attach(part)
##添加附件部分
forpathinfilepath:
if".jpg"inpath:
#jpg类型附件
jpg_name=path.split("\")[-1]
part=MIMEApplication(open(path,'rb').read())
part.add_header('Content-Disposition','attachment',filename=jpg_name)
msgRoot.attach(part)
if".pdf"inpath:
#pdf类型附件
pdf_name=path.split("\")[-1]
part=MIMEApplication(open(path,'rb').read())
part.add_header('Content-Disposition','attachment',filename=pdf_name)
msgRoot.attach(part)
if".xlsx"inpath:
#xlsx类型附件
xlsx_name=path.split("\")[-1]
part=MIMEApplication(open(path,'rb').read())
part.add_header('Content-Disposition','attachment',filename=xlsx_name)
msgRoot.attach(part)
if".txt"inpath:
#txt类型附件
txt_name=path.split("\")[-1]
part=MIMEApplication(open(path,'rb').read())
part.add_header('Content-Disposition','attachment',filename=txt_name)
msgRoot.attach(part)
if".mp3"inpath:
#mp3类型附件
mp3_name=path.split("\")[-1]
part=MIMEApplication(open(path,'rb').read())
part.add_header('Content-Disposition','attachment',filename=mp3_name)
msgRoot.attach(part)
try:
s=smtplib.SMTP()
s.connect("smtp.mail.aliyun.com")#这里我使用的是阿里云邮箱,也可以使用163邮箱:smtp.163.com
s.login(sender,passwd)
s.sendmail(sender,receivers,msgRoot.as_string())
print("邮件发送成功")
exceptsmtplib.SMTPExceptionase:
print("Error,发送失败")
finally:
s.quit()
④ 如何使用python发送包含正文和附件的邮件
fromemail.HeaderimportHeader
fromemail.MIMETextimportMIMEText
fromemail.
importsmtplib,datetime
#创建一个带附件的实例
msg=MIMEMultipart()
#构造附件
att=MIMEText(open('d:\tc201.rar','rb').read(),'base64','gb2312')
att["Content-Type"]='application/octet-stream'
att["Content-Disposition"]='attachment;filename="tc201.rar"'
msg.attach(att)
#加邮件头
msg['to']='[email protected]'
msg['from']='[email protected]'
msg['subject']=Header('冒烟测试结果('+str(datetime.date.today())+')',
'gb2312')
#发送邮件
server=smtplib.SMTP('smtp.xxx.com')
server.sendmail(msg['from'],msg['to'],
msg.as_string())
server.close
⑤ python 发送带附件的邮件,收到的附件名都变成了tcmime.1774.1903.2076.bin格式,怎么回事呢
img["Content-Disposition"] = 'attachment,filename="hdrCount.txt"'这行改为:
img["Content-Disposition"] = 'attachment,filename=%s' % string.encode("uff-8")
也就是说对你发送的内容需要进行uft-8编码。
⑥ Python向多人发送、抄送带附件的邮件(含详细代码)
python要发送带附件的邮件,首先要创建MIMEMultipart()实例,然后构造附件,如果有多个附件,可依次构造,最后使用smtplib.smtp发送。
步骤:
(1)设置服务器所需信息(ps:部门邮箱密码为授权码,需自行登录相应邮箱设置授权码)
(2)设置email信息
(3)附件部分
(4)登录邮箱并发送邮件
附上源码: