溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

python郵件發(fā)送smtplib怎么寫?

發(fā)布時間:2020-06-23 10:08:21 來源:億速云 閱讀:145 作者:清晨 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細講解有關(guān)python郵件發(fā)送smtplib怎么寫?,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

文件形式的郵件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from email.mime.text import MIMEText 
from email.header import Header 
 
sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtp.163.com' 
username = '***' 
password = '***' 
 
msg = MIMEText('你好','text','utf-8')#中文需參數(shù)‘utf-8',單字節(jié)字符不需要 
msg['Subject'] = Header(subject, 'utf-8') 
 
smtp = smtplib.SMTP() 
smtp.connect('smtp.163.com') 
smtp.login(username, password) 
smtp.sendmail(sender, receiver, msg.as_string()) 
smtp.quit() 

HTML形式的郵件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from email.mime.text import MIMEText 
 
sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtp.163.com' 
username = '***' 
password = '***' 
 
msg = MIMEText('<html><h2>你好</h2></html>','html','utf-8') 
 
msg['Subject'] = subject 
 
smtp = smtplib.SMTP() 
smtp.connect('smtp.163.com') 
smtp.login(username, password) 
smtp.sendmail(sender, receiver, msg.as_string()) 
smtp.quit() 

帶圖片的HTML郵件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from email.mime.multipart import MIMEMultipart 
from email.mime.text import MIMEText 
from email.mime.image import MIMEImage 
 
sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtp.163.com' 
username = '***' 
password = '***' 
 
msgRoot = MIMEMultipart('related') 
msgRoot['Subject'] = 'test message' 
 
msgText = MIMEText('<b>Some <i>HTML</i> text</b> and an image.<br><img src="cid:image1"><br>good!','html','utf-8') 
msgRoot.attach(msgText) 
 
fp = open('h:\\python\\1.jpg', 'rb') 
msgImage = MIMEImage(fp.read()) 
fp.close() 
 
msgImage.add_header('Content-ID', '<image1>') 
msgRoot.attach(msgImage) 
 
smtp = smtplib.SMTP() 
smtp.connect('smtp.163.com') 
smtp.login(username, password) 
smtp.sendmail(sender, receiver, msgRoot.as_string()) 
smtp.quit() 

帶附件的郵件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from email.mime.multipart import MIMEMultipart 
from email.mime.text import MIMEText 
from email.mime.image import MIMEImage 
 
sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtp.163.com' 
username = '***' 
password = '***' 
 
msgRoot = MIMEMultipart('related') 
msgRoot['Subject'] = 'test message' 
 
#構(gòu)造附件 
att = MIMEText(open('h:\\python\\1.jpg', 'rb').read(), 'base64', 'utf-8') 
att["Content-Type"] = 'application/octet-stream' 
att["Content-Disposition"] = 'attachment; filename="1.jpg"' 
msgRoot.attach(att) 
  
smtp = smtplib.SMTP() 
smtp.connect('smtp.163.com') 
smtp.login(username, password) 
smtp.sendmail(sender, receiver, msgRoot.as_string()) 
smtp.quit() 

群發(fā)郵件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from email.mime.text import MIMEText 
 
sender = '***' 
receiver = ['***','****',……] 
subject = 'python email test' 
smtpserver = 'smtp.163.com' 
username = '***' 
password = '***' 
 
msg = MIMEText('你好','text','utf-8') 
 
msg['Subject'] = subject 
 
smtp = smtplib.SMTP() 
smtp.connect('smtp.163.com') 
smtp.login(username, password) 
smtp.sendmail(sender, receiver, msg.as_string()) 
smtp.quit() 

各種元素都包含的郵件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from email.mime.multipart import MIMEMultipart 
from email.mime.text import MIMEText 
from email.mime.image import MIMEImage 
 
sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtp.163.com' 
username = '***' 
password = '***' 
 
# Create message container - the correct MIME type is multipart/alternative. 
msg = MIMEMultipart('alternative') 
msg['Subject'] = "Link" 
 
# Create the body of the message (a plain-text and an HTML version). 
text = "Hi!\nHow are you&#63;\nHere is the link you wanted:\nhttp://www.python.org" 
html = """\ 
<html> 
 <head></head> 
 <body> 
 <p>Hi!<br> 
 How are you&#63;<br> 
 Here is the <a href="http://www.python.org" rel="external nofollow" >link</a> you wanted. 
 </p> 
 </body> 
</html> 
""" 
 
# Record the MIME types of both parts - text/plain and text/html. 
part1 = MIMEText(text, 'plain') 
part2 = MIMEText(html, 'html') 
 
# Attach parts into message container. 
# According to RFC 2046, the last part of a multipart message, in this case 
# the HTML message, is best and preferred. 
msg.attach(part1) 
msg.attach(part2) 
#構(gòu)造附件 
att = MIMEText(open('h:\\python\\1.jpg', 'rb').read(), 'base64', 'utf-8') 
att["Content-Type"] = 'application/octet-stream' 
att["Content-Disposition"] = 'attachment; filename="1.jpg"' 
msg.attach(att) 
 
smtp = smtplib.SMTP() 
smtp.connect('smtp.163.com') 
smtp.login(username, password) 
smtp.sendmail(sender, receiver, msg.as_string()) 
smtp.quit() 

基于SSL的郵件

#!/usr/bin/env python3 
#coding: utf-8 
import smtplib 
from email.mime.text import MIMEText 
from email.header import Header 
sender = '***' 
receiver = '***' 
subject = 'python email test' 
smtpserver = 'smtp.163.com' 
username = '***' 
password = '***' 
 
msg = MIMEText('你好','text','utf-8')#中文需參數(shù)‘utf-8',單字節(jié)字符不需要 
msg['Subject'] = Header(subject, 'utf-8') 
 
smtp = smtplib.SMTP() 
smtp.connect('smtp.163.com') 
smtp.ehlo() 
smtp.starttls() 
smtp.ehlo() 
smtp.set_debuglevel(1) 
smtp.login(username, password) 
smtp.sendmail(sender, receiver, msg.as_string()) 
smtp.quit() 

關(guān)于python郵件發(fā)送smtplib怎么寫?就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI