词典中的循环键发送电子邮件(Python)

发布于 2025-01-28 21:06:49 字数 610 浏览 1 评论 0原文

我正在尝试循环浏览一本包含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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小梨窩很甜 2025-02-04 21:06:49

您正在做的是为您想做的每个动作使用一个循环,而是为所有操作做一个循环:

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)
      body = 'Body {0}. Body2 {1}.'.format(keyyy,keyyy)
      msg = f'subject: {subject}\n\n{body}'
      zero.sendmail(Emaill,Prueba[keyy],msg)

这应该在正确的邮件中具有正确的主题和身体。尚未实际测试代码,但这就是它应该使用的方式。

What you are doing is use one loop for every action you wanna do, instead do one loop for all the actions:

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)
      body = 'Body {0}. Body2 {1}.'.format(keyyy,keyyy)
      msg = f'subject: {subject}\n\n{body}'
      zero.sendmail(Emaill,Prueba[keyy],msg)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文