Loopback-next: Document how to send emails in LB4

Created on 5 Nov 2018  路  16Comments  路  Source: strongloop/loopback-next

Description / Steps to reproduce / Feature proposal

Originated from https://github.com/strongloop/loopback-next/issues/1971

In LoopBack 3, users can use our email connector to send emails.
Ideally, we can use the same connector in LoopBack 4 the same way we use other connectors. However, the email connector is part of the LB3 core, so cannot be used as-is.

We've listed the workaround in this comment: https://github.com/strongloop/loopback-next/issues/1971#issuecomment-435923465.

Acceptance Criteria

  • [ ] Remove email connector option when running lb4 datasource command, since this won't work.
  • [ ] Document the workaround in the LB4 documentation page

cc @bajtos

Docs community-contribution help wanted

Most helpful comment

@djamaile just use nodemailer for now. I implemented it as a service:

import {SentMessageInfo} from 'nodemailer';
import Mail = require('nodemailer/lib/mailer');
import params from '../data/params'

const nodemailer = require("nodemailer");

export class MailerService {
  async sendMail(mailOptions: Mail.Options): Promise<SentMessageInfo> {
    const transporter = nodemailer.createTransport(params.mail_account);
    return await transporter.sendMail(mailOptions);
  }
}

And then you just have to inject the MailerService in your controller and use it like this:

let info = await this.mailerService.sendMail({
      to: foundUser.email,
      subject: "Testmail",
      html: '<p>This is a mail</p>'
    });

All 16 comments

Need docs or example to configure mail in loopback 4

So has anyone a real example on how to send email in lb4?

@djamaile just use nodemailer for now. I implemented it as a service:

import {SentMessageInfo} from 'nodemailer';
import Mail = require('nodemailer/lib/mailer');
import params from '../data/params'

const nodemailer = require("nodemailer");

export class MailerService {
  async sendMail(mailOptions: Mail.Options): Promise<SentMessageInfo> {
    const transporter = nodemailer.createTransport(params.mail_account);
    return await transporter.sendMail(mailOptions);
  }
}

And then you just have to inject the MailerService in your controller and use it like this:

let info = await this.mailerService.sendMail({
      to: foundUser.email,
      subject: "Testmail",
      html: '<p>This is a mail</p>'
    });

@RipkensLar , Can you explain ../data/params?

Thanks @RipkensLar . Its worked like a charm... Thank you

@RipkensLar , Can you explain ../data/params?

see: https://nodemailer.com/smtp/

@dhmlau Available to port LB3 Email connector to LB4.

@mrbatista, that would be great. Thanks.
In case you're not aware already, @bajtos and I had some discussion on this earlier, the notes are captured in https://github.com/strongloop/loopback-next/issues/1971#issuecomment-435923465.

Let me assign this issue to you now. Thanks again.

Down the road, it could be expanded with something like https://www.npmjs.com/package/email-templates for example. An API would most likely be firing out templated messages.

I appreciate that especially with current difficulties this enhancement may not be on the milestones for awhile. However, in the meantime the page here: https://loopback.io/doc/en/lb4/Email-connector.html should be updated to inform readers that it doesn't work. I'd edit the page myself but the link doesn't work and last time I tried adding a line to the docs through a pull request I got stuck in linting hell and fell foul of jenkins and the slightly cumbersome contributor process. I'd like to help, that's why I'm commenting here, but I can't afford an hour going through a process designed for major code contributions.

https://loopback.io/doc/en/lb4/Email-connector.html

@garethhcoleman Does this really not work? Can anyone else confirm?

The doc page I linked references files like /server/model-config.json which my readings suggest is depreciated. The OP stated that a workaround was listed in a github issue - as a new(ish) developer I'm still struggling to get that working. But it is extra difficult when the official docs seem confused about the transition from LB3 to LB4.

@garethhcoleman I see, I scanned the doc and it's a fair criticism. Personally, I just implemented a service like in comment above. For a service implementation, you can look at shopping example app and reverse engineer.

Hopefully someone can find the time to put a working example for emails together so that it can be used as a reference.

Thank you for replying, I am attempting the same myself, thank you for the reference to the shopping example. I would like to be able to contribute, perhaps by updating the tutorial (after I get it working!). I appreciate that writing an API is not a wise choice for first real project! thanks!

@garethhcoleman @dougal83 I have extracted the email connector from LB3.
In the next days publish the connector to npm.

@mrbatista any update on publishing connector on npm.

Was this page helpful?
0 / 5 - 0 ratings