我遇到了类似的问题,上面的答案都不适合我。我的问题是因为我的 smtp 服务器使用 SSL。我需要使用smtplib.SMTP_SSL
而不是smtplib.SMTP
. 我只是在下面发布我的完整工作代码。确保在执行之前更改req
和的值。email_config
req = {
"email": "to@something.com",
"subject": "new e-mail from python",
"body": "Hi,\nI am a bot",
}
email_config = {
"server": "smtp.orange.fr",
"port": 465,
"username": "username@orange.fr",
"password": "mypassword",
"email": "fromwhichemail@orange.fr",
}
#!/usr/bin/env python3
import smtplib
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
class Mail(object):
def __init__(self, email_config):
self.email = email_config["email"]
self.password = email_config["password"]
self.server = email_config["server"]
self.port = email_config["port"]
print(f"Logging to {self.server}:{self.port}")
session = smtplib.SMTP_SSL(self.server, self.port)
print(f"Calling ehlo")
session.ehlo()
print(f"Calling login")
session.login(self.email, self.password)
self.session = session
def send_message(self, subject, to, body, filename):
# Create a multipart message and set headers
message = MIMEMultipart()
message["From"] = self.email
message["To"] = to
message["Subject"] = subject
message["Bcc"] = ""
# Add body to email
message.attach(MIMEText(body, "plain"))
print(f"tot: {to}")
print(f"subject: {subject}")
print(f"body: {body}")
if filename is not None:
# Open PDF file in binary mode
with open(filename, "rb") as attachment:
part = MIMEBase("application", "octet-stream")
part.set_payload(attachment.read())
# Encode file in ASCII characters to send by email
encoders.encode_base64(part)
# Add header as key/value pair to attachment part
part.add_header(
"Content-Disposition",
f"attachment; filename= {filename}",)
# Add attachment to message and convert message to string
message.attach(part)
# Send e-mail
print(f"Sending e-mail...")
self.session.sendmail(self.email, to, message.as_string())
if __name__ == "__main__":
req = {
"email": "to@something.com",
"subject": "new e-mail from python",
"body": "Hi,\nI am a bot",
}
email_config = {
"server": "smtp.orange.fr",
"port": 465,
"username": "username@orange.fr",
"password": "mypassword",
"email": "fromwhichemail@orange.fr",
}
m = Mail(email_config)
if "pdf_file" in req:
m.send_message(req["subject"], req["email"], req["body"], req["pdf_file"])
else:
m.send_message(req["subject"], req["email"], req["body"], None)