I see that loopback documents to use email connector at email connector My email datasource in datasources.js as below :
"emailDataSource": {
"name": "emailDataSource",
"connector": "mail",
"transports":[{
"type":"SMTP",
"host": "stmp.gmail.com",
"secure":true,
"port":50,
"auth":{
"user":"test@gmail.com",
"pass":"pass"
}
}]}
And my config in model-config.json
"Email":{
"dataSource":"emailDataSource"}
And here is my test on model js file by using remote method:
MyModel.orderproduct = function(id, email, cb){
MyModel.app.models.Email.send({
to: email,
from: 'test@gmail.com',
subject: 'test subject',
text : 'my text',
html : 'my <em> html </em>'
}, function(err, mail){
console.log('email sent!' + mail);
cb(err);
});
}
MyModel.remoteMethod('orderproduct',{
accepts:[{arg:'id',type : 'string', required: true},
{arg: 'email', type : 'string', required : true}],
http: {path : '/product/:id', verb : 'get',source: 'query'},
return:{arg : 'res', type : 'Object'}
});
I got request was successful status to my remote method. But I got error from mail connector component as :
{"error": {
"statusCode": 500,
"name": "Error",
"message": "connect ETIMEDOUT 125.235.4.59:50",
"errno": "ETIMEDOUT",
"code": "ECONNECTION",
"syscall": "connect",
"address": "125.235.4.59",
"port": 50,
"command": "CONN",
"stack": "Error: connect ETIMEDOUT 125.235.4.59:50\n at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1189:14)"
} }
Someone kindly give some advanced or tutorial to using mail connector of Loopback framework. Thank you~
The problem is in configuration of SMTP
server. Port number should be 465
instead of 50
. Use following configuration
"transports": [{
"type": "SMTP",
"host": "smtp.gmail.com",
"secure": true,
"port": 465,
"auth": {
"user": "name@gmail.com",
"pass": "pass"
}
}]
Ref: https://loopback.io/doc/en/lb3/Email-connector.html#using-gmail