python中傳送郵件(普通文字檔案、附件、圖片等)
阿新 • • 發佈:2018-12-13
1、傳送普通文字檔案
#coding=utf-8 import smtplib from email.mime.text import MIMEText from email.header import Header host = 'smtp.126.com' # 設定發件伺服器地址 port = 25 # 設定發件伺服器埠號。注意,這裡有SSL和非SSL兩種形式 #傳送郵箱 sender = '[email protected]' #接收郵箱 receiver = '[email protected]' #傳送郵件主題 subject = 'Python email test' #傳送郵箱伺服器 smtpserver = 'smtp.126.com' username = '
[email protected]' #傳送郵箱使用者 password = '**************' #郵箱密碼或授權碼 #編寫 text 型別的郵件正文 #msg = MIMEText('<html><h1>比你更忙的人都在學習!</h1></html>','html','utf-8') msg = MIMEText('比你更忙的人都在學習!111','plain','utf-8') msg['Subject'] = Header(subject, 'utf-8') msg['From'] = sender msg['To'] = receiver smtp = smtplib.SMTP() smtp.connect('smtp.126.com',25) smtp.login(username, password) # 登陸郵箱 smtp.sendmail(msg['From'], msg['To'], msg.as_string()) # 傳送郵件! print("郵件傳送成功!")
2、傳送郵件給多個人
#coding=utf-8
import smtplib
from email.mime.text import MIMEText
from email.header import Header
host = 'smtp.126.com' # 設定發件伺服器地址
port = 25 # 設定發件伺服器埠號。注意,這裡有SSL和非SSL兩種形式
#傳送郵箱
sender = '[email protected]'
#接收郵箱
receiver = ['[email protected]','[email protected]','[email protected] ']
#傳送郵件主題
subject = 'Python email test'
#傳送郵箱伺服器
smtpserver = 'smtp.126.com'
username = '[email protected]' #傳送郵箱使用者
password = '**************' #郵箱密碼或授權碼
#編寫 text 型別的郵件正文
msg = MIMEText('<html><h1>比你更忙的人都在學習!</h1></html>','html','utf-8')
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = '[email protected]'
msg['To'] =','.join(receiver)
smtp = smtplib.SMTP()
smtp.connect('smtp.126.com',25)
smtp.login(username, password) # 登陸郵箱
smtp.sendmail(msg['From'], msg['To'].split(','), msg.as_string()) # 傳送郵件!
print("郵件傳送成功!")
3、傳送附件
#coding=utf-8
from email.mime.text import MIMEText#專門傳送正文
from email.mime.multipart import MIMEMultipart#傳送多個部分
from email.mime.application import MIMEApplication#傳送附件
import smtplib#傳送郵件
file = 'C:\\Users\\wm\\Desktop\\101.txt' #附件路徑
file1 = 'C:\\Users\\wm\\Desktop\\102.txt' #附件路徑
send_user = '[email protected]' #發件人
password = '*************' #授權碼/密碼
receive_users = '[email protected]' #收件人,可為list
subject = 'Python email test' #郵件主題
email_text = '這是菜鳥教程Python 郵件傳送測試……' #郵件正文
server_address = 'smtp.126.com' #伺服器地址
mail_type = '1' #郵件型別
#構造一個郵件體:正文 附件
msg = MIMEMultipart()
msg['Subject']=subject #主題
msg['From']=send_user #發件人
msg['To']=receive_users #收件人
#構建正文
part_text=MIMEText(email_text)
msg.attach(part_text) #把正文加到郵件體裡面去
#構建郵件附件
#file = file #獲取檔案路徑
part_attach1 = MIMEApplication(open(file,'rb').read()) #開啟附件
part_attach1.add_header('Content-Disposition','attachment',filename=file) #為附件命名
msg.attach(part_attach1) #新增附件
# 傳送郵件 SMTP
smtp= smtplib.SMTP(server_address,25) # 連線伺服器,SMTP_SSL是安全傳輸
smtp.login(send_user, password)
smtp.sendmail(send_user, receive_users, msg.as_string()) # 傳送郵件
print('郵件傳送成功!')
4、傳送圖片
#coding=utf-8
from email.mime.text import MIMEText#專門傳送正文
from email.mime.multipart import MIMEMultipart#傳送多個部分
from email.mime.application import MIMEApplication#傳送附件
from email.mime.base import MIMEBase
import smtplib#傳送郵件
from email import encoders
file = 'C:\\Users\\wm\\Desktop\\101.txt' #附件路徑
file1 = 'C:\\Users\\wm\\Desktop\\102.txt' #附件路徑
send_user = '[email protected]' #發件人
password = '***************' #授權碼/密碼
receive_users = '[email protected]' #收件人,可為list
subject = 'Python email test' #郵件主題
email_text = '這是菜鳥教程Python 郵件傳送測試……' #郵件正文
server_address = 'smtp.126.com' #伺服器地址
mail_type = '1' #郵件型別
#構造一個郵件體:正文 附件
msg = MIMEMultipart()
msg['Subject']=subject #主題
msg['From']=send_user #發件人
msg['To']=receive_users #收件人
#構建正文
part_text=MIMEText(email_text)
msg.attach(part_text) #把正文加到郵件體裡面去
#構建郵件附件
#file = file #獲取檔案路徑
part_attach1 = MIMEApplication(open(file,'rb').read()) #開啟附件
part_attach1.add_header('Content-Disposition','attachment',filename=file) #為附件命名
msg.attach(part_attach1) #新增附件
with open('C:\\Users\\wm\\Desktop\\tp.png', 'rb') as f:
# 設定附件的MIME和檔名,這裡是png型別:
mime = MIMEBase('image', 'png', filename='tp.png')
# 加上必要的頭資訊:
mime.add_header('Content-Disposition', 'attachment', filename='tp.png')
mime.add_header('Content-ID', '<0>')
mime.add_header('X-Attachment-Id', '0')
# 把附件的內容讀進來:
mime.set_payload(f.read())
# 用Base64編碼:
encoders.encode_base64(mime)
# 新增到MIMEMultipart:
msg.attach(mime)
#正文顯示附件圖片
msg.attach(MIMEText('<html><body><h1>Hello</h1>' +
'<p><img src="cid:0"></p>' +
'</body></html>', 'html', 'utf-8'))
# 傳送郵件 SMTP
smtp= smtplib.SMTP(server_address, 25) # 連線伺服器,SMTP_SSL是安全傳輸
smtp.login(send_user, password)
smtp.sendmail(send_user, receive_users, msg.as_string()) # 傳送郵件
print('郵件傳送成功!')