4

Is it possible to send an attachment with the loopback email datasource?

All I can see in the source documentation are these fields:

  • @property {String} to Email addressee. Required.
  • @property {String} from Email sender address. Required.
  • @property {String} subject Email subject string. Required.
  • @property {String} text Text body of email.
  • @property {String} html HTML body of email.
Farid Nouri Neshat
  • 29,438
  • 6
  • 74
  • 115
dagda1
  • 26,856
  • 59
  • 237
  • 450
  • I believe this question should be left open, as it's not a duplicate in this context, it's clear what the author is asking, it is on topic and not too broad or opinion based. – Farid Nouri Neshat Jan 13 '17 at 04:21
  • people just automatically mark questions down now. it's got ridiculous – dagda1 Jan 13 '17 at 05:30

1 Answers1

9

From the docs:

Nodemailer: Where to Find Documentation

The email connector is essentially a LoopBack-integrated interface to the nodemailer library. This page gives a usage example; for full documentation of configuration options, refer to the nodemailer documention.

Here's where nodemailer have documented attachments.

Here's an example of different types of attachments in the context of loopback:

app.models.Email.send({
  to: 'example@example.com',
  from: 'no-reply@example.com',
  subject: 'Email Subject',
  html: '<b>Hello</b>',
  attachments: [
    {   // utf-8 string as an attachment
      filename: 'text1.txt',
      content: 'hello world!'
    },
    {   // binary buffer as an attachment
      filename: 'text2.txt',
      content: new Buffer('hello world!','utf-8')
    },
    {   // file on disk as an attachment
      filename: 'text3.txt',
      path: '/path/to/file.txt' // stream this file
    },
    {   // filename and content type is derived from path
      path: '/path/to/file.txt'
    },
    {   // stream as an attachment
      filename: 'text4.txt',
      content: fs.createReadStream('file.txt')
    },
    {   // define custom content type for the attachment
      filename: 'text.bin',
      content: 'hello world!',
      contentType: 'text/plain'
    },
    {   // use URL as an attachment
      filename: 'license.txt',
      path: 'https://raw.github.com/nodemailer/nodemailer/master/LICENSE'
    },
    {   // encoded string as an attachment
      filename: 'text1.txt',
      content: 'aGVsbG8gd29ybGQh',
      encoding: 'base64'
    },
    {   // data uri as an attachment
      path: 'data:text/plain;base64,aGVsbG8gd29ybGQ='
    },
    {
      // use pregenerated MIME node
      raw: 'Content-Type: text/plain\r\n' +
        'Content-Disposition: attachment;\r\n' +
        '\r\n' +
        'Hello world!'
    }
  ],
}, err => {
  if (err) {
    throw err;
  }
});
Farid Nouri Neshat
  • 29,438
  • 6
  • 74
  • 115
  • Is there any full sample code for mail sending, because even i am trying to send an email after the form submits. – Sam Mar 28 '19 at 11:11
  • Do i need to install the nodemailer seperately for sending emails through loopback – Sam Mar 28 '19 at 13:03
  • I believe it's installed by loopback as part of it's email connector. – Farid Nouri Neshat Mar 28 '19 at 14:46
  • I am also trying to send an email with attachment but its not working, data is one module and the attachment is in another module am trying combine and send it through email. https://stackoverflow.com/questions/55451895/loopback-send-email-with-attachment-from-another-module – Sam Apr 01 '19 at 10:29