环回电子邮件连接器不发送电子邮件
Loopback Email Connector not sending emails
我有一个带 REST 的环回应用程序 API。
我想制作一个发送电子邮件的 REST API Enpoint /Email/sendEmail。我做了这个教程:https://loopback.io/doc/en/lb3/Email-connector.html,但它对我不起作用。
当我打开 https://localhost:3000/explorer 时,我可以看到 API 端点并且我可以按下按钮 "Try it out"。但它只是永远加载,过了一会儿我在控制台中收到超时错误。
文件: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"
}
}]
}
}
文件:模型-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"}
}
}
邮件正在运行。这是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'
}
}
);
};
我有一个带 REST 的环回应用程序 API。
我想制作一个发送电子邮件的 REST API Enpoint /Email/sendEmail。我做了这个教程:https://loopback.io/doc/en/lb3/Email-connector.html,但它对我不起作用。
当我打开 https://localhost:3000/explorer 时,我可以看到 API 端点并且我可以按下按钮 "Try it out"。但它只是永远加载,过了一会儿我在控制台中收到超时错误。
文件: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"
}
}]
}
}
文件:模型-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"}
}
}
邮件正在运行。这是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'
}
}
);
};