Browse Source

sendemail is working

master
pvincent 3 years ago
parent
commit
a1ece5258a
  1. 37
      src/App.hx
  2. 4
      src/controller/Cron.hx
  3. 11
      src/controller/Messages.hx
  4. 32
      src/sugoi/db/BufferedMail.hx
  5. 15
      src/sugoi/mail/BufferedMailer.hx
  6. 100
      src/sugoi/mail/SendEmailMailer.hx

37
src/App.hx

@ -77,10 +77,14 @@ class App extends sugoi.BaseApp {
} }
public static function log(t:Dynamic, ?infos:haxe.PosInfos) { public static function log(t:Dynamic, ?infos:haxe.PosInfos) {
// if (App.config.DEBUG) {
// neko.Web.logMessage(Std.string(t)); // write in Apache error log
// }
neko.Web.logMessage('[${infos.fileName}:${infos.lineNumber} ${infos.className}#${infos.methodName}] ' + Std.string(t));
var message:String = '[${infos.fileName}:${infos.lineNumber} ${infos.className}#${infos.methodName}] ';
message += Std.string(t);
// trace(message);
if (neko.Web.isModNeko)
neko.Web.logMessage(message);
else
Sys.println(message);
} }
public function event(e:Event) { public function event(e:Event) {
@ -230,17 +234,28 @@ class App extends sugoi.BaseApp {
} }
public static function getMailer():sugoi.mail.IMailer { public static function getMailer():sugoi.mail.IMailer {
var mailer:sugoi.mail.IMailer = new sugoi.mail.BufferedMailer();
var typeOfMailer = sugoi.db.Variable.get("mailer");
App.log('type of mailer is <$typeOfMailer>');
if (App.config.DEBUG) {
App.log("DEBUGEmailer enabled => emails are written to tmp folder");
mailer = new sugoi.mail.DebugMailer();
} else {
if (sugoi.db.Variable.get("mailer") == null) {
if (typeOfMailer == null) {
var msg = sugoi.i18n.Locale.texts._("Please configure the email settings in a <href='/admin/emails'>this section</a>"); var msg = sugoi.i18n.Locale.texts._("Please configure the email settings in a <href='/admin/emails'>this section</a>");
throw sugoi.ControllerAction.ErrorAction("/", msg); throw sugoi.ControllerAction.ErrorAction("/", msg);
} }
if (App.config.DEBUG) {
App.log('mailer in DEBUG mode, no deferred mail from CRON');
var conf = {
smtp_host: sugoi.db.Variable.get("smtp_host"),
smtp_port: sugoi.db.Variable.getInt("smtp_port"),
smtp_user: sugoi.db.Variable.get("smtp_user"),
smtp_pass: sugoi.db.Variable.get("smtp_pass")
};
return new sugoi.mail.SendEmailMailer().init(conf);
}
var mailer:sugoi.mail.IMailer = new sugoi.mail.BufferedMailer();
if (sugoi.db.Variable.get("mailer") == "mandrill") { if (sugoi.db.Variable.get("mailer") == "mandrill") {
// Buffered emails with Mandrill // Buffered emails with Mandrill
untyped mailer.defineFinalMailer("mandrill"); untyped mailer.defineFinalMailer("mandrill");
@ -248,7 +263,6 @@ class App extends sugoi.BaseApp {
// Buffered emails with SMTP // Buffered emails with SMTP
untyped mailer.defineFinalMailer("smtp"); untyped mailer.defineFinalMailer("smtp");
} }
}
return mailer; return mailer;
} }
@ -265,7 +279,6 @@ class App extends sugoi.BaseApp {
App.log('about to send email with subject=<${m.getSubject()}> ...'); App.log('about to send email with subject=<${m.getSubject()}> ...');
getMailer().send(m, params, function(o) {}); getMailer().send(m, params, function(o) {});
App.log('email from=<${m.getSender().email}>, subject=<${m.getSubject()}> successfully sent');
} }
public static function quickMail(to:String, subject:String, html:String, ?group:db.Amap) { public static function quickMail(to:String, subject:String, html:String, ?group:db.Amap) {

4
src/controller/Cron.hx

@ -444,7 +444,6 @@ class Cron extends Controller {
*/ */
function sendEmailsfromBuffer() { function sendEmailsfromBuffer() {
App.log("Send Emails from Buffer"); App.log("Send Emails from Buffer");
print("<h3>Send Emails from Buffer</h3>");
// send // send
for (e in sugoi.db.BufferedMail.manager.search($sdate == null, {limit: 50, orderBy: -cdate}, false)) { for (e in sugoi.db.BufferedMail.manager.search($sdate == null, {limit: 50, orderBy: -cdate}, false)) {
@ -452,8 +451,7 @@ class Cron extends Controller {
if (e.isSent()) if (e.isSent())
continue; continue;
App.log('Send Email id=${e.id} - title=${e.title}');
print('#${e.id} - ${e.title}');
App.log('Send Email id=#${e.id} - title=${e.title}');
e.finallySend(); e.finallySend();
Sys.sleep(0.1); Sys.sleep(0.1);
} }

11
src/controller/Messages.hx

@ -82,16 +82,7 @@ class Messages extends Controller {
var html = app.processTemplate("mail/message.mtt", {text: text, group: app.user.amap, list: getListName(listId)}); var html = app.processTemplate("mail/message.mtt", {text: text, group: app.user.amap, list: getListName(listId)});
e.setHtmlBody(html); e.setHtmlBody(html);
// FIXME: re-apply sendMail
// App.sendMail(e, app.user.getAmap(), listId, app.user);
// Sys.command("sendemail", [
// "-f", "no-reply@comptoirduvrac.re", "-s", "mail1.zourit.net:587", "-t", "pvincent@comptoirduvrac.re", "-bcc",
// "pvincent974@gmail.com,pvincent974@laposte.net", "-u", subject, "-m", "alors tout va bien", "-xu", "postmaster@comptoirduvrac.re", "-xp",
// "QqQeAPT6EpoK"
// ]);
sys.io.File.saveContent('/tmp/my_file.json', "CONTENT");
App.sendMail(e, app.user.getAmap(), listId, app.user);
// store message // store message
var lm = new db.Message(); var lm = new db.Message();

32
src/sugoi/db/BufferedMail.hx

@ -1,4 +1,5 @@
package sugoi.db; package sugoi.db;
import sys.db.Types; import sys.db.Types;
import sugoi.mail.IMail; import sugoi.mail.IMail;
import sugoi.mail.IMailer; import sugoi.mail.IMailer;
@ -7,8 +8,7 @@ import sugoi.mail.IMailer;
* DB Buffer for emails * DB Buffer for emails
*/ */
@:index(remoteId, sdate, cdate) @:index(remoteId, sdate, cdate)
class BufferedMail extends sys.db.Object
{
class BufferedMail extends sys.db.Object {
public var id:SId; public var id:SId;
// email content // email content
@ -31,7 +31,6 @@ class BufferedMail extends sys.db.Object
public var data:SNull<SData<Dynamic>>; // custom datas public var data:SNull<SData<Dynamic>>; // custom datas
public var remoteId:SNull<Int>; // custom remote Id (userId, groupId ...) public var remoteId:SNull<Int>; // custom remote Id (userId, groupId ...)
public function getMailerResultMessage(k:String):{failure:String, success:String} { public function getMailerResultMessage(k:String):{failure:String, success:String} {
var t = sugoi.i18n.Locale.texts; var t = sugoi.i18n.Locale.texts;
var out = {failure: null, success: null}; var out = {failure: null, success: null};
@ -44,7 +43,6 @@ class BufferedMail extends sys.db.Object
case Spam: t._("Message considered as spam"); case Spam: t._("Message considered as spam");
case Unsub: t._("This user unsubscribed"); case Unsub: t._("This user unsubscribed");
case Unsigned: t._("Sender incorrect (Unsigned)"); case Unsigned: t._("Sender incorrect (Unsigned)");
}; };
case tink.core.Outcome.Success(s): case tink.core.Outcome.Success(s):
out.success = switch (s) { out.success = switch (s) {
@ -53,10 +51,8 @@ class BufferedMail extends sys.db.Object
}; };
} }
return out; return out;
} }
public function new() { public function new() {
super(); super();
cdate = Date.now(); cdate = Date.now();
@ -71,8 +67,8 @@ class BufferedMail extends sys.db.Object
* Finally really send the message * Finally really send the message
*/ */
public function finallySend():Void { public function finallySend():Void {
if(isSent()) throw "already sent";
if (isSent())
throw "already sent";
var conf = { var conf = {
smtp_host: sugoi.db.Variable.get("smtp_host"), smtp_host: sugoi.db.Variable.get("smtp_host"),
@ -81,27 +77,27 @@ class BufferedMail extends sys.db.Object
smtp_pass: sugoi.db.Variable.get("smtp_pass") smtp_pass: sugoi.db.Variable.get("smtp_pass")
}; };
App.log('choose mail from ${this.mailerType}');
var mailer:sugoi.mail.IMailer = switch (this.mailerType) { var mailer:sugoi.mail.IMailer = switch (this.mailerType) {
case "mandrill": case "mandrill":
new sugoi.mail.MandrillMailer().init(conf); new sugoi.mail.MandrillMailer().init(conf);
case "smtp": case "smtp":
new sugoi.mail.SmtpMailer().init(conf);
case "debug":
new sugoi.mail.DebugMailer();
new sugoi.mail.SendEmailMailer().init(conf);
default: default:
throw "Unknown mailer type : " + this.mailerType; throw "Unknown mailer type : " + this.mailerType;
}; };
var m = new sugoi.mail.Mail(); var m = new sugoi.mail.Mail();
for( k in this.headers.keys() ) m.setHeader( k,headers[k] );
for (k in this.headers.keys())
m.setHeader(k, headers[k]);
m.setSubject(this.title); m.setSubject(this.title);
for( r in recipients) m.setRecipient(r.email,r.name,r.userId);
for (r in recipients)
m.setRecipient(r.email, r.name, r.userId);
m.setSender(sender.email, sender.name, sender.userId); m.setSender(sender.email, sender.name, sender.userId);
m.setHtmlBody(this.htmlBody); m.setHtmlBody(this.htmlBody);
m.setTextBody(this.textBody); m.setTextBody(this.textBody);
this.lock(); this.lock();
this.tries++; this.tries++;
@ -116,17 +112,11 @@ class BufferedMail extends sys.db.Object
} }
this.update(); this.update();
} }
function afterSendCb(status:MailerResult) { function afterSendCb(status:MailerResult) {
// App.current.logError(status); // App.current.logError(status);
this.status = status; this.status = status;
this.update(); this.update();
} }
} }

15
src/sugoi/mail/BufferedMailer.hx

@ -1,4 +1,5 @@
package sugoi.mail; package sugoi.mail;
import sugoi.mail.IMail; import sugoi.mail.IMail;
import sugoi.mail.IMailer; import sugoi.mail.IMailer;
@ -6,8 +7,7 @@ import sugoi.mail.IMailer;
* Manage an email buffer in a table before sending them * Manage an email buffer in a table before sending them
* @author fbarbut * @author fbarbut
*/ */
class BufferedMailer implements IMailer
{
class BufferedMailer implements IMailer {
var conf:Dynamic; var conf:Dynamic;
var type:String; var type:String;
@ -22,7 +22,6 @@ class BufferedMailer implements IMailer
} }
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(); var bm = new sugoi.db.BufferedMail();
bm.headers = m.getHeaders(); bm.headers = m.getHeaders();
bm.title = m.getTitle(); bm.title = m.getTitle();
@ -50,12 +49,8 @@ class BufferedMailer implements IMailer
bm.status = map; bm.status = map;
bm.insert(); 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);
} }
} }

100
src/sugoi/mail/SendEmailMailer.hx

@ -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);
}
}
}
Loading…
Cancel
Save