3

Tried with only multiple to and multiple cc individually, which works fine but when i try both i get an error:

File

"path\Continuum\anaconda2\envs\mypython\lib\smtplib.py", line 870, in sendmail senderrs[each] = (code, resp) TypeError: unhashable type: 'list'"

Code:

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.application import MIMEApplication

strFrom = '[email protected]'

cc='[email protected], [email protected]'

to='[email protected],[email protected]'

msg = MIMEMultipart('related')
msg['Subject'] = 'Subject'
msg['From'] = strFrom
msg['To'] =to
msg['Cc']=cc

#msg['Bcc']= strBcc

msg.preamble = 'This is a multi-part message in MIME format.'


msgAlternative = MIMEMultipart('alternative')
msg.attach(msgAlternative)

msgText = MIMEText('This is the alternative plain text message.')
msgAlternative.attach(msgText)


msgText = MIMEText('''<html>

<body><p>Hello<p>
        </body>
        </html> '''.format(**locals()), 'html')
msgAlternative.attach(msgText)



import smtplib
smtp = smtplib.SMTP()
smtp.connect('smtp address')
smtp.ehlo()
smtp.sendmail(strFrom, to, msg.as_string())
smtp.quit()
3
  • 1
    Please post the calling code. Commented Mar 2, 2018 at 4:44
  • If you can't put anything useful in the text/plain part, maybe you should not be producing multipart/alternative at all. (I know some people do this as an empty gesture towards some spam filters which require this, but you are really doing your recipients a disservice by sending them junk.) Commented Mar 2, 2018 at 5:51
  • This question is similar to: How to send email to multiple recipients using python smtplib?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Apr 10 at 11:27

3 Answers 3

7

Attaching To or from should be a string and sendmail should always be in the form of the list.

cc=['[email protected]', '[email protected]']

to=['[email protected]','[email protected]']

msg['To'] =','.join(to)

msg['Cc']=','.join(cc)   

toAddress = to + cc    

smtp.sendmail(strFrom, toAddress, msg.as_string())
Sign up to request clarification or add additional context in comments.

2 Comments

@GufranHasan can you please tell me reason for downvote so i would improve my answer??
I didn't downvote dear. Just I edit your post for users attraction.
5

The to parameter should be a list of all the addresses you wish to send the message to. The division in To: and Cc: is basically for display purposes only; SMTP simply has a single sequence of recipients which translate to one RCPT TO command for each address.

def addresses(addrstring):
    """Split in comma, strip surrounding whitespace."""
    return [x.strip() for x in addrstring.split(',')]

smtp.sendmail(strFrom, addresses(to) + addresses(cc), msg.as_string())

Comments

0

Change two lines of code:

  1. the list of recipients

From single string:

to='[email protected],[email protected]'

to a list of strings:

to=['[email protected]','[email protected]']
  1. the message attribute

From a list of strings:

msg['To'] =to

to a single string with comma-separated addresses:

msg['To'] =",".join(to)

2 Comments

The answer is easier to comprehend when just the working code is posted - like in this answer. This way we can copy-paste. Any issues with the code in question can and should be explained in text or comments.
I'll keep in mind.

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.