Strapi Email Provider attachment

System Information
  • Strapi Version: v3.4.1
  • Operating System: Windows 10
  • Database: SQLite
  • Node Version: 12.18.4
  • NPM Version: 6.14.6
  • Yarn Version: 1.22.5

Hello Folks, So I successfully setup the nodemailer provider for strapi and everything is working completely fine I can send an email based on a create request. Now the question I have is, how do I send attachments on email? I have AWS S3 configured to store file and I want to send files that are coming from form-data to the email. I tried the following syntax but the files aren’t getting attached properly.

async create(ctx) {
    let entity;
    if (ctx.is('multipart')) {
        const { data, files } = parseMultipartData(ctx);
        
        const file = files.upload;
        const fileName = file.name;
        const filePath = file.path;
        console.log(filePath);
        const attachment = fs.readFileSync(filePath).toString('base64');
        const res = await strapi.plugins['email'].services.email.send({
            to: 'johndoeunordinary5@gmail.com',
            subject: 'Hello world',
            text: 'Hello world',
            html: `<h4>Hello world</h4>`,
            attachments: [{
                filename: fileName,
                content: attachment
              }]
          });
        entity = await strapi.api['test-mail'].services['test-mail'].create(data, { files });
    }

    return sanitizeEntity(entity, { model: strapi.query('test-mail').model });
}

1 Like

Hello Rinku,
I’m probably a little late, in case any one is facing a similar issue. You’ll need to check the documentation for underline npm module used by your strapi email provider.
In my case, I was using provider-email-mailgun which uses the mailgun-js (package has been deprecated)
As their documentation states for attachment
https://www.npmjs.com/package/mailgun-js#attachments
I’ve used the following options in order to send a single attachement

const {
      files: cv
    } = ctx.request.files;
...
await strapi.plugins['email'].services.email.send({
      to: email,
      from: 'hireme@noqta.tn',
      subject: subject,
      text: message,
      html: message,
      attachment: cv.path,
    });

Please note its attachment without an ‘s’. In case you are looking to send multiple files, I believe it would be better to use the module directly as it looks the strapi mailgun mail provider does handle the use case for now.