sendemail is working
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
package sugoi.mail;
|
||||
|
||||
import sugoi.mail.IMail;
|
||||
import sugoi.mail.IMailer;
|
||||
|
||||
@@ -6,23 +7,21 @@ import sugoi.mail.IMailer;
|
||||
* Manage an email buffer in a table before sending them
|
||||
* @author fbarbut
|
||||
*/
|
||||
class BufferedMailer implements IMailer
|
||||
{
|
||||
var conf : Dynamic;
|
||||
var type : String;
|
||||
class BufferedMailer implements IMailer {
|
||||
var conf:Dynamic;
|
||||
var type:String;
|
||||
|
||||
public function new() {}
|
||||
|
||||
public function init(?c:Dynamic):IMailer{
|
||||
|
||||
public function init(?c:Dynamic):IMailer {
|
||||
return this;
|
||||
}
|
||||
|
||||
public function defineFinalMailer(type:String){
|
||||
public function defineFinalMailer(type:String) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public function send(m:sugoi.mail.IMail,?params:Dynamic,?callback:MailerResult->Void):Void{
|
||||
|
||||
public function send(m:sugoi.mail.IMail, ?params:Dynamic, ?callback:MailerResult->Void):Void {
|
||||
var bm = new sugoi.db.BufferedMail();
|
||||
bm.headers = m.getHeaders();
|
||||
bm.title = m.getTitle();
|
||||
@@ -30,32 +29,28 @@ class BufferedMailer implements IMailer
|
||||
bm.textBody = m.getTextBody();
|
||||
bm.recipients = m.getRecipients();
|
||||
bm.sender = m.getSender();
|
||||
|
||||
|
||||
bm.mailerType = this.type;
|
||||
|
||||
//custom params
|
||||
if(params!=null){
|
||||
|
||||
// custom params
|
||||
if (params != null) {
|
||||
bm.data = params;
|
||||
if(Reflect.hasField(params,"remoteId")){
|
||||
bm.remoteId = Reflect.getProperty(params,"remoteId");
|
||||
if (Reflect.hasField(params, "remoteId")) {
|
||||
bm.remoteId = Reflect.getProperty(params, "remoteId");
|
||||
}
|
||||
}
|
||||
|
||||
//set sending status as "queued"
|
||||
}
|
||||
|
||||
// set sending status as "queued"
|
||||
var map = new MailerResult();
|
||||
for( r in m.getRecipients() ){
|
||||
map.set( r.email , Success(Queued) );
|
||||
for (r in m.getRecipients()) {
|
||||
map.set(r.email, Success(Queued));
|
||||
}
|
||||
|
||||
bm.status = map;
|
||||
bm.insert();
|
||||
|
||||
if(callback!=null) callback(map);
|
||||
|
||||
App.log('email from=<${m.getSender().email}>, subject=<${m.getSubject()}> buffered, waiting for cron to be further processed');
|
||||
if (callback != null)
|
||||
callback(map);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
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 mailServer = "mail1.zourit.net";
|
||||
var mailPort = 587;
|
||||
|
||||
var fromName = "John Doe";
|
||||
var fromEmail = "no-reply@comptoirduvrac.re";
|
||||
|
||||
var args = [
|
||||
"-f",
|
||||
'${fromName} <${fromEmail}>',
|
||||
"-s",
|
||||
"mail1.zourit.net:587",
|
||||
"-t",
|
||||
"pvincent@comptoirduvrac.re",
|
||||
// "-bcc", "pvincent974@gmail.com,pvincent974@laposte.net",
|
||||
"-u",
|
||||
e.getSubject(),
|
||||
"-m",
|
||||
"alors tout va bien",
|
||||
"-xu",
|
||||
"postmaster@comptoirduvrac.re",
|
||||
"-xp",
|
||||
"QqQeAPT6EpoK"
|
||||
];
|
||||
|
||||
App.log('args=${args.join(" ")}');
|
||||
|
||||
var exitCode = Sys.command("sendemail", args);
|
||||
// var exitCode = 0;
|
||||
|
||||
if (exitCode == 0)
|
||||
App.log('email from="${e.getSender().name} <${e.getSender().email}"> subject=<${e.getSubject()}> successfully sent');
|
||||
else
|
||||
App.log('ERROR: email from="${e.getSender().name} <${e.getSender().email}"> subject=<${e.getSubject()}> cannot be sent');
|
||||
|
||||
// sys.io.File.saveContent('/tmp/my_file.json', "CONTENT");
|
||||
|
||||
// var surprise = m.send({
|
||||
// subject: e.getSubject(),
|
||||
// from: new Address({address: e.getSender().email}),
|
||||
// to: Lambda.array(Lambda.map(e.getRecipients(), function(x) return new Address({address: x.email}))),
|
||||
// headers: e.getHeaders(),
|
||||
// content: {
|
||||
// text: e.getTextBody(),
|
||||
// html: e.getHtmlBody()
|
||||
// }
|
||||
// });
|
||||
|
||||
if (callback != null) {
|
||||
var map = new MailerResult();
|
||||
if (exitCode == 0)
|
||||
map.set("*", Success(Sent));
|
||||
else
|
||||
map.set("*", Failure(HardBounce));
|
||||
callback(map);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user