|
@ -1,45 +1,53 @@ |
|
|
package sugoi.mail; |
|
|
package sugoi.mail; |
|
|
|
|
|
|
|
|
using Lambda; |
|
|
using Lambda; |
|
|
|
|
|
|
|
|
class Mail implements IMail |
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
public var title : String; |
|
|
|
|
|
public var htmlBody : String; |
|
|
|
|
|
public var textBody : String; |
|
|
|
|
|
var headers : Map<String,String>; |
|
|
|
|
|
var sender : {name:String,email:String,?userId:Int}; |
|
|
|
|
|
var recipients : Array<{name:String,email:String,?userId:Int}>; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Mail implements IMail { |
|
|
|
|
|
public var title:String; |
|
|
|
|
|
public var htmlBody:String; |
|
|
|
|
|
public var textBody:String; |
|
|
|
|
|
|
|
|
|
|
|
var headers:Map<String, String>; |
|
|
|
|
|
var sender:{name:String, email:String, ?userId:Int}; |
|
|
|
|
|
var recipients:Array<{name:String, email:String, ?userId:Int}>; |
|
|
|
|
|
|
|
|
|
|
|
static public function fromString(name:String, email:String) { |
|
|
|
|
|
if (name == null) |
|
|
|
|
|
return email; |
|
|
|
|
|
else { |
|
|
|
|
|
var finalName = thx.text.Diactrics.clean(name); |
|
|
|
|
|
return '${finalName} <${email}>'; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
public function new() { |
|
|
public function new() { |
|
|
recipients = []; |
|
|
recipients = []; |
|
|
headers = new Map(); |
|
|
headers = new Map(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public function getRecipients(){ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function getRecipients() { |
|
|
return recipients; |
|
|
return recipients; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public function setSender(email, ?name,?userId) { |
|
|
|
|
|
if(!isValid(email)) throw "invalid sender email : \""+email+"\""; |
|
|
|
|
|
|
|
|
|
|
|
sender = {name:name,email:email,userId:userId}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function setSender(email, ?name, ?userId) { |
|
|
|
|
|
if (!isValid(email)) |
|
|
|
|
|
throw "invalid sender email : \"" + email + "\""; |
|
|
|
|
|
|
|
|
|
|
|
sender = {name: name, email: email, userId: userId}; |
|
|
return this; |
|
|
return this; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function setReplyTo(email, ?name) { |
|
|
public function setReplyTo(email, ?name) { |
|
|
if(!isValid(email)) throw "invalid reply-to email : \""+email+"\""; |
|
|
|
|
|
|
|
|
|
|
|
setHeader("Reply-To","<"+email+">"+(name==null?"":name)); |
|
|
|
|
|
|
|
|
if (!isValid(email)) |
|
|
|
|
|
throw "invalid reply-to email : \"" + email + "\""; |
|
|
|
|
|
setHeader("Reply-To", fromString(name, email)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function setSubject(s:String) { |
|
|
public function setSubject(s:String) { |
|
|
title = s; |
|
|
title = s; |
|
|
return this; |
|
|
return this; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* can add one or more recipient |
|
|
* can add one or more recipient |
|
|
* @param email |
|
|
* @param email |
|
@ -47,11 +55,12 @@ class Mail implements IMail |
|
|
* @param ?userId |
|
|
* @param ?userId |
|
|
*/ |
|
|
*/ |
|
|
public function addRecipient(email:String, ?name:String, ?userId:Int) { |
|
|
public function addRecipient(email:String, ?name:String, ?userId:Int) { |
|
|
if(!isValid(email)) throw "invalid recipient \""+email+"\""; |
|
|
|
|
|
recipients.push( {email:email, name:name, userId:userId } ); |
|
|
|
|
|
|
|
|
if (!isValid(email)) |
|
|
|
|
|
throw "invalid recipient \"" + email + "\""; |
|
|
|
|
|
recipients.push({email: email, name: name, userId: userId}); |
|
|
return this; |
|
|
return this; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* alias to addRecipient() |
|
|
* alias to addRecipient() |
|
|
* @param email |
|
|
* @param email |
|
@ -62,24 +71,24 @@ class Mail implements IMail |
|
|
addRecipient(email, name, userId); |
|
|
addRecipient(email, name, userId); |
|
|
return this; |
|
|
return this; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public static function isValid( addr : String ){ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static function isValid(addr:String) { |
|
|
var reg = ~/^[^()<>@,;:\\"\[\]\s[:cntrl:]]+@[A-Z0-9][A-Z0-9-]*(\.[A-Z0-9][A-Z0-9-]*)*\.(xn--[A-Z0-9]+|[A-Z]{2,8})$/i; |
|
|
var reg = ~/^[^()<>@,;:\\"\[\]\s[:cntrl:]]+@[A-Z0-9][A-Z0-9-]*(\.[A-Z0-9][A-Z0-9-]*)*\.(xn--[A-Z0-9]+|[A-Z]{2,8})$/i; |
|
|
return addr != null && reg.match(addr); |
|
|
return addr != null && reg.match(addr); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function setHeader(k:String, v:String) { |
|
|
public function setHeader(k:String, v:String) { |
|
|
headers.set(k, v); |
|
|
headers.set(k, v); |
|
|
return this; |
|
|
return this; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* generate a custom key for transactionnal emails, valid during the current day |
|
|
* generate a custom key for transactionnal emails, valid during the current day |
|
|
*/ |
|
|
*/ |
|
|
public function getKey() { |
|
|
public function getKey() { |
|
|
return haxe.crypto.Md5.encode(App.config.get("key")+recipients[0].email+(Date.now().getDate())).substr(0,12); |
|
|
|
|
|
|
|
|
return haxe.crypto.Md5.encode(App.config.get("key") + recipients[0].email + (Date.now().getDate())).substr(0, 12); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* render html from a template + vars |
|
|
* render html from a template + vars |
|
|
* @param tpl A Template path |
|
|
* @param tpl A Template path |
|
@ -88,7 +97,8 @@ class Mail implements IMail |
|
|
public function setHtmlBodyWithTemplate(tpl, ctx:Dynamic) { |
|
|
public function setHtmlBodyWithTemplate(tpl, ctx:Dynamic) { |
|
|
var app = App.current; |
|
|
var app = App.current; |
|
|
var tpl = app.loadTemplate(tpl); |
|
|
var tpl = app.loadTemplate(tpl); |
|
|
if( ctx == null ) ctx = { }; |
|
|
|
|
|
|
|
|
if (ctx == null) |
|
|
|
|
|
ctx = {}; |
|
|
ctx.HOST = App.config.HOST; |
|
|
ctx.HOST = App.config.HOST; |
|
|
ctx.key = getKey(); |
|
|
ctx.key = getKey(); |
|
|
ctx.senderName = sender.name; |
|
|
ctx.senderName = sender.name; |
|
@ -98,28 +108,27 @@ class Mail implements IMail |
|
|
ctx.recipients = recipients; |
|
|
ctx.recipients = recipients; |
|
|
CSSInlining(ctx); |
|
|
CSSInlining(ctx); |
|
|
htmlBody = tpl.execute(ctx); |
|
|
htmlBody = tpl.execute(ctx); |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function setHtmlBody(s) { |
|
|
public function setHtmlBody(s) { |
|
|
htmlBody = s; |
|
|
htmlBody = s; |
|
|
return this; |
|
|
return this; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public function setTextBodyWithTemplate(tpl, ctx:Dynamic) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function setTextBodyWithTemplate(tpl, ctx:Dynamic) { |
|
|
var app = App.current; |
|
|
var app = App.current; |
|
|
var tpl = app.loadTemplate(tpl); |
|
|
var tpl = app.loadTemplate(tpl); |
|
|
if( ctx == null ) ctx = { }; |
|
|
|
|
|
|
|
|
if (ctx == null) |
|
|
|
|
|
ctx = {}; |
|
|
ctx.HOST = App.config.HOST; |
|
|
ctx.HOST = App.config.HOST; |
|
|
ctx.key = getKey(); |
|
|
ctx.key = getKey(); |
|
|
textBody = tpl.execute(ctx); |
|
|
textBody = tpl.execute(ctx); |
|
|
return this; |
|
|
return this; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function CSSInlining(ctx) { |
|
|
function CSSInlining(ctx) { |
|
|
// CSS inlining |
|
|
// CSS inlining |
|
|
var css : Map<String,Array<String>> = new Map(); |
|
|
|
|
|
|
|
|
var css:Map<String, Array<String>> = new Map(); |
|
|
ctx.addStyle = function(sel:String, style:String) { |
|
|
ctx.addStyle = function(sel:String, style:String) { |
|
|
sel = sel.toLowerCase(); |
|
|
sel = sel.toLowerCase(); |
|
|
if (css.exists(sel)) |
|
|
if (css.exists(sel)) |
|
@ -130,11 +139,11 @@ class Mail implements IMail |
|
|
} |
|
|
} |
|
|
var applyStyleRec = null; |
|
|
var applyStyleRec = null; |
|
|
applyStyleRec = function(x:Xml) { |
|
|
applyStyleRec = function(x:Xml) { |
|
|
if (x.nodeType==Xml.Element) { |
|
|
|
|
|
|
|
|
if (x.nodeType == Xml.Element) { |
|
|
var name = x.nodeName.toLowerCase(); |
|
|
var name = x.nodeName.toLowerCase(); |
|
|
if( css.exists(name) ) |
|
|
|
|
|
if (x.get("style")!=null) |
|
|
|
|
|
x.set("style", x.get("style")+";"+css.get(name).join(";")); |
|
|
|
|
|
|
|
|
if (css.exists(name)) |
|
|
|
|
|
if (x.get("style") != null) |
|
|
|
|
|
x.set("style", x.get("style") + ";" + css.get(name).join(";")); |
|
|
else |
|
|
else |
|
|
x.set("style", css.get(name).join(";")); |
|
|
x.set("style", css.get(name).join(";")); |
|
|
for (n in x) |
|
|
for (n in x) |
|
@ -143,40 +152,38 @@ class Mail implements IMail |
|
|
} |
|
|
} |
|
|
ctx.applyStyle = function(raw:String) { |
|
|
ctx.applyStyle = function(raw:String) { |
|
|
var x = Xml.parse(raw); |
|
|
var x = Xml.parse(raw); |
|
|
for(n in x) |
|
|
|
|
|
|
|
|
for (n in x) |
|
|
applyStyleRec(n); |
|
|
applyStyleRec(n); |
|
|
return x.toString(); |
|
|
return x.toString(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function getSubject(){ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function getSubject() { |
|
|
return title; |
|
|
return title; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public function getTitle(){ |
|
|
|
|
|
|
|
|
public function getTitle() { |
|
|
return getSubject(); |
|
|
return getSubject(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public function getHtmlBody(){ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function getHtmlBody() { |
|
|
return htmlBody; |
|
|
return htmlBody; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public function getTextBody(){ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function getTextBody() { |
|
|
return textBody; |
|
|
return textBody; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public function setTextBody(t){ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function setTextBody(t) { |
|
|
textBody = t; |
|
|
textBody = t; |
|
|
return this; |
|
|
return this; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public function getHeaders(){ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function getHeaders() { |
|
|
return headers; |
|
|
return headers; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public function getSender(){ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function getSender() { |
|
|
return sender; |
|
|
return sender; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} |