在发送Python smtplib之前,请检查电子邮件的完整大小

在发送Python smtplib之前,请检查电子邮件的完整大小,python,smtplib,Python,Smtplib,我在用Python通过smtplib通过电子邮件发送文件时遇到问题 我在Kubernetes Cluster-Jupyter笔记本上运行代码 我收到一个错误SMTPSenderRefused: SMTPSenderRefused Traceback (most recent call last) <ipython-input-72-9046ec8e66c3> in <module> 1 # Send messag

我在用Python通过smtplib通过电子邮件发送文件时遇到问题

我在Kubernetes Cluster-Jupyter笔记本上运行代码

我收到一个错误SMTPSenderRefused:

SMTPSenderRefused                         Traceback (most recent call last)
<ipython-input-72-9046ec8e66c3> in <module>
      1 # Send message
----> 2 s.send_message(msg)
      3 

/opt/app-root/conda/lib/python3.7/smtplib.py in send_message(self, msg, from_addr, to_addrs, mail_options, rcpt_options)
    965             flatmsg = bytesmsg.getvalue()
    966         return self.sendmail(from_addr, to_addrs, flatmsg, mail_options,
--> 967                              rcpt_options)
    968 
    969     def close(self):

/opt/app-root/conda/lib/python3.7/smtplib.py in sendmail(self, from_addr, to_addrs, msg, mail_options, rcpt_options)
    865             else:
    866                 self._rset()
--> 867             raise SMTPSenderRefused(code, resp, from_addr)
    868         senderrs = {}
    869         if isinstance(to_addrs, str):

SMTPSenderRefused: (552, b'size limit exceeded',
得到20971520,大约21mb

因此,我的问题是:

如何在发送前获得整个电子邮件的确切大小(附件+包装等)

是否有办法尽可能限制电子邮件的大小,以便为文件留出更多空间

我尝试直接通过Outlook发送相同的文件和电子邮件正文,但没有收到任何问题。这只有在我尝试使用Python时才会发生

以下是我的电子邮件发件人功能:

def emailer():
# instance of MIMEMultipart
msg = MIMEMultipart()

# Text message to be sent in the body of the mail
text = """\
Report delivery #12345

"""
# attach the message body with the msg instance
msg.attach(MIMEText(text, 'plain'))

# File to be sent
data_out = !pwd
db_file = 'TEST_2020-10-20.csv.gz'
fileAbsolutePath = data_out[0]+'/'+db_file
attachment = open(fileAbsolutePath, "rb")

# instance of MIMEBase and named as p
p = MIMEBase('application', 'octet-stream')

# To change the payload into encoded form
p.set_payload((attachment).read())

# encode into base64
encoders.encode_base64(p)

p.add_header('Content-Disposition', "attachment; filename= %s" % filename)

# Attach the file to be emailed
msg.attach(p)

msg['Subject'] = f'Report #12345'
msg['From'] = "email@company.com"
msg['To'] = "email@company.com"

# Connect to the SMTP server.
s = smtplib.SMTP('mailhub.company.com', 25)

# start TLS for security
s.starttls()

# Send message
s.send_message(msg)

# terminating the session
s.quit()
从:

如果帖子中的错误消息正确,则表明发件人地址过长。根据您发布的代码,它似乎不是,因此请尝试/except并打印
sender
,以查看smtplib认为发件人是什么:

try:
    s = smtplib.SMTP('mailhub.company.com', 25)
    ...
except smtplib.SMTPSenderRefused as exc:
    print('error sending:', exc.sender)
    raise

另外,请具体说明错误发生的位置,我无法判断它是在
SMTP()
函数中,还是在
starttls()
函数中,还是在
send\u message()
函数中。

您是否尝试从桌面运行此功能?顺便说一句,我无法想象这与kubernetes有任何关系,所以我删除了标签。我必须从kubernetes运行它,才能拥有向我的公司mailhub发送电子邮件的权限。它不允许我从本地计算机访问服务器。我提到了库伯内特斯,因为这个问题似乎正在讨论中。Kubernetes不是管理此类配置的地方吗?不是。Kubernetes在该级别不起作用。如果您没有将
p
附加到
msg
,或者如果您没有调用
p.set\u payload()
,您能否检查
send\u message()
是否成功?另外,请尝试我在答案中发布的代码,并使用更新扩展您的答案:。。。
exception smtplib.SMTPSenderRefused
    Sender address refused. In addition to the attributes set by on all SMTPResponseException exceptions, this sets ‘sender’ to the string that the SMTP server refused.
try:
    s = smtplib.SMTP('mailhub.company.com', 25)
    ...
except smtplib.SMTPSenderRefused as exc:
    print('error sending:', exc.sender)
    raise