import { Service } from "../lib/service" import * as Leaf from "leaf-ts" import * as ServiceSpec from "../spec/service" declare const require: any const sgMail = require("@sendgrid/mail") export class MailService extends ServiceSpec.MailService { readonly name = "ThirdPartyMailService" initialize() { console.error("========== Warnning: MailService is deprecated ========") console.error("Please use @root-ts/mail instead") } send(option: { type?: "sendgrid" from: Type.Root.MailService.EmailAddress to: Type.Root.MailService.EmailAddress mail: Type.Root.MailService.Mail }, callback: Callback) { this.sendBySendgrid(option, callback) } private sendBySendgrid(option: { from: Type.Root.MailService.EmailAddress to: Type.Root.MailService.EmailAddress mail: Type.Root.MailService.Mail }, callback: Callback) { sgMail.setApiKey(this.settings.mail.sendgrid.apiKey) let html = option.mail.content || "" let mail = { to: option.to, from: option.from, subject: option.mail.title, // by default we replace html tags to get fallback text text: option.mail.fallbackText || html.replace(/<.*?>/g, ""), html } sgMail.send(mail, (err) => { callback(err) }) } }