Open
Description
I ported this code from my old implementation on standard smtplib module, and perhaps it is not correct for aiosmtplib.
But i think this shouldn't matter.
import asyncio
import aiosmtplib
from email.mime.text import MIMEText
from email.header import Header
async def send_mail(host, login, password, from_addr, to_addr, subject, body_text):
msg=MIMEText(*body_text)
msg['Subject']=Header(subject, 'utf-8')
msg['From']=from_addr
msg['To']=to_addr
server=aiosmtplib.SMTP(host)
await server.connect()
await server.starttls()
await server.login(login, password)
try:
await server.sendmail(from_addr, [to_addr], msg.as_string())
except (aiosmtplib.SMTPRecipientsRefused, aiosmtplib.SMTPSenderRefused):
return 1
finally:
await server.quit()
#code for testing
async def start():
await send_mail('smtp.yandex.ru', '[email protected]', '1234', '[email protected]', '[email protected]', 'Testing', ['Hello', 'plain', 'utf-8'])
print('success')
#emulating work of other asyncio tasks...
await asyncio.sleep(3)
asyncio.run(start())```
```Python 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)] on win32
>>> import mail_test
success
>>> exit()
Future exception was never retrieved
future: <Future finished exception=SMTPServerDisconnected('Unexpected EOF received')>
aiosmtplib.errors.SMTPServerDisconnected: Unexpected EOF received```
If i comment two last lines of my function, it works good.
```>>> import mail_test
success
>>> exit()```