python郵件正文
1. 用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()
2. python,使用smtp發送郵件,求實例
from smtplib import SMTP
from RuckusAutoTest.models import TestCase
from email.MIMEMultipart import MIMEMultipart
from email.mime.application import MIMEApplication
def sendFildByMail(config):
print 'Preparing...',
message = MIMEMultipart( )
message['from'] = config['from']
message['to'] = config['to']
message['Reply-To'] = config['from']
message['Subject'] = config['subject']
message['Date'] = time.ctime(time.time())
message['X-Priority'] = '3'
message['X-MSMail-Priority'] = 'Normal'
message['X-Mailer'] = 'Microsoft Outlook Express 6.00.2900.2180'
message['X-MimeOLE'] = 'Proced By Microsoft MimeOLE V6.00.2900.2180'
f=open(config['file'], 'rb')
file = MIMEApplication(f.read())
f.close()
file.add_header('Content-Disposition', 'attachment', filename= os.path.basename(config['file']))
message.attach(file)
print 'OK'
print 'Logging...',
smtp = SMTP(config['server'])#, config['port'])
smtp.ehlo()
smtp.starttls()
smtp.ehlo()
smtp.login(config['username'], config['password'])
print 'OK'
print 'Sending...',
smtp.sendmail (config['from'], [config['from'], config['to']], message.as_string())
print 'OK'
smtp.close()
time.sleep(1)
sendFildByMail({
'from': '[email protected]',
'to': '[email protected]',
'subject': ''This is an email test!',
'server': '123.125.50.132',
'username': 'username',
'password': 'password'})
可以注冊一個163郵箱試一下,是可以發郵件的。
在sendFildByMail這個函數里填上正確的參數,from是從哪個郵箱發送,也就是剛注冊的163郵箱,to是發送到哪個郵箱,可以填另一個郵箱來檢查是否能接收郵件,server不要改,這是163的地址;username和password那裡填寫163郵箱的用戶名和密碼。
希望能幫到你,有疑問請追問!
3. demo鍦╬ython涓鐨勭敤娉
demo鏄鎸囨紨紺虹▼搴忥紝閫氬父鐢ㄦ潵婕旂ず涓縐嶆妧鏈鎴栧伐鍏風殑浣跨敤鏂規硶銆傚湪Python涓錛宒emo涔熸槸涓涓闈炲父閲嶈佺殑姒傚康銆備笅闈㈡垜灝嗕負澶у朵粙緇峆ython涓璬emo鐨勭敤娉曘
3銆乨emo紼嬪簭鐨勫垎鏋
浠ヤ笂紼嬪簭浣滀負Python涓婕旂ず閭浠跺彂閫佸姛鑳界殑涓涓猟emo紼嬪簭錛屾兜鐩栦簡閭浠跺彂閫佺殑鍏ㄨ繃紼嬶紝鍏朵腑錛岄氳繃email妯″潡鍒涘緩浜嗛偖浠跺疄渚嬶紝騫墮氳繃smtplib妯″潡鎻愪緵鐨凷MTP鍗忚榪涜屼簡閭浠剁殑鍙戦併
4銆乨emo紼嬪簭鐨勭敤閫
榪欎釜demo紼嬪簭鍙浠ュ府鍔╂柊鎵嬩簡瑙Python涓鐨勯偖浠跺彂閫佸姛鑳斤紝鏇村ソ鍦扮悊瑙e拰瀛︿範Python涓鐨勯偖浠舵ā鍧椼傚傛灉鎯寵佽嚜宸變嬌鐢≒ython鏉ュ彂閫侀偖浠訛紝鍙浠ラ氳繃淇鏀硅demo涓鐨勫彂浠朵漢銆佹敹浠朵漢銆佷富棰樸佹f枃銆丼MTP鏈嶅姟鍣ㄥ湴鍧銆佺鍙d互鍙婅處鎴蜂俊鎮絳夊弬鏁幫紝鏉ュ疄鐜拌嚜宸辯殑閭浠跺彂閫佸姛鑳姐
4. python 代碼中 ret=True 代表什麼意思
這里的ret是用於判斷郵件發送成功與否的
先賦值ret為True,如果發送郵件過程中沒有遇到任何的故障,則try裡面的代碼執行完成後,mail()函數則會返回ret的值,此時ret的值即為True;
若發送過程出現差錯,則會跳到except的語句中,此時先前被賦予True值的ret就會被賦上新的布爾值False,因此main()函數此時會返回False的結果。
接下來就是if ... else ...的語句對ret的值做判斷了,如果ret為True,則列印「郵件發送成功」;否則列印「郵件發送失敗」
5. python,如何發送帶附件的郵件
可以通過以下代碼實現:
importsmtplib
fromemail.mime.textimportMIMEText
importemail.mime.multipart
fromemail.
fromemail.MIMEBaseimportMIMEBase
fromemailimportEncoders
defsend_mail():
mailto_list=['xxxx','xxx']#收件人
mail_host="xxxxx"#設置伺服器
mail_user="xxxx"#用戶名
mail_pass="xxxxxx"#口令
mail_postfix="xxxx.xxx"#發件箱的後綴
me="hello"+"<"+mail_user+"@"+mail_postfix+">"#這里的hello可以任意設置,收到信後,將按照設置顯示
content='Thisistestmail!'#郵件正文
msg=MIMEMultipart()
body=MIMEText(content,_subtype='html',_charset='gb2312')#創建一個實例,這里設置為html格式郵件
msg.attach(body)
msg['Subject']="SubjectTest"#設置主題
msg['From']=me
msg['To']=";".join(mailto_list)
#附件內容,若有多個附件,就添加多個part,如part1,part2,part3
part=MIMEBase('application','octet-stream')
#讀入文件內容並格式化,此處文件為當前目錄下,也可指定目錄例如:open(r'/tmp/123.txt','rb')
part.set_payload(open('test.txt','rb').read())
Encoders.encode_base64(part)
##設置附件頭
part.add_header('Content-Disposition','attachment;filename="test.txt"')
msg.attach(part)
try:
s=smtplib.SMTP()
s.connect(mail_host)#連接smtp伺服器
s.login(mail_user,mail_pass)#登陸伺服器
s.sendmail(me,mailto_list,msg.as_string())#發送郵件
s.close()
print'sendmailsucess'
returnTrue
exceptException,e:
printstr(e)
returnFalse
6. python中如何在郵件正文嵌入變數
html ='''你要發送的內容'''+var1+'''其他部分的內容'''+var2+'''剩下的內容'''
msg.attach(MIMEText(html, 'html'))