在現代工作環境中,我們經常需要向多個收件人傳送個性化的郵件。通過使用Python程式語言,我們可以自動化這個過程,從Excel檔案中讀取收件人和相關資料,並行送客製化的郵件。
import pandas as pd import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.application import MIMEApplication import os from email.header import Header
sender_email = '[email protected]' sender_password = 'your_password'
smtp_server = 'smtp.example.com' smtp_port = 587
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls() # 開啟TLS加密
server.login(sender_email, sender_password)
df = pd.read_excel('path_to_excel_file.xlsx')
employee_names = df['員工姓名'].unique()
employee_emails = { '張三': '[email protected]', '李四': '[email protected]', '王五': '[email protected]', # 新增更多員工和郵箱資訊 }
for employee_name in employee_names: employee_data = df[df['員工姓名'] == employee_name] # 建立員工的資料 # 生成員工的 Excel 檔案 employee_data_filename = f'{employee_name}.xlsx' employee_data.to_excel(employee_data_filename, index=False) # 建立郵件 msg = MIMEMultipart() msg['From'] = sender_email msg['To'] = employee_emails.get(employee_name, '') # 根據員工姓名獲取郵箱 msg['Subject'] = '拆分資料通知' body = f"尊敬的{employee_name},您的拆分資料已經準備好,請查收附件。" msg.attach(MIMEText(body, 'plain')) # 新增附件 with open(employee_data_filename, 'rb') as file: part = MIMEApplication(file.read(), Name=os.path.basename(employee_data_filename)) part.add_header('Content-Disposition', 'attachment', filename=Header(os.path.basename(employee_data_filename), 'utf-8').encode()) msg.attach(part) # 傳送郵件 server.sendmail(sender_email, employee_emails.get(employee_name, ''), msg.as_string()) # 刪除生成的員工資料檔案 os.remove(employee_data_filename) # 退出SMTP連線 server.quit()
最後,關閉與SMTP伺服器的連線。
通過上述Python指令碼,我們可以批次傳送個性化的郵件。我們首先設定發件人郵箱和密碼,然後指定SMTP伺服器和埠號。接下來,我們讀取包含員工資訊的Excel檔案,並獲取唯一的員工姓名列表和對應的郵箱地址。然後,我們遍歷員工資料,併為每個員工建立郵件,附帶相應的附件。最後,我們通過SMTP伺服器傳送郵件,並在傳送完成後刪除生成的員工資料檔案。
import pandas as pd import smtplib from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.application import MIMEApplication import os from email.header import Header # 設定發件人郵箱和密碼 sender_email = '[email protected]' sender_password = '授權碼' # 設定SMTP伺服器和埠(QQ郵箱的SMTP伺服器和埠) smtp_server = 'smtp.163.com' smtp_port = 25 # 建立SMTP連線 server = smtplib.SMTP(smtp_server, smtp_port) server.starttls() # 開啟TLS加密 # 登入郵箱 server.login(sender_email, sender_password) # 讀取原始 Excel 檔案 df = pd.read_excel('C:\\Users\\liuchunlin2\\Desktop\\測試資料\\員工.xlsx') # 獲取唯一的員工姓名列表 employee_names = df['員工姓名'].unique() # 獲取員工姓名和對應的郵箱地址,假設這些資訊儲存在一個字典中 employee_emails = { '劉備': '[email protected]', '孫權': '[email protected]', '曹操': '[email protected]', # 新增更多員工和郵箱資訊 } # 遍歷員工資料並行送郵件 for employee_name in employee_names: # 建立員工的資料 employee_data = df[df['員工姓名'] == employee_name] # 生成員工的 Excel 檔案 employee_data_filename = f'{employee_name}.xlsx' employee_data.to_excel(employee_data_filename, index=False) employee_data_filename = f'{employee_name}.xlsx' # 建立郵件 msg = MIMEMultipart() msg['From'] = sender_email msg['To'] = employee_emails.get(employee_name, '') # 根據員工姓名獲取郵箱 msg['Subject'] = '拆分資料通知' body = f"尊敬的{employee_name},您的拆分資料已經準備好,請查收附件。" msg.attach(MIMEText(body, 'plain')) # 新增附件 with open(employee_data_filename, 'rb') as file: part = MIMEApplication(file.read(), Name=os.path.basename(employee_data_filename)) part.add_header('Content-Disposition', 'attachment', filename=Header(os.path.basename(employee_data_filename), 'utf-8').encode()) msg.attach(part) # 傳送郵件 server.sendmail(sender_email, employee_emails.get(employee_name, ''), msg.as_string()) # 刪除生成的員工資料檔案 os.remove(employee_data_filename) # 退出SMTP連線 server.quit()