通过电子邮件(python)发送列表中的项目时遇到问题



我想使用python通过电子邮件发送一些数据(在列表中(,但我遇到了一个问题。这是我的代码:

import smtplib
from email.message import EmailMessage
tokens = ['stfen1','stfen2','stfen3','stfen4','stfen5']
ids = ['idfstfen1','idfstfen1','idfstfen1','idfstfen1','idfstfen1',]
num = 1
EMAIL_ADDRESS = ('example@gmail.com')
EMAIL_PASSWORD = ('password')
msg = EmailMessage()
msg['Subject'] = f'EMP Data'
msg['From'] = f'Name <{EMAIL_ADDRESS}>'
msg['To'] = f'Name <{EMAIL_ADDRESS}>'
for token,id in zip(tokens,ids):
msg.set_content(f'Datan{num}: Account ID->{id}--Account Token->{token}n')
num += 1
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
smtp.send_message(msg)

我在电子邮件中期望的输出是:


Data
1: Account ID-> idfstfen1--Account Token-> stfen1
Data
2: Account ID-> idfstfen1--Account Token-> stfen2
Data
3: Account ID-> idfstfen1--Account Token-> stfen3
Data
4: Account ID-> idfstfen1--Account Token-> stfen4
Data
5: Account ID-> idfstfen1--Account Token-> stfen5

我得到的输出:

Data
5: Account ID-> idfstfen1--Account Token-> stfen5

您必须首先创建一个msg变量,并在for循环中连接tokenid

msg = ''
for token,id in zip(tokens,ids):
msg += f'Datan{num}: Account ID->{id}--Account Token->{token}n'
num += 1
msg.set_content(msg)
with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
smtp.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
smtp.send_message(msg)

相关内容

  • 没有找到相关文章

最新更新