Jabda
Jabda

Reputation: 1792

Sending HTML email from Python

I am trying to send HTML email via python on a unix server. I need to use p.communicate for other reasons. I'd like not to change this code too much so I dont have to fix all the scripts that use this function.

What I am trying to do is send to multiple to_add. it works with one but not more. I've tring to_address as a list, a string with addresses separated by comma.

html = MIMEText("<html><body>"+content+"</body>", "html")
msg = MIMEMultipart("alternative")
msg['To'] =  email.utils.formataddr(('Recipient', to_address))
msg['From'] = email.utils.formataddr(('Author', 'author@company'))
msg['Subject'] = subject
msg.attach(html)
p = Popen(["/usr/sbin/sendmail", "-f","author@company","-t"], stdin=PIPE)
p.communicate(msg.as_string())

works with

"[email protected]"

doesnt work with

"[email protected],[email protected]" ["[email protected]","[email protected]"]

Upvotes: 0

Views: 191

Answers (1)

zwer
zwer

Reputation: 25829

Try separating your addresses with a space, not a comma. Alternatively you can just pass the list of addresses directly to the /usr/sbin/sendmail call (and omit them from the MIMEMultipart structure):

email_addresses = ["[email protected]", "[email protected]"]
p = Popen(["/usr/sbin/sendmail",
           "-f", "[email protected]",
           ",".join(email_addresses)] , stdin=PIPE)

Upvotes: 1

Related Questions