python - 如何更改服务器上的最大邮件大小限制?

标签 python email smtp email-attachments smtpclient

我写了一个 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/

相关文章:

java - 无法在java中使用pop3访问所有 'gmail'消息

php - 单击电子邮件中的 Outlook 安全链接保护链接似乎执行了两次代码

node.js - Node `smtp-server` 模块无法使用 TLS

c# - 测试 SMTP 服务器正在通过 C# 运行

python - 值错误 : Attempt to reuse RNNCell with a different variable scope than its first use

python - 如何以 png 格式保存 Plotly 离线图?

python - Openpyxl:似乎无法获得适合我想要做的事情的语法(向下阅读下一个单元格)

django - 验证电子邮件是从单元测试发送的

python - 反转列表中的字符串并进一步反转嵌套列表中的列表

email - 如何在Meteor中使用自己的邮件服务器发送电子邮件?