0

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]"]

0

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

2 Comments

The -t option to Sendmail should be omitted in the alternative scenario.
@tripleee - Whoops, I happened to be a victim of copy/pasta :(. Good catch!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.