如何更改服务器上的最大消息大小限制?

Har*_*ley 5 python email smtp smtpclient email-attachments

我编写了一个 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')
Run Code Online (Sandbox Code Playgroud)

如何更改服务器上的最大邮件大小限制以避免此错误消息并正确发送带有附件的电子邮件?

我用我的代码更新了我的问题:

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) 
Run Code Online (Sandbox Code Playgroud)

我不知道如何更改服务器上的最大消息大小限制,我使用的是 Debian。

Leo*_*ndl 2

这看起来像是来自 postfix 的错误消息。

您可以使用以下命令检查大小限制

postconf message_size_limit
Run Code Online (Sandbox Code Playgroud)

并增加它

postconf -e 'message_size_limit = 104857600'
Run Code Online (Sandbox Code Playgroud)

即 100 MB。之后你必须重新加载配置

service postfix reload
Run Code Online (Sandbox Code Playgroud)

请记住,Postfix 在发送附件之前会对其进行转换,因此大小限制必须稍大一些。