我写了一个 Python 脚本,它发送一封带有附件的电子邮件,但我总是收到相同的错误消息:
raise SMTPSenderRefused(code, resp, from_addr)
smtplib.SMTPSenderRefused: (552, b'5.3.4 Message size exceeds fixed maximum mess
age size', 'notification@company.com')
如何更改服务器上的最大邮件大小限制以避免此错误消息并正确发送带有附件的电子邮件?
我用我的代码更新了我的问题:
emailfrom = "myemailadress"
emailto = "1.person"
emailto = "2.person"
fileToSend = "data.csv"
username = "user"
password = "password"
msg = MIMEMultipart()
msg["From"] = emailfrom
msg["To"] = emailto
msg["Subject"] = "subject"
msg.preamble = "subject"
ctype, encoding = mimetypes.guess_type(fileToSend)
if ctype is None or encoding is not None:
ctype = "application/octet-stream"
maintype, subtype = ctype.split("/", 1)
if maintype == "text":
fp = open("data.csv")
# Note: we should handle calculating the charset
attachment = MIMEText(fp.read(), _subtype=subtype)
fp.close()
elif maintype == "image":
fp = open("data.csv", "rb")
attachment = MIMEImage(fp.read(), _subtype=subtype)
fp.close()
elif maintype == "audio":
fp = open("data.csv", "rb")
attachment = MIMEAudio(fp.read(), _subtype=subtype)
fp.close()
else:
fp = open("data.csv", "rb")
attachment = MIMEBase(maintype, subtype)
attachment.set_payload(fp.read())
fp.close()
encoders.encode_base64(attachment)
attachment.add_header("Content-Disposition", "attachment", filename="data.csv")
msg.attach(attachment)
#server = smtplib.SMTP("smtp.gmail.com:587")
server = smtplib.SMTP('smtp.upcmail.hu', 25)
#server.starttls()
#server.login(username,password)
server.sendmail(emailfrom, emailto, msg.as_string())
server.quit()
time.sleep(5)
我不知道如何更改服务器上的最大消息大小限制,我使用的是 Debian。
最佳答案
这看起来像是来自 postfix 的错误消息。
您可以使用
检查大小限制postconf message_size_limit
并增加它
postconf -e 'message_size_limit = 104857600'
即 100 MB。之后你必须重新加载配置
service postfix reload
请记住,Postfix 在发送附件之前会对其进行转换,因此大小限制必须稍大一些。
关于python - 如何更改服务器上的最大邮件大小限制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47259008/