Forum Discussion
How to send an email with an attached file from notebook
- 1 year ago
Hello amaaiia
You don’t need a traditional Azure Blob Storage connection string to access files from another lakehouse in Fabric. Each Fabric lakehouse is stored in OneLake, and you can directly mount it to your notebook session using its ABFS path. Then you can open the file from the local mount path and attach it to your email. For example
from notebookutils import mssparkutils
# Provide the workspace and lakehouse IDs of the lakehouse you want to read
workspace_id = "<YOUR_WORKSPACE_ID>"
lakehouse_id = "<YOUR_LAKEHOUSE_ID>"# Construct the ABFS path for the target lakehouse
abfs_path = f"abfss://{workspace_id}@onelake.dfs.fabric.microsoft.com/{lakehouse_id}"# Mount the lakehouse to a local folder in your notebook
mount_point = "/mnt/external_lakehouse"
mssparkutils.fs.mount(abfs_path, mount_point)# Use the mounted path to open the file
local_file_path = f"{mount_point}/Files/path/to/file.xlsx"
attachment = open(local_file_path, "rb")# Your email sending logic
send_email(
subject="File from another lakehouse",
body="See attached",
attachment_content=attachment.read(),
attachment_name="file.xlsx"
)
attachment.close()Please give it a try and let me if this works
https://learn.microsoft.com/en-us/fabric/data-engineering/microsoft-spark-utilities
Thanks
Hello amaaiia
You don’t need a traditional Azure Blob Storage connection string to access files from another lakehouse in Fabric. Each Fabric lakehouse is stored in OneLake, and you can directly mount it to your notebook session using its ABFS path. Then you can open the file from the local mount path and attach it to your email. For example
from notebookutils import mssparkutils
# Provide the workspace and lakehouse IDs of the lakehouse you want to read
workspace_id = "<YOUR_WORKSPACE_ID>"
lakehouse_id = "<YOUR_LAKEHOUSE_ID>"
# Construct the ABFS path for the target lakehouse
abfs_path = f"abfss://{workspace_id}@onelake.dfs.fabric.microsoft.com/{lakehouse_id}"
# Mount the lakehouse to a local folder in your notebook
mount_point = "/mnt/external_lakehouse"
mssparkutils.fs.mount(abfs_path, mount_point)
# Use the mounted path to open the file
local_file_path = f"{mount_point}/Files/path/to/file.xlsx"
attachment = open(local_file_path, "rb")
# Your email sending logic
send_email(
subject="File from another lakehouse",
body="See attached",
attachment_content=attachment.read(),
attachment_name="file.xlsx"
)
attachment.close()
Please give it a try and let me if this works
https://learn.microsoft.com/en-us/fabric/data-engineering/microsoft-spark-utilities
Thanks
- amaaiia1 year ago
Skilled Sharer
It works BUT:
1. Instead of:
local_file_path = f"{mount_point}/Files/path/to/file.xlsx" attachment = open(local_file_path, "rb")You need to read the absolute path, and the notebook_id should be added. That is, when you mount the lakehouse to /mnt/external_lakehouse, the truth is that is being mounted to:
/synfs/notebook/{notebook_id}/mnt/external_lakehouse- nilendraFabric1 year ago
Super User
Great insights amaaiia!