sendmail 在 Orange Pi 上的 Python 中不起作用

sendmail 在 Orange Pi 上的 Python 中不起作用

我遇到了一个无法解释的问题 - 除非是配置问题:

在 AMD64 Ubuntu 22.04 安装中,我可以从 Python 成功运行一个简单的 sendmail 脚本,但在 Orange Pi Ubuntu 22.04 安装中失败。错误如下:

STARTTLS extension not supported by server.
Traceback (most recent call last):
File "/root/python_sendmail.py", line 34, in <module>
server.quit() 
File "/usr/lib/python3.10/smtplib.py", line 1004, in quit
res = self.docmd("quit")
File "/usr/lib/python3.10/smtplib.py", line 432, in docmd
return self.getreply()
File "/usr/lib/python3.10/smtplib.py", line 405, in getreply
raise SMTPServerDisconnected("Connection unexpectedly 
closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly 
closed

代码如下:

# Sending mail with Python
import smtplib, ssl

smtp_server = "smtp.gmail.com"
port = 587  # For starttls
sender_email = [email protected]"
password = 'xxxxxxxxxxxxxxx'


# Create a secure SSL context
context = ssl.create_default_context()

# Try to log in to server and send email
try:
server = smtplib.SMTP(smtp_server,port)
server.ehlo() # Can be omitted
server.starttls(context=context) # Secure the connection
server.ehlo() # Can be omitted
server.login(sender_email, password)
# TODO: Send email here
sender_email = "[email protected]"
receiver_email = "[email protected]"
message = """\
Subject: Hi there

This message is sent from Python."""

# Send email here
server.sendmail(sender_email, receiver_email, message)
except Exception as e:
# Print any error messages to stdout
print(e)
finally:
server.quit() 

我刚刚在 Orange Pi 上重新安装了全新的 Jammy 图像,并且该脚本之前已在 Orange Pi 上成功运行。

相关内容