-1

I have an email address like my-email@domain.com Im getting an error:

TypeError: unhashable type: 'list'

This is the gist of the code I am using:

subject = "Email"
msg['From'] = 'me@company.com'
recipients = ['myemail@domain.com,my-email@domain.com'] 
emaillist = [elem.strip().split(',') for elem in recipients]

msg.attach(message)
server = smtplib.SMTP('00.0.0.00', 25)
server.sendmail(msg['From'], emaillist , msg.as_string())

It works fine if I remove my-email@domain.com from the recipients list.

Any advice?

thentangler
  • 1,048
  • 2
  • 12
  • 38
  • 2
    This has nothing to do with special characters. The problem is that `emaillist` is a 2-dimensional list. Why is `recipients` a list instead of just a single string? – Barmar Jun 28 '22 at 20:38
  • I'm pretty sure you'll get the same error if you remove `my-email@domain.com`. The list comprehension still creates a 2-dimensional list. – Barmar Jun 28 '22 at 20:39
  • 1
    Just flatten `emaillist`. See https://www.geeksforgeeks.org/python-ways-to-flatten-a-2d-list/ – Barmar Jun 28 '22 at 20:40
  • @Barmar yup that was the issue! Thank You! – thentangler Jun 29 '22 at 15:43

1 Answers1

1
import smtplib, ssl

port = 465  # For SSL
smtp_server = "smtp.gmail.com"
sender_email = "my@gmail.com"  # Enter your address
receiver_email = "your@gmail.com"  # Enter receiver address
password = input("Type your password and press enter: ")
message = """\
Subject: Hi there

This message is sent from Python."""

context = ssl.create_default_context()
with smtplib.SMTP_SSL(smtp_server, port, context=context) as server:
    server.login(sender_email, password)
    server.sendmail(sender_email, receiver_email, message)

for more details ypu can look at https://realpython.com/python-send-email/#:~:text=Set%20up%20a%20secure%20connection,attachments%20using%20the%20email%20package