首页 > 解决方案 > 如何使用 python 请求将电子邮件发送到本地电子邮件服务器

问题描述

我已经设置了一个本地电子邮件服务器,如下所述:

https://gist.github.com/raelgc/6031274

我使用以下 python 代码尝试发送电子邮件:

import requests
requests.post('http://localhost.com')

我收到以下错误:

requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost.com', port=80): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f0c75631f10>: Failed to establish a new connection: [Errno 111] Connection refused',))

我尝试禁用防火墙 ( sudo ufw disable),指定 pop3 和 IMAP 端口并使用不同的方案,但我继续收到此错误。

我怀疑这可能是因为requests只支持http。如果是这样,那么我如何实现使用requests.

标签: pythonnetwork-programmingpython-requestspop3

解决方案


Postfix 是一个邮件服务器,使用 TCP 端口 25、465 和 587。它使用 SMTP 进行通信,据我所知,没有可用的 API 接口。如果你想发送需要 smtplib 的电子邮件,请求将不起作用。

带有后缀服务器的 python smtplib 示例:http: //masnun.com/2010/01/01/sending-mail-via-postfix-a-perfect-python-example.html


推荐阅读