PythonでGMAILメールを送信する(更新)
練習內容
這一篇是上一篇寫的加強版
1.在內容內加入需要更新員工的姓名
2.在CC內加入需要更新員工的SVmail
3.定期執行腳本
由於每個員工的SV和姓名都不一樣,所以做迴圈一個一個送
import mysql.connectorimport smtplib
from email.mime.text import MIMEText
connection = mysql.connector.connect
(host=”***”, port=”*****”, user=”***”, password=****”, database=”***” )
YEAR_1MON = 334
Limit_day= 365- YEAR_1MON
cusor =connection.cursor()
cusor.execute(“SELECT * FROM vendor WHERE to_days(now())- to_days(renew_date) =334”)
records =cusor.fetchall()
to_addr_list = []
to_addr_name = []
to_addr_svlist = []
for r in records: user = [r]
name =user[0][1]
mail =user[0][4]
svmail =user[0[5]
to_addr_name.append(name)
to_addr_list.append(mail)
to_addr_svlist.append(svmail)
for i in range(len(to_addr_list)):
from_addr = “****@gmail.com”
pwd = “*****”
msg = MIMEText(f”{to_addr_name[i]} のカードを{Limit_day}日内に更新してください。”,”plain”,”utf-8″r_name[i]} のカードを更新してください。”,”plain”,”utf-8″)
msg[“subject”] = “セキュリティカード”
msg[“From”] =”From:更新通知”
msg[“TO”] = f”{to_addr_list[i]}”
msg[“CC”] = f”{to_addr_svlist[i]}”
mySMPT = smtplib.SMTP(“smtp.gmail.com”,587)
mySMPT.ehlo()
mySMPT.starttls()
mySMPT.login(from_addr,pwd)
status =mySMPT.sendmail(from_addr,to_addr_list[i], msg.as_string())
if status == {}:
print(f”{to_addr_name[i]}へ送信成功”)
mySMPT.quit()
DB設定
CODE
翌日の実施