Send an email with the attachment uploaded by the ODA user!

Often we get the below question:

How can we add the ODA-uploaded file as an attachment to an email?

an ODA developer

This can easily be done in 2 parts.

Get the attachment

Whenever a user uploads a file to the conversation (whether is a picture a pdf or any other type), that file is internally stored In the ODA attachment server.

In order to fetch that file we need a custom component where we run the below code:

let attachment = context.attachment();
let fileurl = attachment != null? attachment.url : null;

The variable fileurl will hold an URL that points to the attachment

Send the email with the attachment

Some time ago I wrote a post on how to Send emails with the OCI Email Service and Node.js , that can be the basis for sending an email – obviously it needs to be converted into a custom component.

Nodemailer makes it super easy to send an attachment with an URL – no need to convert to a base64 (although that is also possible).

let info = await transporter.sendMail({
      from: 'noreply@notification.eu-amsterdam-1.oci.oraclecloud.com', 
      to: "youremail@mail.com", 
      subject: "Tech Trantor",
      html: "This is the Tech Trantor oci email service with an attachment! ", 
 attachments: [
        {   // utf-8 string as an attachment
            filename: 'surf.jpg',
            path: 'https://cdn.pixabay.com/photo/2019/04/22/04/32/blue-4145659_960_720.jpg'
        }
      ]    });

You would need to replace the path url with the value from fileurl, and also update the filename.

When I ran the above code I get an email with the surg.jpg attached!