Want to Stop Rebuilding "Expensive" Parts of your Flow? Explicit Builds are the Answer!READ MORE

Sending an email with an attached file from a notebook

Angelina
Level 1
Sending an email with an attached file from a notebook

Hi,

 

I'm currently working on a DSS project with a notebook in which I manipulate/modify/update excel files.
At the end of the treatment I would like to send several emails to attest that the program completed the job and that everything went well for each resulting excel file. Thus, each email sent would have its associated excel file in attached piece.

 

I found examples of codes for sending emails with folders or datasets as attached pieces, but not files.

This is the function that I currently use, it only half works because I receive the emails, the title and the content, but not the attached files :

 

def sending_mail(extraction_type, partner, sharepoint_path, file):
    s = MessageSender(channel_id='id1', type='mail-scenario', configuration={})
    attachment = {
                  "type": "FILE",
                  "params": {
                    "exportParams": {
                      "selection": {
                        "samplingMethod": "FULL"
                      },
                      "format": {
                        "type": "csv",
                        "params": {
                          "style": "excel",
                          "charset": "utf8",
                          "separator": ",",
                          "quoteChar": "\"",
                          "escapeChar": "\\",
                          "parseHeaderRow": True
                        }
                      }
                    },
                    "attachedFile": file
                  }
                }
    
    content = "Data of type : {}, concerning the site of {} have been broadcasted on the sharepoint of this Partner : {}".format(extraction_type, partner, sharepoint_path)
    topic = "Broadcast of `{}` to {}".format(extraction_type, partner)
    # print(attachment)
    s.send(message=content, subject=topic, recipient='myemail@something.com', attachments=[attachment])
    print("Mail sent for partner {}".format(partner))

 

I hope I explained well the matter and that my problem is clear enough, let me know if it is not.
Thank you in advance for your help.


Operating system used: Windows

0 Kudos
1 Reply
AlexT
Dataiker
Dataiker

@Angelina ,

Just posting here as well as you have resolved this over another channel. 
To attach files, you can do this if the files are in a managed folder in DSS using "FOLDER_ITEM" type.

 

import dataiku
from dataiku import pandasutils as pdu
from dataiku.core.message_sender import MessageSender

#change email channel id
s = MessageSender(channel_id='my-email-channel', type='mail-scenario', configuration={})
#replace folderID found in URL of managed folder and readme.txt with atactual filename to attach
attachment = {
                "type": "FOLDER_ITEM",
                 "params": {
                            "folderId": "X9SD9y5B", 
                            "attachedItemPath": "readme.txt"
                        } 
                    }
        
s.send(message="something", subject="test", recipient='email@doimain', attachments=[attachment])

 

 

Thanks,