Reputation: 45
I want to send an email to multiple users. Right now, I am only able to send it to one user. I want to make a property file so that whenever I need to add or remove any user from the list, I dont have to edit anything in .py file.
I am able to send the email to one user
import smtplib
import email.utils
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
me = [email protected]
you = [email protected]
msg = MIMEMultipart('alternative')
msg['Subject']="subject of the email"
msg['From"]=me
msg['To']=you
text="hi"
html= '''body of email'''
part1=MIMEText(text, 'plain')
part1=MIMEText(html, 'html')
msg.attach(part1)
msg.attach(part2)
s = smtplib.SMTP(host,port)
s.sendmail(me,you, msg.as_string())
s.quit()
the email should go to multiple users.
Upvotes: 0
Views: 112
Reputation: 189327
The argument you
can be a list of email addresses. You will need to adapt your code to set the To:
header to accept a list of strings
msg['To'] = '.'.join(you)
or you can use a placeholder like
msg['To'] = 'undisclosed-recipients:;'
but other than that, your existing code should just work.
In some more detail, the latter is basically equivalent to putting all the addresses in a Bcc:
header, so that the recipients cannot see each others' addresses.
To populate you
from a file, try
with open(filename) as f:
you = f.readlines()
where the file contains one email address per line.
Upvotes: 1