You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
114 lines
2.5 KiB
114 lines
2.5 KiB
package sugoi.mail;
|
|
|
|
import tink.core.Future;
|
|
import tink.core.Noise;
|
|
import sugoi.mail.IMailer;
|
|
import smtpmailer.Address;
|
|
|
|
typedef SmtpInfo = {
|
|
host:String,
|
|
port:Int,
|
|
auth:{
|
|
username:String, password:String
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Send emails thru OS command `sendemail`
|
|
* requires: `apt install sendemail libio-socket-ssl-perl libnet-ssleay-perl`
|
|
*/
|
|
class SendEmailMailer implements IMailer {
|
|
var m:SmtpInfo;
|
|
|
|
public function new() {}
|
|
|
|
public function init(?conf:{
|
|
smtp_host:String,
|
|
smtp_port:Int,
|
|
smtp_user:String,
|
|
smtp_pass:String
|
|
}):IMailer {
|
|
var mailer:SendEmailMailer = new SendEmailMailer();
|
|
mailer.m = {
|
|
host: conf.smtp_host,
|
|
port: conf.smtp_port,
|
|
auth: {
|
|
username: conf.smtp_user,
|
|
password: conf.smtp_pass
|
|
}
|
|
};
|
|
return mailer;
|
|
}
|
|
|
|
public function send(e:sugoi.mail.IMail, ?params:Dynamic, ?callback:MailerResult->Void) {
|
|
var to:String;
|
|
var bcc:String;
|
|
|
|
var recipientCounts = e.getRecipients().length;
|
|
if (recipientCounts > 1) {
|
|
to = sugoi.mail.Mail.fromString(e.getSender().name, e.getSender().email);
|
|
bcc = [for (i in e.getRecipients()) i.email].join(",");
|
|
App.log('recipients has been transformed to ${recipientCounts} bcc');
|
|
App.log(bcc);
|
|
} else {
|
|
to = e.getRecipients()[0].email;
|
|
bcc = "";
|
|
}
|
|
|
|
var args = [
|
|
// arguments
|
|
"-s",
|
|
'${this.m.host}:${this.m.port}',
|
|
"-xu",
|
|
${this.m.auth.username},
|
|
"-xp",
|
|
${this.m.auth.password},
|
|
"-o",
|
|
"message-charset=utf-8",
|
|
"-o",
|
|
"message-format=html",
|
|
"-o",
|
|
"message-header=X-Mailer: CagettePei",
|
|
"-f",
|
|
sugoi.mail.Mail.fromString(e.getSender().name, e.getSender().email),
|
|
"-t",
|
|
to,
|
|
"-bcc",
|
|
bcc,
|
|
"-u",
|
|
e.getSubject(),
|
|
];
|
|
|
|
// FIME: deal with replyTo
|
|
if (e.getHeaders().exists("Reply-To")) {
|
|
var replyTo = e.getHeaders()["Reply-To"];
|
|
App.log('replyTo "$replyTo" detected');
|
|
args.push("-o");
|
|
args.push('message-header=Reply-To: ${replyTo}');
|
|
}
|
|
|
|
args.push("-m");
|
|
args.push('<html>${e.getHtmlBody()}</html>');
|
|
|
|
// if (App.config.DEBUG)
|
|
// App.log('args=${args.join(" ")}');
|
|
|
|
var exitCode = Sys.command("sendemail", args);
|
|
// var exitCode = 0;
|
|
|
|
var summary = 'email from="${sugoi.mail.Mail.fromString(e.getSender().name, e.getSender().email)}" subject=<${e.getSubject()}>';
|
|
if (exitCode == 0)
|
|
App.log('$summary successfully sent');
|
|
else
|
|
App.log('ERROR: $summary cannot be sent, please review logs');
|
|
|
|
if (callback != null) {
|
|
var map = new MailerResult();
|
|
if (exitCode == 0)
|
|
map.set("*", Success(Sent));
|
|
else
|
|
map.set("*", Failure(HardBounce));
|
|
callback(map);
|
|
}
|
|
}
|
|
}
|