词典中的循环键发送电子邮件(Python)
我正在尝试循环浏览一本包含4个项目的字典中的键。 但是,它并不是所有这些,只显示了最后一项的关键。
Prueba = {"A":"###@gmail.com",
"B":"###@gmail.com",
"C":"###@gmail.com"}
with smtplib.SMTP('smtp.gmail.com',587) as zero:
zero.ehlo()
zero.starttls()
zero.ehlo()
zero.login(Emaill, Passs)
for keyyy in Prueba.keys():
subject = 'Subject {0}'.format(keyyy)
for keyyy in Prueba.keys():
body = 'Body {0}. Body2 {1}.'.format(keyyy,keyyy)
msg = f'subject: {subject}\n\n{body}'
for keyy in pp.Prueba:
zero.sendmail(Emaill,Prueba[keyy],msg)
发送电子邮件时。所有电子邮件仅显示最后一项的键(“ C”)。我想用各自的密钥显示每个电子邮件。
I am trying to loop over a key in a dictionary that has 4 items in total.
However, it doesn't go through all of them and only shows the key of the last item.
Prueba = {"A":"###@gmail.com",
"B":"###@gmail.com",
"C":"###@gmail.com"}
with smtplib.SMTP('smtp.gmail.com',587) as zero:
zero.ehlo()
zero.starttls()
zero.ehlo()
zero.login(Emaill, Passs)
for keyyy in Prueba.keys():
subject = 'Subject {0}'.format(keyyy)
for keyyy in Prueba.keys():
body = 'Body {0}. Body2 {1}.'.format(keyyy,keyyy)
msg = f'subject: {subject}\n\n{body}'
for keyy in pp.Prueba:
zero.sendmail(Emaill,Prueba[keyy],msg)
When the email is sent. All the emails only show the key of the last item ("C"). I want to show each email with its respective key.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在做的是为您想做的每个动作使用一个循环,而是为所有操作做一个循环:
这应该在正确的邮件中具有正确的主题和身体。尚未实际测试代码,但这就是它应该使用的方式。
What you are doing is use one loop for every action you wanna do, instead do one loop for all the actions:
This should have the right subject and body in the right mail. Haven't actually tested the code yet, but that's the way it should work.