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)登錄郵箱並發送郵件
附上源碼: