Не работает SMTP на виртуальном сервере
Пишу ТГ бота на аиограм, в котором есть функция: пользователь вводит данные, а они потом отправляются на нужную мне почту. Реализовал эту функцию через smtplib, все работало прекрасно, но теперь, как я загрузил бота на виртуальный сервер - он не отправляет мне письмо с данными и выдает ошибку. может быть я не установил что-то дополнительно на виртуальный сервер? Ubuntu 22.04
import smtplib
def send_email(text, encode='utf-8'):
user = '***'
passwd = '***'
server = 'smtp.mail.ru'
port = 587
subject = 'Запись на консультацию'
to = '*****'
charset = 'Content-Type: text/plain; charset=utf-8'
mime = 'MIME-Version: 1.0'
body = '\r\n'.join((f'From: {user}', f'To: {to}',
f'Subject: {subject}', mime, charset, '', text))
try:
smtp = smtplib.SMTP(server, port)
smtp.starttls()
smtp.ehlo
smtp.login(user, passwd)
smtp.sendmail(user, to, body.encode('utf-8'))
except smtplib.SMTPException as Ex:
print('Smth going wrong')
raise Ex
finally:
smtp.quit()
А вот так выглядит трейсбек
Traceback (most recent call last):
File "/root/bot/main.py", line 625, in <module>
executor.start_polling(dp, skip_updates=True)
File "/usr/local/lib/python3.10/dist-packages/aiogram/utils/executor.py", line 45, in start_polling
executor.start_polling(
File "/usr/local/lib/python3.10/dist-packages/aiogram/utils/executor.py", line 328, in start_polling
loop.run_until_complete(self._shutdown_polling())
File "/usr/lib/python3.10/asyncio/base_events.py", line 633, in run_until_complete
self.run_forever()
File "/usr/lib/python3.10/asyncio/base_events.py", line 600, in run_forever
self._run_once()
File "/usr/lib/python3.10/asyncio/base_events.py", line 1896, in _run_once
handle._run()
File "/usr/lib/python3.10/asyncio/events.py", line 80, in _run
self._context.run(self._callback, *self._args)
File "/usr/local/lib/python3.10/dist-packages/aiogram/dispatcher/dispatcher.py", line 415, in _process_polling_updates
for responses in itertools.chain.from_iterable(await self.process_updates(updates, fast)):
File "/usr/local/lib/python3.10/dist-packages/aiogram/dispatcher/dispatcher.py", line 235, in process_updates
return await asyncio.gather(*tasks)
File "/usr/local/lib/python3.10/dist-packages/aiogram/utils/executor.py", line 323, in start_polling
loop.run_forever()
File "/usr/lib/python3.10/asyncio/base_events.py", line 600, in run_forever
self._run_once()
File "/usr/lib/python3.10/asyncio/base_events.py", line 1896, in _run_once
handle._run()
File "/usr/lib/python3.10/asyncio/events.py", line 80, in _run
self._context.run(self._callback, *self._args)
File "/usr/local/lib/python3.10/dist-packages/aiogram/dispatcher/handler.py", line 117, in notify
response = await handler_obj.handler(*args, **partial_data)
File "/usr/local/lib/python3.10/dist-packages/aiogram/dispatcher/dispatcher.py", line 283, in process_update
return await self.callback_query_handlers.notify(update.callback_query)
File "/usr/local/lib/python3.10/dist-packages/aiogram/dispatcher/handler.py", line 117, in notify
response = await handler_obj.handler(*args, **partial_data)
File "/root/bot/main.py", line 306, in get_callback_restart
send_email(message_for_email)
File "/root/bot/send_email.py", line 18, in send_email
smtp = smtplib.SMTP(server, port)
File "/usr/lib/python3.10/smtplib.py", line 255, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python3.10/smtplib.py", line 341, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python3.10/smtplib.py", line 312, in _get_socket
return socket.create_connection((host, port), timeout,
File "/usr/lib/python3.10/socket.py", line 833, in create_connection
sock.connect(sa)
KeyboardInterrupt
ERROR:asyncio:Task exception was never retrieved
future: <Task finished name='Task-50' coro=<Dispatcher._process_polling_updates()
done, defined at /usr/local/lib/python3.10/dist-
packages/aiogram/dispatcher/dispatcher.py:407> exception=KeyboardInterrupt()>
Traceback (most recent call last):
File "/root/bot/main.py", line 625, in <module>
executor.start_polling(dp, skip_updates=True)
File "/usr/local/lib/python3.10/dist-packages/aiogram/utils/executor.py", line 45, in start_polling
executor.start_polling(
File "/usr/local/lib/python3.10/dist-packages/aiogram/utils/executor.py", line 328, in start_polling
loop.run_until_complete(self._shutdown_polling())
File "/usr/lib/python3.10/asyncio/base_events.py", line 633, in run_until_complete
self.run_forever()
File "/usr/lib/python3.10/asyncio/base_events.py", line 600, in run_forever
self._run_once()
File "/usr/lib/python3.10/asyncio/base_events.py", line 1896, in _run_once
handle._run()
File "/usr/lib/python3.10/asyncio/events.py", line 80, in _run
self._context.run(self._callback, *self._args)
File "/usr/local/lib/python3.10/dist-packages/aiogram/dispatcher/dispatcher.py", line 415, in _process_polling_updates
for responses in itertools.chain.from_iterable(await self.process_updates(updates, fast)):
File "/usr/local/lib/python3.10/dist-packages/aiogram/dispatcher/dispatcher.py", line 235, in process_updates
return await asyncio.gather(*tasks)
File "/usr/local/lib/python3.10/dist-packages/aiogram/utils/executor.py", line 323, in start_polling
loop.run_forever()
File "/usr/lib/python3.10/asyncio/base_events.py", line 600, in run_forever
self._run_once()
File "/usr/lib/python3.10/asyncio/base_events.py", line 1896, in _run_once
handle._run()
File "/usr/lib/python3.10/asyncio/events.py", line 80, in _run
self._context.run(self._callback, *self._args)
File "/usr/local/lib/python3.10/dist-packages/aiogram/dispatcher/handler.py", line 117, in notify
response = await handler_obj.handler(*args, **partial_data)
File "/usr/local/lib/python3.10/dist-packages/aiogram/dispatcher/dispatcher.py", line 283, in process_update
return await self.callback_query_handlers.notify(update.callback_query)
File "/usr/local/lib/python3.10/dist-packages/aiogram/dispatcher/handler.py", line 117, in notify
response = await handler_obj.handler(*args, **partial_data)
File "/root/bot/main.py", line 306, in get_callback_restart
send_email(message_for_email)
File "/root/bot/send_email.py", line 18, in send_email
smtp = smtplib.SMTP(server, port)
File "/usr/lib/python3.10/smtplib.py", line 255, in __init__
(code, msg) = self.connect(host, port)
File "/usr/lib/python3.10/smtplib.py", line 341, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python3.10/smtplib.py", line 312, in _get_socket
return socket.create_connection((host, port), timeout,
File "/usr/lib/python3.10/socket.py", line 833, in create_connection
sock.connect(sa)
KeyboardInterrupt
На винде и на Линукс Минт все работает, но мб из-за того что виртуалка - там чего-то не установлено