问题
I am writing a python script to help a friend's after-school program to send tuition bill to parents. I have student tuition in a {studentName, tuition}
dict, and parent contacts in another {studentName, parentContact}
dict, due to different attendance, each student's tuition number is different. I have the following function to intend to send individual email with different tuition info to individual parent. The problem I am having is, when I run my python script, from python shell, when I print it out, it looks like different email with different tuition numbers was intended for different parents, but the actual email each parent received was the same copy of the first student's tuition info. Here is my send email function:
def send_mail(studentParentContact, studentTuition):
msg=MIMEMultipart()
msg['Subject'] = 'Your kid After-school Program Tuition Bill'
msg['From'] = FLPEmail #after-school program's email address
# Send the message via SMTP server
s = smtplib.SMTP('smtp.gmail.com', 587)
s.starttls()
s.login(username, PASSWORD) #after-school program gmail login
for student in studentParentContact:
ParentEmail=studentParentContact[student]
tuition=studentTuition[student]
msg['To'] = ParentEmail
body="Hi, \nThe tuition for "+student+' is '+'$'+ str(tuition)
msg.attach(MIMEText(body, 'plain'))
text=msg.as_string()
print FLPEmail, ParentEmail, text
s.sendmail(FLPEmail, ParentEmail, text)
s.quit()
So instead of sending student A's tuition info to A's parent, student B's tuition info to B's parent, student C's tuition info to C's parent, the actual outcome right now is that it is sending A's tuition info to A and B and C's parents. Same tuition to all parents. How do I fix this? Thank you very much!
回答1:
You are adding another body part each time through the loop. So the first parent receives only their own body part, the second parent receives two body parts, the first of which is that of the previous student, etc. By the end of the loop, the last parent receives n different body parts, where the last one is actually theirs, but they also receive those of all the previous students.
I would simply move the msg = MimeMultipart()
etc initialization inside the loop; recycling an empty multipart is a dubious optimization anyway (and of course as of now, the one you were recycling wasn't empty).
If you are only sending a single part, putting it inside a multipart container is pointless, anyway.
def send_mail(studentParentContact, studentTuition):
s = smtplib.SMTP('smtp.gmail.com', 587)
s.starttls()
s.login(username, PASSWORD)
for student in studentParentContact:
ParentEmail=studentParentContact[student]
tuition=studentTuition[student]
body="Hi, \nThe tuition for "+student+' is '+'$'+ str(tuition)
msg=MIMEText(body, 'plain')
msg['Subject'] = "Your Kid's After-school Program Tuition Bill"
msg['From'] = FLPEmail
msg['To'] = ParentEmail
text=msg.as_string()
#print FLPEmail, ParentEmail, text
s.sendmail(FLPEmail, ParentEmail, text)
s.quit()
Having multiple dict
variables with the same keys isn't a bug, but it feels a bit cluttered. A common design is to have a dict of dicts, where each record has a number of identical keys (in this case, maybe student[s]['parent_email']
to get the parent's email, student[s]['name']
to get the student's full name, student[s]['tuition']
to get the tuition fee, etc. Eventually you might want to encapsulate these as attributes of a student
class.)
来源:https://stackoverflow.com/questions/48521036/how-to-send-different-content-email-to-different-recipient-in-python