-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ivan Yang
authored
Aug 17, 2022
1 parent
40a3089
commit 93eb105
Showing
5 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
行銷人轉職爬蟲王實戰|5大社群+2大電商/10-3.Python寄信基礎-寄送檔案教學/10-3.Python寄信基礎-寄送檔案教學.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Created on Thu Jun 11 18:18:31 2020 | ||
@author: Ivan | ||
課程教材:行銷人轉職爬蟲王實戰|5大社群平台+2大電商 | ||
版權屬於「楊超霆」所有,若有疑問,可聯絡[email protected] | ||
第十章 贈品:Gmail自動寄信工具 | ||
Python寄信基礎-寄送檔案教學 | ||
""" | ||
from email.mime.multipart import MIMEMultipart | ||
from email.mime.text import MIMEText | ||
import smtplib | ||
from email.mime.image import MIMEImage | ||
from pathlib import Path | ||
from email.mime.application import MIMEApplication | ||
|
||
|
||
sendFrom = "寄件者信箱" | ||
senderPassword = "寄件者密碼" | ||
content = MIMEMultipart() #建立MIMEMultipart物件 | ||
content["subject"] = "輸入您想要的郵件標題" #郵件標題 | ||
content["from"] = sendFrom #寄件者 | ||
content["to"] = "收件者信箱" #收件者 | ||
|
||
|
||
content.attach(MIMEText("Ivan的測試寄信,寄信處女作品~~")) #郵件內容 | ||
content.attach(MIMEImage(Path("夕陽.jpg").read_bytes())) # 郵件圖片內容 | ||
|
||
#寄送PDF檔案 | ||
fileName = 'test.pdf' | ||
pdfload = MIMEApplication(open(fileName,'rb').read()) | ||
pdfload.add_header('Content-Disposition', | ||
'attachment', | ||
filename=fileName) | ||
content.attach(pdfload) | ||
|
||
#寄送Word檔案 | ||
fileName = 'test.docx' | ||
pdfload = MIMEApplication(open(fileName,'rb').read()) | ||
pdfload.add_header('Content-Disposition', | ||
'attachment', | ||
filename=fileName) | ||
content.attach(pdfload) | ||
|
||
#寄送csv檔案 | ||
fileName = '顧客訂單.csv' | ||
pdfload = MIMEApplication(open(fileName,'rb').read()) | ||
pdfload.add_header('Content-Disposition', # 內容配置 | ||
'attachment', # 附件 | ||
filename=fileName) | ||
content.attach(pdfload) | ||
|
||
with smtplib.SMTP(host="smtp.gmail.com", port="587") as smtp: # 設定SMTP伺服器 | ||
try: | ||
smtp.ehlo() # 驗證SMTP伺服器 | ||
smtp.starttls() # 建立加密傳輸 | ||
smtp.login( sendFrom, senderPassword) # 登入寄件者gmail | ||
smtp.send_message(content) # 寄送郵件 | ||
print("成功傳送") | ||
except Exception as e: | ||
print("Error message: ", e) |
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
姓名,性別,電子郵件,購買商品,數量,購買總金額 | ||
王曉明,男,[email protected],奶粉,2,990 | ||
范堅強,男,[email protected],鉛筆,10,40 | ||
莊孝偉,男,[email protected],波羅麵包,3,72 | ||
李撓施,女,[email protected],筆記本,2,100 | ||
賈慶紀,男,[email protected],慶紀,100,3200 | ||
石輝演,男,[email protected],燒杯,3,100 |