Sending an email with an attached file from a notebook

Angelina
Angelina Registered Posts: 1 ✭✭✭
edited July 16 in Using Dataiku

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

Tagged:

Answers

  • Alexandru
    Alexandru Dataiker, Dataiku DSS Core Designer, Dataiku DSS ML Practitioner, Dataiku DSS Adv Designer, Registered Posts: 1,226 Dataiker
    edited July 17

    @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,

  • Sarthak
    Sarthak Dataiku DSS Core Designer, Dataiku DSS Adv Designer, Registered Posts: 15 ✭✭✭

    This has enabled me to send the email but without attachments. The file in the managed folder is in .xlsx format.

    Could you please advise on this?

  • fbu
    fbu Registered Posts: 2 ✭✭✭✭

    Hello,
    here's the code we used successfully to send .xlsx files.

    FB

    def sending_file_mail(mail_dest, my_title, file):
    s = MessageSender(channel_id='id1', type='mail-scenario', configuration={})

    attachment = {
    "type": "FOLDER_ITEM",
    "params": {
    'folderId': "WgsjIQOl",
    'attachedItemPath': file
    }
    }

    topic = "{}".format(my_title)
    s.send(message="", subject=topic, recipient=mail_dest, attachments=[attachment])

  • Sarthak
    Sarthak Dataiku DSS Core Designer, Dataiku DSS Adv Designer, Registered Posts: 15 ✭✭✭

    Thanks for your response, but I am still unable to get the attachments. I am using version 11 of DSS. Is there something that I might have to try along with this code?

    Also, where are you running this code? In a notebook or a python code step in a scenario?

    Also, in place of 'file' do I need to give only the filename with extension or the complete path of the file storage?

Setup Info
    Tags
      Help me…