Python 傳送帶附件的郵件

2021-08-01 23:25:32 字數 1308 閱讀 8714

#!/usr/bin/python3

import smtplib

from email.mime.text import mimetext

from email.mime.multipart import mimemultipart

from email.header import header

sender = '[email protected]'

receivers = ['[email protected]'] # 接收郵件,可設定為你的qq郵箱或者其他郵箱

#建立乙個帶附件的例項

message = mimemultipart()

message['from'] = header("菜鳥教程", 'utf-8')

message['to'] = header("測試", 'utf-8')

subject = 'python smtp 郵件測試'

message['subject'] = header(subject, 'utf-8')

#郵件正文內容

message.attach(mimetext('這是菜鳥教程python 郵件傳送測試……', 'plain', 'utf-8'))

# 構造附件1,傳送當前目錄下的 test.txt 檔案

att1 = mimetext(open('test.txt', 'rb').read(), 'base64', 'utf-8')

# 這裡的filename可以任意寫,寫什麼名字,郵件中顯示什麼名字

att1["content-disposition"] = 'attachment; filename="test.txt"'

message.attach(att1)

# 構造附件2,傳送當前目錄下的 runoob.txt 檔案

att2 = mimetext(open('runoob.txt', 'rb').read(), 'base64', 'utf-8')

att2["content-disposition"] = 'attachment; filename="runoob.txt"'

message.attach(att2)

try:

smtpobj = smtplib.smtp('localhost')

smtpobj.sendmail(sender, receivers, message.as_string())

print ("郵件傳送成功")

except smtplib.smtpexception:

print ("error: 無法傳送郵件")

python傳送帶附件郵件

1.不包括附件的郵件 coding utf 8 import smtplib import string 傳送普通的文字郵件 郵件smtp的位址 host smtp.163.com 定義郵件的標題 subject 這是郵件標題 發件人 from 163.com 收件人 to qq.com 傳送的郵件...

python傳送帶附件的郵件

coding utf8 import os,smtplib from email.mime.multipart import mimemultipart from email.mime.text import mimetext from email.header import header clas...

python 傳送郵件帶附件 封裝

usr bin python coding utf 8 import smtplib smtplib這個模組是管發郵件 from email.mime.text import mimetext 構造郵件內容 from email.mime.multipart import mimemultipart...