import {Log} from "fme-logger" var L = new Log("emails"); import {TradechatServer,FmeUser} from "../tradechat-server"; import {Db,Collection} from "mongodb"; import {TradechatProduct} from "./products"; import {TradechatMessage} from "./messages"; var ObjectId = require('mongodb').ObjectId; var ejs = require('ejs'); var nodemailer = require("nodemailer"); //var sendmailTransport = require('nodemailer-sendmail-transport'); var mail = nodemailer.createTransport({ sendmail:true, path: '/usr/sbin/sendmail', newline:"unix" }); export class TradechatEmail { _id?:string name = "" from = "" subject = "" description = "" body = "" html = ""; created = new Date(); updated = new Date(); creator:FmeUser = {} as FmeUser; editors:FmeUser[] = []; company = "MRTOPSTEP" create_date = new Date(); update_date = new Date(); last_editor:FmeUser = {} as FmeUser; to?:string; text:string = ""; bcc?:string; subject_obj?:any; body_obj?:any; } export class TradechatEmailController { emailDb:Collection = {} as Collection; ticknumber = 0; constructor(public tc:TradechatServer){ } init = () => { this.emailDb = this.tc.db.collection("emails"); } create = async (doc:TradechatEmail) => { var res = this.emailDb.update({name:doc.name},doc,{upsert:true}) return res } save = async (doc:TradechatEmail) => { var res = this.emailDb.update({name:doc.name},doc,{upsert:true}) return res } delete = async (doc:TradechatEmail) => { var res = this.emailDb.remove({name:doc.name}) return res } sendNotify = async (user:FmeUser,product:TradechatProduct = {} as TradechatProduct) => { var email = await this.getByName(product.email_notify_template); var subject = this.render(email.subject,{U:user,P:product}); var body = this.render(email.body,{U:user,P:product}); L.debug("Sending email to",user.local.email); this.sendEmail( { from:"support@tradechat.me", to:product.email_notify, subject:subject, html:body } ) } sendEmail = async (email:any) => { L.info("Sending Email",email.to) try { await mail.sendMail(email) } catch (err) { L.error("sendMail",err); } return; } sendWelcome = async (user:FmeUser,product:TradechatProduct = {} as TradechatProduct) => { var email = await this.getByName(product.email_welcome); if (!email) { L.error("sendWelcome: product:",product.name,"has no welcome email!"); return; } var subject = this.render(email.subject,{U:user,P:product}); var body = this.render(email.body,{U:user,P:product}); L.debug("Sending email to",user.local.email); this.sendEmail( { from:"support@tradechat.me", to:user.local.email, cc:product.email_notify, subject:subject, html:body } ) } sendByName = async (name:string,user:FmeUser) => { var email = await this.getByName(name); if (!email) { L.error("sendByName: can't find",name); return } var subject = this.render(email.subject,{U:user}); var body = this.render(email.body,{U:user}); this.sendEmail( { from:"support@tradechat.me", to:user.local.email, subject:subject, html:body } ) } sendSupport = ( message:TradechatMessage, subject="Message from Tradechat") => { this.ticknumber++; this.sendEmail({ from: "support@tradechat.me", to: "support@tradechat.me", subject: "ticket-" + this.ticknumber + " " + subject + " " + message.from.name, html: message.body }) } send = async (doc:TradechatEmail) => { if (doc.subject_obj) { doc.subject = this.render(doc.subject,doc.subject_obj); } if (doc.body_obj) { doc.html = this.render(doc.body,doc.body_obj); } if (doc.html == "") doc.html = doc.body; return await this.sendEmail(doc); } render = (html:string,object:any) => { ejs.open = "{{" ejs.close = "}}" var rendered = ejs.render(html,object); ejs.open = "<%" ejs.close = "%>" return rendered } removeById = async (id:string) => { return await this.emailDb.remove({_id: ObjectId(id)}) } get = async (mail:TradechatEmail) => { if (mail._id) { var email = this.getById(mail._id) } else { var email = this.getByName(mail.name); } if (!email) { L.error("Get: no email",mail); return null } return email; } getByName = async (name:string) => { var email = await this.emailDb.findOne({name:name}); if (!email) { L.error("Get: no email by name",name); return null } return email; } getById = async (id:string) => { var email = await this.emailDb.findOne({_id: ObjectId(id)}); if (!email) { L.error("Get: no email by id",id); return null } return email; } }