UNPKG

flexbiz-server

Version:

Flexible Server

15 lines (14 loc) 6.35 kB
const {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"),v8=require("v8"),{postToParent,onParentMessage}=require("./worker-utils"),sendHtml=async function($google_credentials_message$$,$fn$$){if($google_credentials_message$$.configs&&_.isString($google_credentials_message$$.configs))try{$google_credentials_message$$.configs= JSON.parse($google_credentials_message$$.configs)}catch($e$$){delete $google_credentials_message$$.configs,console.error("[sendHtml] l\u1ed7i parse configs:",$e$$)}const $configs$$=$google_credentials_message$$.configs||require("../configs"),$options$$=$google_credentials_message$$.options||{};let $transporter$$,$from$$,$sender$$;if($options$$.account)$google_credentials_message$$=$options$$.account.smtp.host.split("."),$google_credentials_message$$.length>1&&($sender$$=wellknown($google_credentials_message$$[1])), $sender$$||={host:$options$$.account.smtp.host,port:$options$$.account.smtp.port},$sender$$.auth={user:$options$$.account.username,pass:$options$$.account.password},$google_credentials_message$$=smtpTransport($sender$$),$transporter$$=nodemailer.createTransport($google_credentials_message$$),$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:$options$$.app_info.email_secure===void 0?!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$$=$sender$$.google_credentials;let $credentials$$;try{$credentials$$=typeof $google_credentials_message$$=="string"?JSON.parse($google_credentials_message$$):$google_credentials_message$$,$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$$,Logger.error("send email by gmail",$err_error$$,$msg$$),$callback$$($err_error$$)):(Logger.info("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$$&&Logger.error("[sendEmail]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$$&&Logger.error("Error send email",$options$$.to,$error$$,$info$$,$sender$$)}),$fn$$())})}; onParentMessage($msg$$=>{if($msg$$){if(_.isString($msg$$))try{$msg$$=JSON.parse($msg$$)}catch($e$$){Logger.error($e$$);postToParent({error:"msg is not valid"});return}sendHtml($msg$$,function($e$$,$rs$$){postToParent({error:$e$$,data:$rs$$,id_task:$msg$$.id_task})})}else postToParent({error:"msg is not valid"})}); workerData&&(()=>{let $msg$$=workerData;if(Buffer.isBuffer(workerData)||workerData instanceof Uint8Array)try{$msg$$=v8.deserialize(workerData)}catch($e$$){return Logger.error("[Worker] L\u1ed7i deserialize task:",$e$$),postToParent({error:"Deserialization failed"})}if(_.isString($msg$$))try{$msg$$=JSON.parse($msg$$)}catch($e$$){Logger.error($e$$);postToParent({error:"msg is not valid"});return}$msg$$.filePath&&sendHtml($msg$$,function($e$$,$rs$$){postToParent({error:$e$$,data:$rs$$,id_task:$msg$$.id_task})})})();