Archived
2
This repository has been archived on 2023-03-02. You can view files and clone it, but cannot push or open issues or pull requests.

88 lines
2.9 KiB
TypeScript
Raw Normal View History

2020-04-23 17:46:00 +02:00
import axios from 'axios';
2020-05-18 14:47:22 +02:00
// sends an email using the BIC service
2020-04-23 17:46:00 +02:00
2020-08-06 11:25:41 +02:00
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: 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);
2020-04-23 17:46:00 +02:00
}
});
2020-08-06 11:25:41 +02:00
}).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);
});
2020-04-23 17:46:00 +02:00
}
2020-08-06 11:25:41 +02:00
static send (mailAddress, subject, content, f = () => {}) { // callback, executed empty or with error
if (process.env.NODE_ENV === 'production') { // only send mails in production
axios({
2020-04-23 17:46:00 +02:00
method: 'post',
2020-08-06 11:25:41 +02:00
url: this.uri + '/email',
auth: this.auth,
2020-04-23 17:46:00 +02:00
data: {
recipients: [{to: mailAddress}],
subject: {content: subject},
body: {
content: content,
contentType: "text/html"
},
from: {
2020-08-06 11:25:41 +02:00
eMail: this.address,
password: this.mailPass
2020-04-23 17:46:00 +02:00
}
}
2020-08-06 11:25:41 +02:00
}).then(() => {
2020-04-23 17:46:00 +02:00
f();
2020-08-06 11:25:41 +02:00
}).catch((err) => {
2020-04-23 17:46:00 +02:00
f(err);
});
2020-08-06 11:25:41 +02:00
}
else { // dev dummy replacement
console.info('Sending mail to ' + mailAddress + ': -- ' + subject + ' -- ' + content);
f();
}
2020-04-23 17:46:00 +02:00
}
2020-08-06 11:25:41 +02:00
}