I want to send some data(in lists) through Email using python but I am facing an issue. Here is my code:
import smtplib
from email.message import EmailMessage
tokens = ['stfen1','stfen2','stfen3','stfen4','stfen5']
ids = ['idfstfen1','idfstfen1','idfstfen1','idfstfen1','idfstfen1',]
num = 1
EMAIL_ADDRESS = ('[email protected]')
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'Data\n{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)
The Output I'm expecting in my email is:
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
The output I'm getting:
Data
5: Account ID-> idfstfen1--Account Token-> stfen5
CodePudding user response:
You have to create a msg
variable first, and concatenate token
and id
s in the for loop
msg = ''
for token,id in zip(tokens,ids):
msg = f'Data\n{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)