如何使用 python 将加密的 PDF 附加到 gmail 的附件
How to attach Encrypted PDF to gmail's attachment using python
我有这段代码可以将文件附加到 gmail。它适用于其他文件类型但在处理“加密的 pdf”时(但文件可以正常查看或手动附加到 gmail 而无需输入密码)此功能添加的 gmail 中的 'pdf' 附件需要密码才能看法。有谁知道如何解决?我没有该文件的密码,因为它只是我老板想发送给客户以供参考的密码。
def create_message_with_attachment():
message = MIMEMultipart()
message = MIMEText(message_data, "plain")
message.attach(message )
message['to'] = "test@gmail.com"
message['cc'] = "cc@gmail.com"
message['subject'] = "testing sub"
file= "test.pdf" # this file is encrypted, can view and print and edit the text box
just_fun = True
#BEGIN attach files to attachment of the email
id= 1 #just to be used in cid
if just_fun:
content_type, encoding = mimetypes.guess_type(file)
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':
fp = open(file, 'rb')
msg = MIMEText(fp.read().decode("utf-8"), _subtype=sub_type)
fp.close()
elif main_type == 'image':
fp = open(file, 'rb')
msg = MIMEImage(fp.read(), _subtype=sub_type)
fp.close()
elif main_type == 'audio':
fp = open(file, 'rb')
msg = MIMEAudio(fp.read(), _subtype=sub_type)
fp.close()
elif main_type == "pdf":
fp = open(file, 'rb')
msg = MIMEApplication(fp.read(), _subtype = sub_type)
else:
fp = open(file, 'rb')
msg = MIMEBase(main_type, sub_type)
msg.set_payload(fp.read())
fp.close()
filename = os.path.basename(file)
msg.add_header('Content-Disposition', 'attachment', filename=filename)
message.attach(msg)
raw_message = base64.urlsafe_b64encode(message.as_string().encode("utf-8"))
return {'raw': raw_message.decode("utf-8")}
这是上述代码输出的图像示例。已附加并发送加密的 PDF,但打开时会询问密码。
enter image description here
感谢阅读
我从 post 中找到了一个关于附加过程后 pdf 显示为空的解决方案。
所以下面是我自己的问题的答案。 .
def create_message_with_attachment():
message = MIMEMultipart()
message = MIMEText(message_data, "plain")
message.attach(message )
message['to'] = "test@gmail.com"
message['cc'] = "cc@gmail.com"
message['subject'] = "testing sub"
file= "test.pdf" # this file is encrypted, can view and print and edit the text box
just_fun = True
#BEGIN attach files to attachment of the email
id= 1 #just to be used in cid
if just_fun:
content_type, encoding = mimetypes.guess_type(file)
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':
fp = open(file, 'rb')
msg = MIMEText(fp.read().decode("utf-8"), _subtype=sub_type)
fp.close()
elif main_type == 'image':
fp = open(file, 'rb')
msg = MIMEImage(fp.read(), _subtype=sub_type)
fp.close()
elif main_type == 'audio':
fp = open(file, 'rb')
msg = MIMEAudio(fp.read(), _subtype=sub_type)
fp.close()
elif main_type == "pdf":
fp = open(file, 'rb')
msg = MIMEApplication(fp.read(), _subtype = sub_type)
else:
fp = open(file, 'rb')
msg = MIMEBase(main_type, sub_type)
msg.set_payload(fp.read())
fp.close()
filename = os.path.basename(file)
msg.add_header('Content-Disposition', 'attachment', filename=filename)
#need to encode the pdf here before attach
import email.encoders #import this one to encode
email.encoders.encode_base64(msg)
# now can attach the pdf file to the message
message.attach(msg)
raw_message = base64.urlsafe_b64encode(message.as_string().encode("utf-8"))
return {'raw': raw_message.decode("utf-8")}
@Tomek Jurkiewicz 引用的答案
我有这段代码可以将文件附加到 gmail。它适用于其他文件类型但在处理“加密的 pdf”时(但文件可以正常查看或手动附加到 gmail 而无需输入密码)此功能添加的 gmail 中的 'pdf' 附件需要密码才能看法。有谁知道如何解决?我没有该文件的密码,因为它只是我老板想发送给客户以供参考的密码。
def create_message_with_attachment():
message = MIMEMultipart()
message = MIMEText(message_data, "plain")
message.attach(message )
message['to'] = "test@gmail.com"
message['cc'] = "cc@gmail.com"
message['subject'] = "testing sub"
file= "test.pdf" # this file is encrypted, can view and print and edit the text box
just_fun = True
#BEGIN attach files to attachment of the email
id= 1 #just to be used in cid
if just_fun:
content_type, encoding = mimetypes.guess_type(file)
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':
fp = open(file, 'rb')
msg = MIMEText(fp.read().decode("utf-8"), _subtype=sub_type)
fp.close()
elif main_type == 'image':
fp = open(file, 'rb')
msg = MIMEImage(fp.read(), _subtype=sub_type)
fp.close()
elif main_type == 'audio':
fp = open(file, 'rb')
msg = MIMEAudio(fp.read(), _subtype=sub_type)
fp.close()
elif main_type == "pdf":
fp = open(file, 'rb')
msg = MIMEApplication(fp.read(), _subtype = sub_type)
else:
fp = open(file, 'rb')
msg = MIMEBase(main_type, sub_type)
msg.set_payload(fp.read())
fp.close()
filename = os.path.basename(file)
msg.add_header('Content-Disposition', 'attachment', filename=filename)
message.attach(msg)
raw_message = base64.urlsafe_b64encode(message.as_string().encode("utf-8"))
return {'raw': raw_message.decode("utf-8")}
这是上述代码输出的图像示例。已附加并发送加密的 PDF,但打开时会询问密码。 enter image description here
感谢阅读
我从 post 中找到了一个关于附加过程后 pdf 显示为空的解决方案。 所以下面是我自己的问题的答案。 .
def create_message_with_attachment():
message = MIMEMultipart()
message = MIMEText(message_data, "plain")
message.attach(message )
message['to'] = "test@gmail.com"
message['cc'] = "cc@gmail.com"
message['subject'] = "testing sub"
file= "test.pdf" # this file is encrypted, can view and print and edit the text box
just_fun = True
#BEGIN attach files to attachment of the email
id= 1 #just to be used in cid
if just_fun:
content_type, encoding = mimetypes.guess_type(file)
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':
fp = open(file, 'rb')
msg = MIMEText(fp.read().decode("utf-8"), _subtype=sub_type)
fp.close()
elif main_type == 'image':
fp = open(file, 'rb')
msg = MIMEImage(fp.read(), _subtype=sub_type)
fp.close()
elif main_type == 'audio':
fp = open(file, 'rb')
msg = MIMEAudio(fp.read(), _subtype=sub_type)
fp.close()
elif main_type == "pdf":
fp = open(file, 'rb')
msg = MIMEApplication(fp.read(), _subtype = sub_type)
else:
fp = open(file, 'rb')
msg = MIMEBase(main_type, sub_type)
msg.set_payload(fp.read())
fp.close()
filename = os.path.basename(file)
msg.add_header('Content-Disposition', 'attachment', filename=filename)
#need to encode the pdf here before attach
import email.encoders #import this one to encode
email.encoders.encode_base64(msg)
# now can attach the pdf file to the message
message.attach(msg)
raw_message = base64.urlsafe_b64encode(message.as_string().encode("utf-8"))
return {'raw': raw_message.decode("utf-8")}
@Tomek Jurkiewicz 引用的答案