Forum Discussion

amaaiia's avatar
amaaiia
Icon for Skilled Sharer rankSkilled Sharer
1 year ago
Solved

How to send an email with an attached file from notebook

Hi, I have a notebook with the code to send an email. The email can be send with an attached file. My code is able to send an attached file located in Files/emails path of the default lakehouse on ...
  • nilendraFabric's avatar
    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