UNPKG

flexbiz-server

Version:

Flexible Server

14 lines (13 loc) 6.29 kB
'use strict';const {parentPort,workerData}=require("worker_threads");require("../global"); const nodemailer=require("nodemailer"),smtpTransport=require("nodemailer-smtp-transport"),async=require("async"),underscore=require("underscore"),path=require("path"),fs=require("fs"),wellknown=require("nodemailer-wellknown"),Axios=require("axios"),_=require("lodash"),sendHtml=async function($google_credentials_message_smtp_tp$$,$fn$$){if($google_credentials_message_smtp_tp$$.configs&&_.isString($google_credentials_message_smtp_tp$$.configs))try{$google_credentials_message_smtp_tp$$.configs=JSON.parse($google_credentials_message_smtp_tp$$.configs)}catch($e$$){delete $google_credentials_message_smtp_tp$$.configs, console.info($e$$)}const $configs$$=$google_credentials_message_smtp_tp$$.configs||require("../configs"),$options$$=$google_credentials_message_smtp_tp$$.options||{};let $transporter$$,$from$$,$sender$$;if($options$$.account)$google_credentials_message_smtp_tp$$=$options$$.account.smtp.host.split("."),1<$google_credentials_message_smtp_tp$$.length&&($sender$$=wellknown($google_credentials_message_smtp_tp$$[1])),$sender$$||($sender$$={host:$options$$.account.smtp.host,port:$options$$.account.smtp.port}), $sender$$.auth={user:$options$$.account.username,pass:$options$$.account.password},$google_credentials_message_smtp_tp$$=smtpTransport($sender$$),$transporter$$=nodemailer.createTransport($google_credentials_message_smtp_tp$$),$from$$={name:$options$$.account.fullname,address:$options$$.account.username};else if($sender$$=$options$$.app_info?$options$$.app_info.email_host&&$options$$.app_info.email_user&&$options$$.app_info.email_pass?{host:$options$$.app_info.email_host,port:$options$$.app_info.email_port|| 465,secure:void 0===$options$$.app_info.email_secure?!0:$options$$.app_info.email_secure,authMethod:$options$$.app_info.email_authMethod||"LOGIN",auth:{user:$options$$.app_info.email_user,pass:$options$$.app_info.email_pass},name:$options$$.app_info.email_name||$options$$.app_info.name||$options$$.app_info.email_user}:$options$$.app_info.google_credentials?{google_credentials:$options$$.app_info.google_credentials,name:$options$$.app_info.name}:$configs$$.google_credentials?{google_credentials:$configs$$.google_credentials, name:$options$$.app_info.name}:$configs$$.sender:$configs$$.google_credentials?{google_credentials:$configs$$.google_credentials,name:($configs$$.sender||{}).name||$configs$$.company||"SYSTEM"}:$configs$$.sender,$sender$$.url)$transporter$$={sendMail:($msg$$,$callback$$)=>{$msg$$.secretKey=$sender$$.secretKey;$msg$$.message=$msg$$.message||$msg$$.html;$msg$$.name=$sender$$.name;Axios.post($sender$$.url,$msg$$).then($res$$=>{$callback$$(null,$res$$.data)}).catch($error$$=>{$callback$$($error$$.data)})}}; else if($sender$$.google_credentials){$google_credentials_message_smtp_tp$$=$sender$$.google_credentials;let $credentials$$;try{$credentials$$="string"==typeof $google_credentials_message_smtp_tp$$?JSON.parse($google_credentials_message_smtp_tp$$):$google_credentials_message_smtp_tp$$,$from$$={name:$sender$$.name,address:$credentials$$.client_email}}catch($e$$){return $fn$$($e$$)}$transporter$$={sendMail:($msg$$,$callback$$)=>{const {google:$google$$}=require("googleapis");try{const {client_email:$client_email$$, private_key:$private_key$$}=$credentials$$,$auth$$=new $google$$.auth.JWT($client_email$$,null,$private_key$$,["https://www.googleapis.com/auth/gmail.send"]),$gmail$$=$google$$.gmail({version:"v1",auth:$auth$$}),$encodedMessage$$=Buffer.from(`From: "${$msg$$.from.name}" <${$msg$$.from.address}>\n`+`To: ${$msg$$.to.address}\n`+`Subject: ${$msg$$.subject}\n\n`+`${$msg$$.message||$msg$$.html||"test email"}`).toString("base64").replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,"");$gmail$$.users.messages.send({userId:"me", resource:{raw:$encodedMessage$$}},($err_error$$,$res$$)=>{$err_error$$?($err_error$$=(($err_error$$.response||{}).data||{}).error||$err_error$$,console.error("send email by gmail",$err_error$$,$msg$$),$callback$$($err_error$$)):(console.log("Email sent successfully"),$callback$$(null,$res$$))})}catch($e$$){$callback$$((($e$$.response||{}).data||{}).error||$e$$)}}}}else $transporter$$=nodemailer.createTransport(smtpTransport($sender$$)),$from$$={name:$sender$$.name,address:$sender$$.auth.user},$options$$.sender&& ($from$$.name=$options$$.sender);let $attachments$$=[];$options$$.attachments||($options$$.attachments=[]);async.map($options$$.attachments,($att$$,$callback$$)=>{let $file_name$$=$att$$.file.name,$root$$=$configs$$.paths.uploads;$root$$||($root$$=__dirname,$root$$=path.dirname($root$$),$root$$=path.join($root$$,"uploads"));$file_name$$=path.join($root$$,$file_name$$);fs.existsSync($file_name$$)&&$attachments$$.push({path:$file_name$$,filename:$att$$.file.originalname});$callback$$()},()=>{underscore.isArray($options$$.to)? async.map($options$$.to,function($to$$,$callback$$){$to$$.address?($transporter$$.sendMail({from:$from$$,to:$to$$,cc:$options$$.cc,bcc:$options$$.bcc,subject:$options$$.subject,html:$options$$.html,attachments:$attachments$$},function($error$$,$info$$){$error$$&&console.error("Error send email",$to$$,$error$$,$info$$)}),$callback$$()):$callback$$(null)},function($error$$,$t$$){$fn$$($error$$,$t$$)}):($transporter$$.sendMail({from:$from$$,to:$options$$.to,cc:$options$$.cc,bcc:$options$$.bcc,subject:$options$$.subject, html:$options$$.html,attachments:$attachments$$},function($error$$,$info$$){$error$$&&console.error("Error send email",$options$$.to,$error$$,$info$$,$sender$$)}),$fn$$())})};parentPort.on("message",$msg$$=>{if($msg$$){if(_.isString($msg$$))try{$msg$$=JSON.parse($msg$$)}catch($e$$){console.error($e$$);parentPort.postMessage({error:"msg is not valid"});return}sendHtml($msg$$,function($e$$,$rs$$){parentPort.postMessage({error:$e$$,data:$rs$$,id_task:$msg$$.id_task})})}else parentPort.postMessage({error:"msg is not valid"})}); workerData&&(()=>{let $msg$$=workerData;if(_.isString($msg$$))try{$msg$$=JSON.parse($msg$$)}catch($e$$){console.error($e$$);parentPort.postMessage({error:"msg is not valid"});return}$msg$$.filePath&&sendHtml($msg$$,function($e$$,$rs$$){parentPort.postMessage({error:$e$$,data:$rs$$,id_task:$msg$$.id_task})})})();setInterval(()=>{parentPort.postMessage("pong")},5E3);