welsonjs/lib/sendmail.js

110 lines
3.7 KiB
JavaScript
Raw Normal View History

2020-06-28 14:22:57 +00:00
//////////////////////////////////////////////////////////////////////////////////
//
// sendmail.js
//
// Sendmail using either CDO or Persits.MailSender
//
/////////////////////////////////////////////////////////////////////////////////
2020-06-28 14:22:57 +00:00
/////////////////////////////////////////////////////////////////////////////////
// Private APIs / Utility functions
/////////////////////////////////////////////////////////////////////////////////
2024-01-27 05:01:57 +00:00
var usePersitsMailSender = false;
var useSSL = false;
2020-06-28 14:22:57 +00:00
/////////////////////////////////////////////////////////////////////////////////
// Send Mail Message
/////////////////////////////////////////////////////////////////////////////////
2024-01-27 05:01:57 +00:00
function sendmail(msg) {
2020-07-03 09:15:23 +00:00
var ok, MAIL;
2020-06-28 14:22:57 +00:00
console.log("SENDMAIL: " + msg.To);
2020-06-28 14:22:57 +00:00
2020-07-03 09:15:23 +00:00
// Which method we use depends on the system. On some versions of
// Persits.MailSender it does not support adding of Message-ID
// so we have to use CDO (which is the preferred option anyway).
if (exports.usePersitsMailSender) {
2020-06-28 14:22:57 +00:00
2020-07-03 09:15:23 +00:00
// Use Persits AspEmail to send mail
try {
MAIL = CreateObject("Persits.MailSender");
2020-07-03 09:15:23 +00:00
} catch (e) {
console.log("ERROR " + e.number + ", " + e.description);
2020-07-03 09:15:23 +00:00
throw e;
}
2020-06-28 14:22:57 +00:00
console.log("USING PERSITS MAIL SENDER");
console.log("MAIL FROM " + msg.From);
console.log("MAIL TO " + msg.To);
console.log("SUBJECT " + msg.Subject);
2020-06-28 14:22:57 +00:00
2020-07-03 09:15:23 +00:00
MAIL.Host = msg.MAILHOST;
2020-06-28 14:22:57 +00:00
MAIL.From = msg.From;
2020-07-03 09:15:23 +00:00
if (msg.Name) MAIL.FromName = msg.Name;
MAIL.AddAddress(msg.To);
MAIL.Subject = msg.Subject;
if (msg.cc) MAIL.AddCC(msg.Cc);
MAIL.IsHTML = msg.IsHTML;
MAIL.Body = msg.Body;
MAIL.addCustomHeader("Reply-To: <" + msg.ReplyTo + ">");
console.log("Reply-To: <" + msg.ReplyTo + ">");
2020-07-03 09:15:23 +00:00
if (msg.id) {
console.log("Message-ID: <" + msg.id + ">");
2020-07-03 09:15:23 +00:00
MAIL.addCustomHeader("Message-ID: <" + msg.id + ">");
}
2020-06-28 14:22:57 +00:00
} else {
2020-07-03 09:15:23 +00:00
// Use CDO objects to send mail. Setup SMTP server details.
2024-01-27 05:01:57 +00:00
var CONF = CreateObject("CDO.Configuration");
2020-07-03 09:15:23 +00:00
CONF.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2;
CONF.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = msg.MAILHOST;
CONF.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = msg.MAILPORT || 25;
CONF.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 0;
2024-01-27 05:01:57 +00:00
CONF.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = (!useSSL ? 0 : 1);
2020-07-03 09:15:23 +00:00
CONF.Fields.Update();
// Create the email message
2024-01-27 05:01:57 +00:00
MAIL = CreateObject("CDO.Message");
2020-07-03 09:15:23 +00:00
MAIL.Configuration = CONF;
CONF = null;
if (msg.Name) {
MAIL.From = '"' + msg.Name + '" <' + msg.From + '>';
} else {
MAIL.From = msg.From;
}
MAIL.To = msg.To;
if (msg.Cc) MAIL.Cc = msg.cc;
MAIL.Subject = msg.Subject;
MAIL.Fields("urn:schemas:mailheader:reply-to") = "<" + msg.ReplyTo + ">";
MAIL.Fields("urn:schemas:mailheader:message-id") = "<" + msg.id + ">";
if (msg.IsHTML) {
MAIL.HTMLBody = msg.Body;
} else {
MAIL.TextBody = msg.Body;
}
MAIL.Fields.Update();
}
try {
console.log("Sending email To " + msg.To + (msg.Cc ? " (Cc " + msg.Cc + ")" : ""));
2020-07-03 09:15:23 +00:00
MAIL.Send();
ok = true;
} catch (e) {
console.log(e.number + "," + e.description);
2020-07-03 09:15:23 +00:00
ok = false;
console.log("failed");
2020-06-28 14:22:57 +00:00
}
2020-07-03 09:15:23 +00:00
MAIL = null;
return ok;
2020-06-28 14:22:57 +00:00
};
2024-01-27 05:01:57 +00:00
exports.sendmail = sendmail;
2020-06-28 14:22:57 +00:00
2024-01-27 05:01:57 +00:00
exports.VERSIONINFO = "Sendmail Library (sendmail.js) version 0.1.1";
exports.global = global;
exports.require = global.require;