Skip to content

Commit

Permalink
[Feature] Mail with mailgun region support
Browse files Browse the repository at this point in the history
Signed-off-by: AntoineDeChassey <[email protected]>
  • Loading branch information
AntoineDeChassey committed Aug 14, 2018
1 parent 97ebdc8 commit d35c367
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
17 changes: 13 additions & 4 deletions api/common/models/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class user {
});

// Send mail
if (process.env.MAILGUN_API_KEY && process.env.MAILGUN_DOMAIN) {
if (process.env.MAILGUN_API_KEY && process.env.MAILGUN_DOMAIN && process.env.MAILGUN_REGION) {

const verificationToken = generateVerificationToken();
userInstance.updateAttributes({ verificationToken: verificationToken });
Expand All @@ -200,20 +200,29 @@ class user {
const options = {
type: 'email',
to: userInstance.email,
from: 'sigfox-platform@' + process.env.MAILGUN_DOMAIN,
from: 'Sigfox Platform <sigfox-platform@iotageny.sigfox.com>',
subject: 'Welcome to the Sigfox Platform!',
html: html_body,
redirect: '',
user: userInstance
};

loopback.Email.send(options)
const mailgun = require('mailgun-js')({host: 'api.' + process.env.MAILGUN_REGION + '.mailgun.net', apiKey: process.env.MAILGUN_API_KEY, domain: process.env.MAILGUN_DOMAIN});
mailgun.messages().send(options, (error: any, body: any) => {
if (error) {
console.error(error);
} else {
console.log('> verification email sent:', body);
}
});

/*loopback.Email.send(options)
.then((response: any) => {
console.log('> verification email sent:', response);
})
.catch((err: any) => {
console.error(err);
});
});*/

/*userInstance.verify(options, (err: any, response: any, next: Function) => {
if (err) {
Expand Down
7 changes: 4 additions & 3 deletions api/server/boot/02-load-mailgun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ module.exports = (app: any) => {

const mailgunApiKey = process.env.MAILGUN_API_KEY;
const mailgunDomain = process.env.MAILGUN_DOMAIN;
const mailgunRegion = process.env.MAILGUN_REGION;

if (mailgunApiKey && mailgunDomain) {
console.log('Loading mailgun with: ' + mailgunApiKey + ' | ' + mailgunDomain);
if (mailgunApiKey && mailgunDomain && mailgunRegion) {
console.log('Using mailgun with: \n\tAPI key: ' + mailgunApiKey + ' | Domain: ' + mailgunDomain + ' | Region: ' + process.env.MAILGUN_REGION);

const DataSource = require('loopback-datasource-juggler').DataSource;
const dataSource = new DataSource('mailgun', {
connector: 'loopback-connector-mailgun',
apikey: mailgunApiKey,
domain: mailgunDomain
domain: mailgunDomain,
});

app.models.Email.attachTo(dataSource);
Expand Down
2 changes: 1 addition & 1 deletion api/server/datasources.local.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var mongodbUrl = process.env.MONGO_URL || process.env.MONGODB_URI;

if (mongodbUrl) {
console.log('Loading MongoDB with: ' + mongodbUrl);
console.log('Using MongoDB with: ' + mongodbUrl);
module.exports = {
mongodb: {
url: mongodbUrl,
Expand Down

0 comments on commit d35c367

Please sign in to comment.