8

I'm working on an project where I have to use the smtplib and email modules in Python 3.4 to send an email.

I'm able to create the email itself and I'm able to connect to the server, but then it returns this Exception:

reply: b'235 2.7.0 Accepted\r\n'
reply: retcode (235); Msg: b'2.7.0 Accepted'
send: 'QUIT\r\n'
reply: b'221 2.0.0 closing connection s66sm8304113yhp.2 - gsmtp\r\n'
reply: retcode (221); Msg: b'2.0.0 closing connection s66sm8304113yhp.2 - gsmtp'
Traceback (most recent call last):
  File "base.py", line 108, in <module>
    send(fromaddr, toaddrs, msg)
  File "base.py", line 61, in send
    server.send_message(fromaddr, toaddrs, msg)
  File "/usr/lib/python3.4/smtplib.py", line 829, in send_message
    resent = msg.get_all('Resent-Date')
AttributeError: 'str' object has no attribute 'get_all'

The code (troublesome line linked directly to) is available here. Strangely enough, the code actually sends QUIT prior to actually sending any of the email body - not sure if that's something that would affect this.

Does anyone know what is causing this error?

EDIT Turns out that part of my issue was that I was using the incorrect format. send_message() requires the variables in the order of Message, From, To, while my code was sending it in the order of From, To, Message.

However, I'm now getting this error:

reply: b'235 2.7.0 Accepted\r\n'
reply: retcode (235); Msg: b'2.7.0 Accepted'
send: 'QUIT\r\n'
reply: b'221 2.0.0 closing connection s66sm8443316yhp.2 - gsmtp\r\n'
reply: retcode (221); Msg: b'2.0.0 closing connection s66sm8443316yhp.2 - gsmtp'
Traceback (most recent call last):
  File "MIME-base.py", line 108, in <module>
    send(fromaddr, toaddrs, msg)
  File "MIME-base.py", line 61, in send
    server.send_message(msg, fromaddr, toaddrs)
  File "/usr/lib/python3.4/smtplib.py", line 839, in send_message
    g.flatten(msg_copy, linesep='\r\n')
  File "/usr/lib/python3.4/email/generator.py", line 109, in flatten
    self._write(msg)
  File "/usr/lib/python3.4/email/generator.py", line 189, in _write
    self._write_headers(msg)
  File "/usr/lib/python3.4/email/generator.py", line 416, in _write_headers
    self._fp.write(self.policy.fold_binary(h, v))
  File "/usr/lib/python3.4/email/_policybase.py", line 325, in fold_binary
    folded = self._fold(name, value, sanitize=self.cte_type=='7bit')
  File "/usr/lib/python3.4/email/_policybase.py", line 352, in _fold
    parts.append(h.encode(linesep=self.linesep,
AttributeError: 'list' object has no attribute 'encode'
RPiAwesomeness
  • 5,009
  • 10
  • 34
  • 52

2 Answers2

9

The signature for SMTP.send_message is not the same as SMTP.sendmail. So try:

server.send_message(msg, fromaddr, toaddrs)

EDIT:

You also need to add the To: headers as a string, rather than as a list:

receivers = ['someone@abc.com', 'someone@xyz.com']
msg['To'] = ', '.join(receivers)
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
  • Thanks for pointing that out, I had missed the difference. I'm not getting that error any more, but I'm now getting this error: `AttributeError: 'list' object has no attribute 'encode'`. I'll update my question with the full traceback. – RPiAwesomeness Apr 27 '15 at 16:23
  • Never mind, just figured out the new error on my own. It was caused by the `msg['To'] = input().split()` line, since that takes the input and makes a list. Thanks, accepting your answer. – RPiAwesomeness Apr 27 '15 at 16:52
  • @RPiAwesomeness. Yes: you need to add each item separately (I already updated my answer). – ekhumoro Apr 27 '15 at 16:55
  • This saved me. I really wish this had been called out explicitly somewhere. Thanks @ekhumoro! – TopherGopher Sep 02 '18 at 01:23
  • hi, help please. even am getting same error. despite following this format. `server.send_message(msg_body,message['From'],rec_list)` – The Great Apr 05 '22 at 04:08
  • My rec_list looks like below = `message['To'] = ", ".join(rec_list)`. I also passed `message['To']` instead of `rec_list` in send_message function but still it throws error. can help please? `AttributeError: 'str' object has no attribute 'get_all'` – The Great Apr 05 '22 at 04:12
  • Here is the post - can help me please? https://stackoverflow.com/questions/71746290/python-smtplib-send-message-with-list – The Great Apr 05 '22 at 04:27
0

What caused the error for me was the fact that I was mistakingly using send_message instead of sendmail, which are similar but have different argument types

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 21 '23 at 13:58