电子邮件上的垫子文件空白?
我使用以下功能创建带有附件的电子邮件。
def createEmaiLWithAttachments_Filepaths(self,send_to_emails, subject_text , main_message_text , attachment_filepaths, msgID=None, inReplyTo=None, html=False):
try:
fromEmail = self.from_email_total
if (type(main_message_text) == list) or (type(main_message_text) == tuple):
total_text = ""
for line in main_message_text:
if type(line) == str:
total_text = total_text + line + "\n"
main_message_text = total_text
mimeMessage = MIMEMultipart()
if type(send_to_emails) == list:
mimeMessage['to'] = ", ".join(send_to_emails)
else:
mimeMessage['to'] = send_to_emails
mimeMessage['from'] = fromEmail
mimeMessage['subject'] = subject_text
if inReplyTo != None:
mimeMessage["In-Reply-To"] = inReplyTo
mimeMessage["References"] = inReplyTo
if msgID != None:
mimeMessage['Message-ID'] = msgID
if html:
msg= MIMEText(main_message_text, 'html')
else:
msg= MIMEText(main_message_text, "plain")
mimeMessage.attach(msg)
attachments = list()
for attachment_filepath in attachment_filepaths:
attachment_filepath = fr"{attachment_filepath}"
with open(attachment_filepath, 'rb') as FID:
file_instance_bytes = FID.read()
file_name = Path(attachment_filepath).name
to_attach = (file_name,file_instance_bytes)
attachments.append(to_attach)
totalSizeOf = 0
MAX_BYTES = 50000000
for attachment in attachments:
attachment_name = attachment[0]
attachment_instance = attachment[1]
attachmentSizeBytes = sys.getsizeof(attachment_instance)
if totalSizeOf + attachmentSizeBytes > MAX_BYTES:
self.HifiGmailAPILogger.error(f"The attachment ({attachment_name}) not attached due to the attachment causing the email to exceed maximum limit ({MAX_BYTES}) bytes")
continue
else:
totalSizeOf = totalSizeOf + attachmentSizeBytes
content_type, encoding = mimetypes.guess_type(attachment_name)
if content_type is None or encoding is not None:
content_type = 'application/octet-stream'
main_type, sub_type = content_type.split('/', 1)
if main_type == 'text':
try:
msg = MIMEText(attachment_instance, _subtype=sub_type)
except:
attachment_instance = attachment_instance.decode("utf-8")
msg = MIMEText(attachment_instance, _subtype=sub_type)
elif main_type == 'image':
msg = MIMEImage(attachment_instance, _subtype=sub_type)
elif main_type == 'audio':
msg = MIMEAudio(attachment_instance, _subtype=sub_type)
else:
msg = MIMEBase(main_type, sub_type)
msg.set_payload(attachment_instance)
msg.add_header('Content-Disposition', 'attachment', filename=attachment_name)
mimeMessage.attach(msg)
raw_string = base64.urlsafe_b64encode(mimeMessage.as_string().encode()).decode()
theMessage = {'raw': raw_string}
return theMessage
except:
self.GmailAPILogger.error("An error occurred in createEmaiLWithAttachments")
tb = traceback.format_exc()
self.GmailAPILogger.exception(tb)
return False
它通常有效。但是,我遇到的问题是附加.mat
文件。为了澄清,我可以使用此功能将.mat
文件附加。但是,.mat
文件无效。有时,由于标题中的错误,无法读取文件。其他时候,我可以加载文件,但不会将任何变量加载到工作区中。相反,即使我可以看到.mat
文件就像217kb,但工作空间仍然空。
我还试图补充:
if '.mat' in file_name:
file_instance_bytes = scipy.io.loadmat(attachment_filepath)
else:
with open(attachment_filepath, 'rb') as FID:
file_instance_bytes = FID.read()
但是,这会导致几个错误:
raw_string = base64.urlsafe_b64encode(mimeMessage.as_string().encode()).decode()
我该怎么办?
CRC SHA256在原始文件和下载文件之间有所不同。
I create an email with attachments using the following function.
def createEmaiLWithAttachments_Filepaths(self,send_to_emails, subject_text , main_message_text , attachment_filepaths, msgID=None, inReplyTo=None, html=False):
try:
fromEmail = self.from_email_total
if (type(main_message_text) == list) or (type(main_message_text) == tuple):
total_text = ""
for line in main_message_text:
if type(line) == str:
total_text = total_text + line + "\n"
main_message_text = total_text
mimeMessage = MIMEMultipart()
if type(send_to_emails) == list:
mimeMessage['to'] = ", ".join(send_to_emails)
else:
mimeMessage['to'] = send_to_emails
mimeMessage['from'] = fromEmail
mimeMessage['subject'] = subject_text
if inReplyTo != None:
mimeMessage["In-Reply-To"] = inReplyTo
mimeMessage["References"] = inReplyTo
if msgID != None:
mimeMessage['Message-ID'] = msgID
if html:
msg= MIMEText(main_message_text, 'html')
else:
msg= MIMEText(main_message_text, "plain")
mimeMessage.attach(msg)
attachments = list()
for attachment_filepath in attachment_filepaths:
attachment_filepath = fr"{attachment_filepath}"
with open(attachment_filepath, 'rb') as FID:
file_instance_bytes = FID.read()
file_name = Path(attachment_filepath).name
to_attach = (file_name,file_instance_bytes)
attachments.append(to_attach)
totalSizeOf = 0
MAX_BYTES = 50000000
for attachment in attachments:
attachment_name = attachment[0]
attachment_instance = attachment[1]
attachmentSizeBytes = sys.getsizeof(attachment_instance)
if totalSizeOf + attachmentSizeBytes > MAX_BYTES:
self.HifiGmailAPILogger.error(f"The attachment ({attachment_name}) not attached due to the attachment causing the email to exceed maximum limit ({MAX_BYTES}) bytes")
continue
else:
totalSizeOf = totalSizeOf + attachmentSizeBytes
content_type, encoding = mimetypes.guess_type(attachment_name)
if content_type is None or encoding is not None:
content_type = 'application/octet-stream'
main_type, sub_type = content_type.split('/', 1)
if main_type == 'text':
try:
msg = MIMEText(attachment_instance, _subtype=sub_type)
except:
attachment_instance = attachment_instance.decode("utf-8")
msg = MIMEText(attachment_instance, _subtype=sub_type)
elif main_type == 'image':
msg = MIMEImage(attachment_instance, _subtype=sub_type)
elif main_type == 'audio':
msg = MIMEAudio(attachment_instance, _subtype=sub_type)
else:
msg = MIMEBase(main_type, sub_type)
msg.set_payload(attachment_instance)
msg.add_header('Content-Disposition', 'attachment', filename=attachment_name)
mimeMessage.attach(msg)
raw_string = base64.urlsafe_b64encode(mimeMessage.as_string().encode()).decode()
theMessage = {'raw': raw_string}
return theMessage
except:
self.GmailAPILogger.error("An error occurred in createEmaiLWithAttachments")
tb = traceback.format_exc()
self.GmailAPILogger.exception(tb)
return False
It generally works. However, the problem I have been encountering is attaching .mat
files. To clarify, I can attach the .mat
files using this function and they arrive. However, the .mat
file is invalid. Sometimes, the file cannot be read due to errors in the header. Other times, I can load the file, but it doesn't load any variables into the workspace. Rather, the workspace remains empty, even though I can see the .mat
file is like 217KB.
I also tried to add:
if '.mat' in file_name:
file_instance_bytes = scipy.io.loadmat(attachment_filepath)
else:
with open(attachment_filepath, 'rb') as FID:
file_instance_bytes = FID.read()
However, that causes several errors in the part:
raw_string = base64.urlsafe_b64encode(mimeMessage.as_string().encode()).decode()
What can I do?
The CRC SHA256 is different between the original, attached file and the downloaded file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我必须添加以下内容
,其中
msg
是附件。我在每个附件上运行该功能。现在发送所有附件,包括
.mat
文件。I had to add the following
where
msg
is the attachment. I run that function on each attachment.Sends all attachments just fine now, including
.mat
files.