通过Mailgun向词典列表发送多封电子邮件

2024-06-02 13:50:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个字典列表,我正试图在为Mailgun发送电子邮件时传递“to”参数。像这样:

users = [{'User': entry.name.value, 'Email': entry.mail.value, 'Number': entry.telephoneNumber.value,'Days': days_left.days}]

还有,剩下的日子只是我做的一些数学运算,减去他们最后一次设置密码的时间-今天

days_left = (pwd_expire_date.astimezone(tz=None) - today)

我的函数如下所示:

        def send_email():
            for d in notify_users:
                return requests.post(
                    "https://api.mailgun.net/v3/MYDOMAIN/messages", auth=("api", "MAILGUN_API_KEY"),
                    data={"from": "IT <mailgun@MYDOMAIN>",
                          "to": d['Email'],
                          "subject": "Password Reminder",
                          "text": f"Hello {d['User']}, you have {d['days']} days left to reset your password. -IT"})
            for d in expired_users:
                return requests.post(
                    "https://api.mailgun.net/v3/MYDOMAIN/messages", auth=("api", "MAILGUN_API_KEY"),
                    data={"from": "IT <mailgun@MYDOMAIN>",
                          "to": d['Email'],
                          "subject": "Password Reminder",
                          "text": f"Hello {d['User']}, your password has expired, please change it asap. -IT"})
        send_email()

在“to”值中传入字典似乎只允许列表中的第一封电子邮件,这不是我想要的。我一定是做了什么语法错误的事。我知道你可以将每封邮件都传递到“收件人”字段中,这样就可以了,但是否有必要将字典列表传递到“收件人”字段中?这种方法在TwilioAPI中可以发送多条短信,但Mailgun似乎不喜欢它

我的脚本用于在用户需要重置密码30,15,3天后发送密码提醒。notify_用户是指仍然拥有有效密码但只需要提醒的任何人,过期的_用户是指密码不再有效的任何人。我想传递的每封电子邮件的名称,以及该特定用户的剩余天数。这样做的语法是正确的,因为我已经发送了测试电子邮件,并且它传递了正确的值。我只是无法将字典列表作为to参数传入。如果有人有任何见解,这将是非常有益的

这是我期望得到的预期结果:

each user in the list, for both expired users and users receives a unique email.

'Hello John Apple, you have 15 days left to reset your password. - IT'
'Hello Sandy Cheeks, your password has expired, please change it asap - IT'

Tags: toapi密码hello列表your字典it