我有一个带有REST API的环回应用程序。
我想创建一个REST API Enpoint / Email / sendEmail来发送电子邮件。我完成了本教程:https://loopback.io/doc/en/lb3/Email-connector.html,但是它对我不起作用。
当我打开https://localhost:3000/explorer时,可以看到API端点,并且可以按“尝试一下”按钮。但是然后它会永久加载,一段时间后,我在控制台中收到超时错误。
文件: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": "xbit.dany@gmail.com",
"pass": "XXX"
}
}]
}
}
文件:model-config.json
"Email": {
"dataSource": "email",
"public": true
}
文件:email.js
module.exports = function(Email) {
// send an email
Email.sendEmail = function(cb) {
console.log("Sending Email");
Email.app.models.Email.send({
to: 'siemens.dany@gmail.com',
from: 'xbit.dany@gmail.com',
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: {
}
}
);
};
文件: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"}
}
}
答案 0 :(得分:0)
邮件现在可以使用了。这是email.js的代码
module.exports = function(emailDS) {
// send an email
emailDS.sendEmail = function(cb) {
console.log("Sending Email");
emailDS.app.models.Email.send({
to: 'siemens.dany@gmail.com',
from: 'xbit.dany@gmail.com',
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'
}
}
);
};