Python Forum

Full Version: Password Protection to the file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi All,

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 email with attachement

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))