1. Html網頁結構介紹-網頁到底如何傳送資料?爬蟲必學
2. 資料傳遞:Get與Post差異,網路封包傳送的差異
3. Html爬蟲Get教學-抓下Yahoo股票資訊,程式交易的第一步
4. Html爬蟲Get實戰-全台最大美食平台FoodPanda爬蟲,把熊貓抓回家
5. 資料分析實戰,熊貓FoodPanda熱門美食系列|看出地區最火料理種類
6. Json爬蟲教學-Google趨勢搜尋|掌握最火關鍵字
7. Json爬蟲實戰-24小時電商PChome爬蟲|雖然我不是個數學家但這聽起來很不錯吧
8. Html爬蟲Post教學-台灣股市資訊|網韭菜們的救星
9. Html爬蟲Post實戰-全球美食平台UberEat爬蟲
10. Pandas爬蟲教學-Yahoo股市爬蟲|不想再盯盤
11. Pandas爬蟲實戰-爬下全台各地區氣象預報歷史資料
12. 資料分析實戰-天氣預報圖像化|一張圖巧妙躲過雨季
åé¢çåºç¤è¦å çåï¼
- Gmailè¨å®èªåå¯ä¿¡å°æ¡ï½å¾æ¤ä¸ç¨åæ ¢æ ¢ç¼ä¿¡äºãæææå½±çæå¸ã
- Pythonå¯Gmailåºç¤ï¼æå樣å¼æå¸ï½æåç²é«ãé¡è²ééå¯ä»¥æ¹ãéPythonç¨å¼ç¢¼ã
- Pythonå¯ä¿¡åºç¤ï¼å¯éæªæ¡æå¸ï½ç¨ç¨å¼éæªWordãPDFãCSVãåçé½å¯ä»¥ãéç¨å¼ç¢¼ã
å¨å¸ç¿å®åé¢ç課ç¨å¾ï¼åºæ¬ä¸å¨Gmailå¯ä»¶ææ使ç¨å°çåè½ï¼æåé½æç¨Python幫æå解決äºï¼å æ¤æ¥ä¸ä¾æå便ä¾æ¨¡æ¬è¡é·äººå¸¸å¸¸ç¢°å°çåé¡ï¼å¦ä½å®¢è£½åæ¯å°ä¿¡ä»¶ï¼ä¸¦ä¸éå°èªååå¯ä»¶çç®çã
é裡æåå¿
é 使ç¨email.mime
å¥ä»¶ï¼ä¸¦ä¸éè¦å©ç¨smtplib
å¥ä»¶é²è¡SMTPåå®çå¯éï¼å æ¤éè¦å
å°ææå¥ä»¶è¼å
¥ã
import pandas as pd
import smtplib
from pathlib import Path
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
é次æååè¨çæ å¢ï¼æ³è¦è®Python ä¾ç §å ¬å¸ç顧客è¨å®ï¼èªå幫æåç¼é信件給æ¯ä½é¡§å®¢ï¼å æ¤æåéè¦å è¼å ¥é¡§å®¢è¨å®è³æã
custemor = pd.read_csv('顧客è¨å®.csv')
sendFrom = "å¯ä»¶è
ä¿¡ç®±"
senderPassword = "å¯ä»¶è
å¯ç¢¼"
ç¸ä¿¡æ¨å¨çå®ãPythonå¯ä¿¡åºç¤ï¼å¯éæªæ¡æå¸ï½ç¨ç¨å¼éæªWordãPDFãCSVãåçé½å¯ä»¥ãéç¨å¼ç¢¼ãã課ç¨å¾ä¸å®ææä¸åæ³æ³ï¼å¥½å大é¨åçé件ç¨å¼ç¢¼é½æ¯ä¸æ¨£çï¼å æ¤é裡ç´æ¥å°å ¶å¯«æä¸ådef æ¹æ³å³å¯ï¼åªéè¦çµ¦äºä¿¡ä»¶çç©ä»¶èæ³å¯éçæªæ¡å稱å³å¯ã
#å¯éæªæ¡å°ç¨
def sendFile(fileName, content):
pdfload = MIMEApplication(open(fileName,'rb').read())
pdfload.add_header('Content-Disposition',
'attachment',
filename=fileName)
content.attach(pdfload)
return content
é裡æ¯ä½¿ç¨ã顧客è¨å®.csvãæªæ¡å §çååè³æï¼å æ¤éè¦ä½¿ç¨for è¿´åä¸åå顧客ç¼é信件ã
#éå§æ¯ä¸çè³æé²å»å®¢è£½åçå¯é信件
for coste in range(len(custemor)):
é¦å éè¦å©ç¨æ§å¥çæ¬ä½ï¼ä¾ç¢ºèªçç信件ç稱è¬æ¯å çéæ¯å°å§ï¼ä¸¦ä¸åå¨gender è®æ¸ç¶ä¸ã
message=''
#å
確èªæ§å¥ï¼æ±ºå®çµ¦äºä»éº¼ç¨±è¬
if custemor.iloc[coste]['æ§å¥'] == 'ç·':
gender='å
ç'
else:
gender='å°å§'
å°ææç客製åè³æé½æ¾å ¥message è®æ¸ç¶ä¸ã
# æ´çå°è¦å³éåºå»çæå
message += '親æç {} {} æ¨å¥½ï¼\n\né常æè¬æ¨å¨æ¬åºè³¼è²·ã{}ã{}åï¼å
± {}å
ã\n\nIvanè¡ä»½æéå
¬å¸\nMediemJæç« ï¼https://medium.com/@ivanyang0606'.format(
custemor.iloc[coste]['å§å'],
gender,
custemor.iloc[coste]['購買åå'],
custemor.iloc[coste]['æ¸é'],
custemor.iloc[coste]['購買總éé¡'],
)
è¨å®ä¿¡ä»¶çæ¶ä»¶è ã
content = MIMEMultipart() #建ç«MIMEMultipartç©ä»¶
content["subject"] = "ãè¡é·è¾¦é²å¤§ç¨å¼ãæè¬æ¨çå
顧" #éµä»¶æ¨é¡
content["from"] = sendFrom
content["to"] = custemor.iloc[coste]['é»åéµä»¶'] #æ¶ä»¶è
content.attach(MIMEText(message)) #éµä»¶å
§å®¹
æ¤ç¯ä¾ä¸å¯éåçãWordæªæ¡ãPDFæªæ¡ï¼ä¸¦ä¸å¼å«åå建ç«çsendFile() æ¹æ³ï¼èæ¤åç¾è¥è¦å¯éå¤åæªæ¡çæåï¼Python 該è¦å¦ä½å¯«ã
#以ä¸çºæªæ¡é件ï¼è¥è¦å®¢è£½åå¯éä¸åçé件ï¼å¯ä»¥ç¨ifä¾éæ
content.attach(MIMEImage(Path("å¤é½.jpg").read_bytes())) # éµä»¶åçå
§å®¹
for file in ['test.pdf','test.docx','顧客è¨å®.csv']: #ææ³å¯éç黨åç´æ¥æ¾å¨é£åï¼è®ç¨å¼èªåå»æå
content = sendFile(file, content)
å¨ä¿¡ä»¶å §å®¹ç¢ºå®å¾ï¼å·è¡ä»¥ä¸ç¨å¼ç¢¼ï¼å°±æè¨é信件äºã
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)
作者:楊超霆 行銷搬進大程式 創辦人