0

I am currently reading file from azure blob storage and sending email. I would like to add password protection to the attachment. below is my current code which is sending an email with the attachment

import base64
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import (Mail, Attachment, FileContent, FileName, FileType)
from azure.storage.blob import BlockBlobService

# mail details
to = To_address
from_email = From_Address
subject = Subject
content = '''
Test Content'''


# create mail object
message = Mail(
from_email = from_email,
to_emails = to,
subject = subject,
html_content = content
)

# read the content from azure blob storage
blob_service = BlockBlobService(account_name=Blob_Storage_Account, account_key=account_key)
## read the content inside blob
# read export file
export_file_data = blob_service.get_blob_to_bytes(Blob_Container, export_file_name)
export_file_mail_content = export_file_data.content


# create export file attachement
export_file_encoded = base64.b64encode(export_file_mail_content).decode()
export_file_attachment = Attachment()
export_file_attachment.file_content = FileContent(export_file_encoded)
export_file_attachment.file_type = FileType('application/txt')
export_file_attachment.file_name = FileName(export_file_name.split('/')[-1])
message.add_attachment(export_file_attachment)


# send mail with above attachment
try:
mail = SendGridAPIClient(send_grid_api_key)
response = mail.send(message)
except Exception as e:
print(str(e))

any possibilities here to add password to the file which is located in Azure storage before sending email.

I tried with pyminizip but the same is not supporting to my version 3.6. Is there any suggestion apart from open source module?

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • Is your ultimate question "How do I create a password protected zip file"? – JonSG Jun 15 '21 at 14:56
  • @JonSG yes you are right! – Shanmugam Natarajan Jun 15 '21 at 15:16
  • 1
    look through these - https://stackoverflow.com/search?q=password+protected+zip+file+python – Life is complex Jun 15 '21 at 15:57
  • 1
    Does this answer your question? [Create password protected zip file Python](https://stackoverflow.com/questions/47547590/create-password-protected-zip-file-python) – JonSG Jun 15 '21 at 16:29
  • Yes it will be helpful. Let me try something and get back to you. Thank you so much for your help – Shanmugam Natarajan Jun 15 '21 at 19:10
  • `code` #!/usr/bin/env python2.6 import subprocess, random, os file = "test.txt" s = "abcdefghijklmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()?" zipPass = "".join(random.sample(s,12)) print(zipPass) devnull = open(os.devnull, 'w') rc = subprocess.call("zip -P "+zipPass+" "+file+".zip "+file, shell=True, stdout=devnull, stderr=devnull) `code` I have used the above code,bu i am not sure where the zip file will get store? can someone guide me please. – Shanmugam Natarajan Jun 16 '21 at 04:31

0 Answers0