Loopback Email Connector not sending emails
Asked Answered
V

1

8

I have a Loopback Application with a REST API.

I want to make a REST API Enpoint /Email/sendEmail which sends an email. I did this tutorial: https://loopback.io/doc/en/lb3/Email-connector.html, but it's not working for me somehow.

When I open https://localhost:3000/explorer , I can see the API Endpoint and I can press the button "Try it out". But then it just loads forever and after a while I get a Timeout error in the console.

File: datasource.json

{
  "db": {
    "host": "localhost",
    "port": 27017,
    "url": "",
    "database": "siemens",
    "password": "",
    "name": "db",
    "user": "",
    "useNewUrlParser": true,
    "connector": "mongodb"
  },
  "email": {
    "name": "email",
    "connector": "mail",
    "transports": [{
      "type": "SMTP",
      "host": "smtp.gmail.com",
      "secure": true,
      "port": 465,
      "auth": {
        "user": "[email protected]",
        "pass": "XXX"
      }
    }]
  }
}

File: model-config.json

"Email": {
    "dataSource": "email",
    "public": true
  }

File: email.js

module.exports = function(Email) {

  // send an email
  Email.sendEmail = function(cb) {
    console.log("Sending Email");
    Email.app.models.Email.send({
      to: '[email protected]',
      from: '[email protected]',
      subject: 'my subject',
      text: 'my text',
      html: 'my <em>html</em>'
    }, function(err, mail) {
      console.log('email sent!');
      cb(err);
    });
  }

  Email.remoteMethod(
    'sendEmail', {
      http: {
        path: '/sendEmail',
        verb: 'get'
      },
      returns: {

      }
    }
  );

};

File: models/email.json

{
    "name": "Email",
    "base": "Model",
    "properties": {
      "to": {"type": "String", "required": true},
      "from": {"type": "String", "required": true},
      "subject": {"type": "String", "required": true},
      "text": {"type": "String"},
      "html": {"type": "String"}
    }
}
Vanhouten answered 23/5, 2019 at 12:50 Comment(3)
can you post the console error?Peterson
Do you get this console log: Sending Email when you invoke the method, or it's not reached at all? Also, you can try to replace Email.app.models.Email.send( with just Email.send(.Synaeresis
Yes when I invoke the method. When I do just "Email.send", I get "Email is not defined"Vanhouten
V
2

The mail is now working. This is the code of email.js

module.exports = function(emailDS) {

  // send an email
  emailDS.sendEmail = function(cb) {
    console.log("Sending Email");

    emailDS.app.models.Email.send({
      to: '[email protected]',
      from: '[email protected]',
      subject: 'my subject',
      html: 'my <em>html</em>'
    }, function(err, mail) {
      console.log("Mail: " + mail);
      console.log("Error: " + err);
      cb(null, mail);
    });
  }

  emailDS.remoteMethod(
    'sendEmail', {
      http: {
        path: '/sendEmail',
        verb: 'get'
      },
      returns: {
        arg: 'Status', type:'string'
      }
    }
  );

};
Vanhouten answered 28/5, 2019 at 9:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.