improved mail service
This commit is contained in:
@ -2,44 +2,65 @@ import axios from 'axios';
|
||||
|
||||
// sends an email using the BIC service
|
||||
|
||||
export default (mailAddress, subject, content, f) => { // callback, executed empty or with error
|
||||
if (process.env.NODE_ENV === 'production') {
|
||||
const mailService = JSON.parse(process.env.VCAP_SERVICES).Mail[0];
|
||||
axios({
|
||||
method: 'post',
|
||||
url: mailService.credentials.uri + '/email',
|
||||
auth: {username: mailService.credentials.username, password: mailService.credentials.password},
|
||||
data: {
|
||||
recipients: [{to: mailAddress}],
|
||||
subject: {content: subject},
|
||||
body: {
|
||||
content: content,
|
||||
contentType: "text/html"
|
||||
},
|
||||
from: {
|
||||
eMail: "definma@bosch-iot.com",
|
||||
password: "PlasticsOfFingerprintDigital"
|
||||
}
|
||||
}
|
||||
})
|
||||
.then(() => {
|
||||
f();
|
||||
})
|
||||
.catch((err) => {
|
||||
f(err);
|
||||
});
|
||||
}
|
||||
else if (process.env.NODE_ENV === 'test') {
|
||||
console.info('Sending mail to ' + mailAddress + ': -- ' + subject + ' -- ' + content);
|
||||
f();
|
||||
}
|
||||
else { // dev
|
||||
axios({
|
||||
export default class Mail{
|
||||
|
||||
static readonly address = 'definma@bosch-iot.com';
|
||||
static uri: string;
|
||||
static auth = {username: '', password: ''};
|
||||
static mailPass: string;
|
||||
|
||||
static init() {
|
||||
this.mailPass = Array(64).map(() => Math.floor(Math.random() * 10)).join('');
|
||||
this.uri = JSON.parse(process.env.VCAP_SERVICES).Mail[0].credentials.uri;
|
||||
this.auth.username = JSON.parse(process.env.VCAP_SERVICES).Mail[0].credentials.username;
|
||||
this.auth.password = JSON.parse(process.env.VCAP_SERVICES).Mail[0].credentials.password;
|
||||
axios({ // get registered mail addresses
|
||||
method: 'get',
|
||||
url: 'https://digital-fingerprint-of-plastics-mail-test.apps.de1.bosch-iot-cloud.com/api',
|
||||
data: {
|
||||
url: this.uri + '/management/userDomainMapping',
|
||||
auth: this.auth
|
||||
}).then(res => {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
try {
|
||||
if (res.data.addresses.indexOf(this.address) < 0) { // mail address not registered
|
||||
if (res.data.addresses.length) { // delete wrong registered mail address
|
||||
await axios({
|
||||
method: 'delete',
|
||||
url: this.uri + '/management/mailAddresses/' + res.data.addresses[0],
|
||||
auth: this.auth
|
||||
});
|
||||
}
|
||||
await axios({ // register right mail address
|
||||
method: 'post',
|
||||
url: this.uri + '/management/mailAddresses/' + this.address,
|
||||
auth: this.auth
|
||||
});
|
||||
}
|
||||
resolve();
|
||||
}
|
||||
catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
});
|
||||
}).then(() => {
|
||||
return axios({ // set new mail password
|
||||
method: 'put',
|
||||
url: this.uri + '/management/mailAddresses/' + this.address + '/password/' + this.mailPass,
|
||||
auth: this.auth
|
||||
});
|
||||
}).then(() => { // init done successfully
|
||||
this.send('lukas.veit@bosch.com', 'Mail Service started', new Date().toString());
|
||||
}).catch(err => { // anywhere an error occurred
|
||||
console.error(`Mail init error: ${err.request.method} ${err.request.path}: ${err.response.status}`,
|
||||
err.response.data);
|
||||
});
|
||||
}
|
||||
|
||||
static send (mailAddress, subject, content, f = () => {}) { // callback, executed empty or with error
|
||||
if (process.env.NODE_ENV === 'production') { // only send mails in production
|
||||
axios({
|
||||
method: 'post',
|
||||
url: '/email',
|
||||
url: this.uri + '/email',
|
||||
auth: this.auth,
|
||||
data: {
|
||||
recipients: [{to: mailAddress}],
|
||||
subject: {content: subject},
|
||||
@ -48,17 +69,19 @@ export default (mailAddress, subject, content, f) => { // callback, executed em
|
||||
contentType: "text/html"
|
||||
},
|
||||
from: {
|
||||
eMail: "dfop-test@bosch-iot.com",
|
||||
password: "PlasticsOfFingerprintDigital"
|
||||
eMail: this.address,
|
||||
password: this.mailPass
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
.then(() => {
|
||||
}).then(() => {
|
||||
f();
|
||||
})
|
||||
.catch((err) => {
|
||||
}).catch((err) => {
|
||||
f(err);
|
||||
});
|
||||
}
|
||||
else { // dev dummy replacement
|
||||
console.info('Sending mail to ' + mailAddress + ': -- ' + subject + ' -- ' + content);
|
||||
f();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user