|
| 1 | +"use strict"; |
| 2 | +const config = require("../../server/config.local"); |
| 3 | +const superagent = require("superagent"); |
| 4 | + |
| 5 | +class M365Authenticate { |
| 6 | + constructor(config){ |
| 7 | + this.tenantId = config.auth.tenantId || ""; |
| 8 | + this.clientId = config.auth.clientId || ""; |
| 9 | + this.clientSecret = config.auth.clientSecret || ""; |
| 10 | + this.aadEndpoint = config.aadEndpoint || ""; |
| 11 | + this.graphEndpoint = config.graphEndpoint || ""; |
| 12 | + } |
| 13 | + |
| 14 | + async getAuthToken() { |
| 15 | + const formData = { |
| 16 | + "grant_type": "client_credentials", |
| 17 | + scope: `${this.graphEndpoint}/.default`, |
| 18 | + "client_id": this.clientId, |
| 19 | + "client_secret": this.clientSecret, |
| 20 | + }; |
| 21 | + |
| 22 | + const tokenObject = await superagent.post( |
| 23 | + `${this.aadEndpoint}/${this.tenantId}/oauth2/v2.0/token`) |
| 24 | + .set({ "Content-Type": "application/x-www-form-urlencoded" }) |
| 25 | + .send(formData); |
| 26 | + return tokenObject.body.access_token; |
| 27 | + } |
| 28 | + |
| 29 | +} |
| 30 | + |
| 31 | +class M365Email { |
| 32 | + constructor(authClass) { |
| 33 | + this.authClass = authClass; |
| 34 | + this.graphEndpoint = authClass.graphEndpoint; |
| 35 | + } |
| 36 | + |
| 37 | + createRecipients(rcpts) { |
| 38 | + return rcpts.filter(r => r).map((rcpt) => { |
| 39 | + return { |
| 40 | + emailAddress: { |
| 41 | + address: rcpt, |
| 42 | + }, |
| 43 | + }; |
| 44 | + }); |
| 45 | + } |
| 46 | + |
| 47 | + createEmailAsJson(rcpts, body, from, replyTo, subject) { |
| 48 | + let messageAsJson = { |
| 49 | + message: { |
| 50 | + subject: subject, |
| 51 | + from: { |
| 52 | + emailAddress: { address: from } |
| 53 | + }, |
| 54 | + replyTo: [{ |
| 55 | + emailAddress: { address: replyTo } |
| 56 | + }], |
| 57 | + body: { |
| 58 | + contentType: "HTML", |
| 59 | + content: body, |
| 60 | + }, |
| 61 | + }, |
| 62 | + }; |
| 63 | + |
| 64 | + messageAsJson = this.addRecipients(messageAsJson, rcpts); |
| 65 | + return messageAsJson; |
| 66 | + } |
| 67 | + |
| 68 | + async sendNotification(from, message){ |
| 69 | + const accessToken = await this.authClass.getAuthToken(); |
| 70 | + try { |
| 71 | + const response = await superagent.post( |
| 72 | + `${this.graphEndpoint}/v1.0/users/${from}/sendMail`) |
| 73 | + .set({ Authorization: `Bearer ${accessToken}` }) |
| 74 | + .set({ "Content-Type": "application/json" }) |
| 75 | + .send(message); |
| 76 | + |
| 77 | + console.log("sendNotification status", response.statusText); |
| 78 | + } catch (error) { |
| 79 | + console.log(error); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + addRecipients(messageBody, rcpts = {}) { |
| 84 | + const cloned = Object.assign({}, messageBody); |
| 85 | + |
| 86 | + Object.keys(rcpts).forEach((element) => { |
| 87 | + const recipients = this.createRecipients(rcpts[element]); |
| 88 | + if (recipients.length > 0) { |
| 89 | + cloned.message[element + "Recipients"] = recipients; |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + return cloned; |
| 94 | + } |
| 95 | + |
| 96 | +} |
| 97 | + |
| 98 | +exports.M365Email = M365Email; |
| 99 | + |
| 100 | +exports.sendEmailM365 = async(to, cc, subjectText, mailText, e, next, html = null) => { |
| 101 | + try { |
| 102 | + const client = new M365Authenticate(config.smtpSettings); |
| 103 | + const emailSender = new M365Email(client); |
| 104 | + const messageCofig = config.smtpMessage; |
| 105 | + const message = emailSender.createEmailAsJson( |
| 106 | + { to: to.split(","), cc: cc ?cc.split(","): [] }, |
| 107 | + html, |
| 108 | + messageCofig.from, |
| 109 | + messageCofig.replyTo, |
| 110 | + messageCofig.subject |
| 111 | + ); |
| 112 | + emailSender.sendNotification(messageCofig.from, message); |
| 113 | + } catch (err) { |
| 114 | + console.log(err); |
| 115 | + } |
| 116 | + return next && next(e); |
| 117 | +}; |
0 commit comments