I'm sending customized emails to the people from an email list I took from an Excel sheet.
I have used MIMEMultipart
to send the email and attachment, and first person is getting the mail properly.
The second person is getting mail with the messages of the first person as well as their own. The third person is getting the message of first two as well as theirs.
This and goes on, each time adding another message to the mail. How do I reset the MIMEMultipart
after sending a single mail in python? My code is below:
for i in range(len(emails)):
name = names[i] + ","
email = emails[i] + "@nmamit.in"
TEXT = "my message" + name
msg['Subject'] = subjet_txt
msg['From'] = your_email
msg['To'] = email
msg.attach(MIMEText(TEXT, 'plain'))
text = msg.as_string()
server.sendmail(your_email, email, text)
server.quit()
You're not creating a new message in each iteration, it (msg
) appears to have been created before the loop starts. That means the line:
msg.attach(MIMEText(TEXT, 'plain'))
will attach another copy of the TEXT
each time through the loop, to the already-existing message.
The solution is to start with a fresh message each time, such as with:
for i in range(len(emails)):
message = MIMEMultipart() # Fresh message for each iteration.
name = names[i] + "," # Remainder of code as per current.
# ...