使用pythonsmtplib发送电子邮件失败?

2024-10-02 12:23:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我想配置一个Linux服务器以向我的组成员发送邮件。我编写了自动测试脚本,并使用python smtplib发送邮件。我配置了postfix、dovecot、cyrus sasl,后缀配置如下

当我使用smtplib连接邮件服务器时,python代码中出现了一个错误:

[Errno 113] No route to host

我确信python服务器也可以pingmail.cnbu-test.cncnbu-test.cn。我尝试将python shell移动到邮件服务器并运行它,然后出现错误:

raise SMTPException("SMTP AUTH extension not supported by server.")
smtplib.SMTPException: SMTP AUTH extension not supported by server.

我尝试将server.starttls()之类的代码添加到代码中,结果是服务器不支持tls。python代码如下所示:

# coding=utf-8
import smtplib
from email.mime.text import MIMEText
msg = MIMEText('hello,send by python_test...','plain','utf-8')
sender = 'cnbu_autotest@cnbu-test.cn'
password = 'xxxxxxxx'
receiver = 'abc@163.com'
mailto_list = ['abc@163.com']
smtp_server = 'mail.lenovo-cnbu-test.cn'
msg['From'] = sender
msg['To'] =';'.join(mailto_list)
msg['Subject'] = 'from IMYalost'

server = smtplib.SMTP(smtp_server,25)
server.ehlo()
#server.starttls()
server.login(sender,password)#ogin()
server.set_debuglevel(1)
server.sendmail(sender,mailto_list,msg.as_string())
server.quit()

如何配置postfix服务器以适应python代码


这是后缀配置

mail.cnbu-test.cn

mydomain = cnbu-test.cn

myorigin = $mydomain

inet_interfaces = all

inet_protocols = all

mydestination = $myhostname, $mydomain

message_size_limit = 20971520

mailbox_size_limit = 10737418240

smtp_sasl_auth_enable = yes

smtp_sasl_security_options = noanonymous

mynetworks = 127.0.0.0/8

smtpd_recipient_restrictions = permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination

Tags: 代码test服务器byserver邮件msgcn

热门问题