如何在Python中将不同内容的电子邮件发送给不同的收件人? [英] How to send different content email to different recipient in Python?

查看:401
本文介绍了如何在Python中将不同内容的电子邮件发送给不同的收件人?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Python脚本,以帮助朋友的课后计划向父母发送学费。我在 {studentName,学费} 格中有学生学费,在另一个 {studentName,parentContact} 格中有父母学费,由于出勤率不同,每个学生的学费编号也不同。我具有以下功能,打算将具有不同学费信息的电子邮件发送给父母。我遇到的问题是,当我从python shell运行python脚本时,当我将其打印出来时,它看起来像是针对不同父母的,具有不同学费编号的不同电子邮件,但每个父母收到的实际电子邮件都是相同的副本第一个学生的学费信息。这是我的发送电子邮件功能:

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()

所以不要发送将学生A的学费信息发送给A的父母,将学生B的学费信息发送给B的父母,将学生C的学费信息发送给C的父母,目前的实际结果是,它将A的学费信息发送给A和B以及C的父母。所有父母的学费相同。我该如何解决?

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!

推荐答案

您每次在循环中都添加另一个正文部分。因此,第一个父母仅接收自己的身体部位,第二个父母则接收两个身体部位,第一个是前一个学生的身体部位,依此类推。循环结束时,最后一个父母接收 n 不同的身体部位,最后一个实际上是他们的身体部位,但是他们也接受了以前所有学生的身体部位。

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.

我只需要移动 msg = MimeMultipart()等在循环内初始化;无论如何,回收一个空的零件是一个可疑的优化(当然,到目前为止,您回收的那个还不是空的)。

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).

如果您只发送一个

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()

具有多个具有相同键的dict 变量不是bug,但感觉有点混乱。常见的设计是具有dict dict,其中每个记录都具有许多相同的键(在这种情况下,可能是 student [s] ['parent_email'] 父母的电子邮件学生['name'] 以获得学生的全名,学生[s] ['tuition'] 获取学费等。最终,您可能希望将它们封装为 student 类的属性。)

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.)

这篇关于如何在Python中将不同内容的电子邮件发送给不同的收件人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆