1. 程式人生 > >python 傳送郵件基礎

python 傳送郵件基礎

python 傳送郵件基礎程式碼

import datetime
import email
import shutil
import os
from email.mime.application import MIMEApplication
import smtplib as sm
import email.mime.multipart
import email.mime.text

current_date = datetime.date.today()- datetime.timedelta(days=1)
msg=email.mime.multipart.MIMEMultipart()
msg[‘to’]=’接受郵箱’
msg[‘Cc’]=’抄送郵箱’
msg[‘from’]=’傳送郵箱’
msg[‘subject’]=’附件名’
content=’主題’+str(current_date)
content=email.mime.text.MIMEText(content)
msg.attach(content)

txt=MIMEApplication(open(‘檔案路徑’,’rb’).read())
txt[“Content-Type”] = ‘application/octet-stream’
txt.add_header(‘Content-Disposition’, ‘attachment’, filename=”檔名稱”)
msg.attach(txt)

smtpObj = sm.SMTP_SSL(‘smtp.exmail.qq.com’, 465)
type(smtpObj)
smtpObj.ehlo()
smtpObj.login(‘傳送郵箱’,’傳送郵箱密碼’)
smtpObj.sendmail(msg[‘from’],msg[‘Cc’].split(‘,’)+msg[‘to’].split(‘,’),msg.as_string())
smtpObj.quit()

shutil.rmtree(‘檔案路徑’) ————刪除資料夾
os.mkdir(‘檔案路徑’) ————建立資料夾