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.

67 lines
1.5 KiB

  1. package sugoi.mail;
  2. import tink.core.Future;
  3. import tink.core.Noise;
  4. import sugoi.mail.IMailer;
  5. import smtpmailer.Address;
  6. /**
  7. * Send emails thru SMTP by using ben merckx's library
  8. * @ref https://github.com/benmerckx/smtpmailer
  9. */
  10. class SmtpMailer implements IMailer
  11. {
  12. var m : smtpmailer.SmtpMailer;
  13. public function new(){}
  14. public function init(?conf:{smtp_host:String,smtp_port:Int,smtp_user:String,smtp_pass:String}) :IMailer
  15. {
  16. m = new smtpmailer.SmtpMailer({
  17. host: conf.smtp_host,
  18. port: conf.smtp_port,
  19. auth: {
  20. username: conf.smtp_user,
  21. password: conf.smtp_pass
  22. }
  23. });
  24. return this;
  25. }
  26. public function send(e:sugoi.mail.IMail,?params:Dynamic,?callback:MailerResult->Void)
  27. {
  28. var surprise = m.send({
  29. subject: e.getSubject(),
  30. /*from: e.getSender().email,
  31. to: Lambda.array(Lambda.map(e.getRecipients(), function(x) return smtpmailer.Address.ofString(x.email) )),
  32. //headers : e.getHeaders(),*/
  33. from: new Address({address:e.getSender().email}),
  34. to: Lambda.array(Lambda.map(e.getRecipients(), function(x) return new Address({address:x.email}) )),
  35. headers : e.getHeaders(),
  36. content: {
  37. text: e.getTextBody(),
  38. html: e.getHtmlBody()
  39. }/*,
  40. attachments: []*/
  41. });
  42. if (callback != null){
  43. surprise.handle(function(s){
  44. var map = new MailerResult();
  45. switch(s){
  46. case Success(_):
  47. map.set("*",Success(Sent));
  48. case Failure(e):
  49. map.set("*",Failure(GenericError(e)));
  50. }
  51. callback(map);
  52. });
  53. }
  54. }
  55. }