Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: 'NoneType' object has no attribute 'readuntil' #37

Closed
ghost opened this issue Jun 29, 2018 · 6 comments
Closed

AttributeError: 'NoneType' object has no attribute 'readuntil' #37

ghost opened this issue Jun 29, 2018 · 6 comments

Comments

@ghost
Copy link

ghost commented Jun 29, 2018

Hello,

I'm having trouble sending several emails in a loop. When I do so, I've got an error apparently spawning randomly. Do you have any ideas of what the issue is?

Please read the code below.

from time import sleep
import email
import asyncio
import aiosmtplib

loop = asyncio.get_event_loop()
for i in range(20):
    smtp = aiosmtplib.SMTP(hostname='gmail-smtp-in.l.google.com', port=25, loop=loop, use_tls=False)
    msg = email.message_from_string(my_eml)
    loop.run_until_complete(smtp.connect())
    loop.run_until_complete(smtp.starttls())
    loop.run_until_complete(smtp.send_message(msg))
    sleep(0.5)
    smtp.close()
loop.close()
Traceback (most recent call last):
  File "test.py", line 33, in <module>
    loop.run_until_complete(smtp.send_message(msg))
  File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/base_events.py", line 468, in run_until_complete
    return future.result()
  File "/Users/myuser/.local/share/virtualenvs/proxy-D8_I7f06/lib/python3.6/site-packages/aiosmtplib/smtp.py", line 229, in send_message
    sender, recipients, flat_message, timeout=timeout)
  File "/Users/myuser/.local/share/virtualenvs/proxy-D8_I7f06/lib/python3.6/site-packages/aiosmtplib/smtp.py", line 155, in sendmail
    response = await self.data(message, timeout=timeout)
  File "/Users/myuser/.local/share/virtualenvs/proxy-D8_I7f06/lib/python3.6/site-packages/aiosmtplib/esmtp.py", line 278, in data
    timeout=timeout)
  File "/Users/myuser/.local/share/virtualenvs/proxy-D8_I7f06/lib/python3.6/site-packages/aiosmtplib/protocol.py", line 120, in read_response
    line = await self._readline(timeout=timeout)
  File "/Users/myuser/.local/share/virtualenvs/proxy-D8_I7f06/lib/python3.6/site-packages/aiosmtplib/protocol.py", line 227, in _readline
    self._stream_reader.readuntil(separator=b'\n'), loop=self._loop)
AttributeError: 'NoneType' object has no attribute 'readuntil'

Python 3.6.5
aiosmtplib 1.0.2

Cheers,
Taslim42.

@skillian
Copy link

Though I'm not positive about this, my first suspicion is it might have to do with starting and stopping the loop between every call. I would recommend first moving all of your code that uses the smtp object into an async function and then passing the invocation of that function into loop.run_until_complete(my_async_function(smtp))

@ghost
Copy link
Author

ghost commented Jul 2, 2018

Hello, thanks for your quick response.

I have the same result with this code.

async def test(smtp):
    for i in range(30):
        msg = email.message_from_string(my_eml)
        await smtp.send_message(msg)
        sleep(0.5)

loop = asyncio.get_event_loop()
smtp = aiosmtplib.SMTP(hostname='gmail-smtp-in.l.google.com', port=25, loop=loop, use_tls=False)
loop.run_until_complete(smtp.connect())
loop.run_until_complete(smtp.starttls())

loop.run_until_complete(test(smtp))
smtp.close()

Have you an idea ?

@cole
Copy link
Owner

cole commented Jul 2, 2018

I can't reproduce error so far (testing with smtp.gmail.com). It seems like the server is dropping the connection without sending a response, but that should raise SMTPServerDisconnected.

One quick thing, for STARTTLS connections, typically the port is 587, not 25.

@ghost
Copy link
Author

ghost commented Jul 8, 2018

Yes, the server is dropping the connection without sending a response. I encountered this issue only with gmail. So, I will catch AttributeError exception. Thank for your help.

@ghost ghost closed this as completed Jul 8, 2018
@cole
Copy link
Owner

cole commented Sep 21, 2018

Going to reopen this as a reminder to myself to add a test case for it.

@cole cole reopened this Sep 21, 2018
@cole cole assigned cole and unassigned cole Sep 21, 2018
@cole
Copy link
Owner

cole commented Jan 14, 2019

Can't reproduce this still. Closing.

@cole cole closed this as completed Jan 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants