我一直在尝试制作一个python脚本,在给定的日期发送一封生日快乐电子邮件,通过短信通知我它向谁发送了电子邮件。
我有3个文件。
首先我有一个英国日。csv文件,我输入所有将要使用的生日数据。
下面是一个文件中内容的示例:
name,email,year,month,day
Ivy,Testemail@mail.com,1,3,25
Rose,Testemail@mail.com,1,3,28
Kimberly,Testemail@mail.com,1,4,10
然后我有一个字母模板,脚本读取并用CSV文件中的名称替换[NAME]:
问候语,
祝[名字]生日快乐![姓名],😊我希望你过得愉快,祝你有更多的生活。
非常感谢。
然后我有了我的实际代码
############## IMPORTS ##############
import smtplib
import datetime as dtime
import pandas as pd
import random
from time import strftime
from email.mime.text import MIMEText
############## Reading Data and Check Current Day & Month ##############
# READ CSV BIRTHDAY FILE
df = pd.read_csv("birthdays.csv")
# PRINT CURRENT DAY
current_day = dtime.datetime.now().day
current_month = dtime.datetime.now().month
##################ENTER LOGIN HERE#############################
LOGIN = "EMAIL"
PASS = "PASSWORD"
############## LOGIC ##############
# save the rows that has the current day in new variable
new_df = df.loc[df['day'] == current_day]
# check the length of new_df of the current month so if the result is larger than 1
# so there is birthdays on this day
if len(new_df.loc[new_df['month'] == current_month]) > 0:
# check the length of people having birthday in this day
for i in range(len(new_df.loc[new_df['month'] == current_month])):
# OPEN BIRTHDAY TEMPLATE
with open(f"./letter_1.txt") as letter_file:
# READING FILE
letter_contents = letter_file.read()
#CREATE NAME VARIABLE
name = df["name"][i]
# replace [NAME] with actual name on the data
if len(new_df["name"]) > 1:
the_letter = letter_contents.replace("[NAME]", new_df["name"][i])
the_email = new_df["email"][i]
else:
the_letter = letter_contents.replace("[NAME]", new_df["name"].item())
the_email = new_df["email"].item()
# SMTPLIB LOGIN TO SEND EMAIL
# CONNECTION
with smtplib.SMTP("smtp.outlook.com") as con:
# START
con.starttls()
# LOGIN
con.login(user=LOGIN, password=PASS)
# create the msg
msg = MIMEText(the_letter, 'html')
msg["From"] = LOGIN
msg["To"] = the_email
msg["Subject"] = "Happy Birthday " + name + "!!!"
msg["Cc"] = "CC EMAILS"
# SEND EMAIL
con.send_message(msg)
#SENDS TEXT MESSAGE CONFIRMATION
msg = MIMEText ("Sent Happy Birthday Email to " + name + " on " + str(text.strftime('%Y-%m-%d %H:%M:%S %p')))
msg["From"] = LOGIN
msg["To"] = "VERIZONPHONENUMBER@vtext.com"
# SEND TEXT
con.send_message(msg)
# LOGS OUT OF EMAIL
con.quit()
我认为问题出在我创建name变量的第45行。
例如,如果我运行脚本,电子邮件将如下所示
常春藤生日快乐!!!
问候语,
我祝罗斯生日快乐!罗斯#128522我希望你过得愉快,祝你有更多的生活。
我试图使主题上出现的名称与正文上出现的名称相匹配。