1 module appbase.gateway.smtp; 2 3 import std.net.curl : SMTP; 4 import std.typecons : Tuple; 5 import std.string : split; 6 import std.algorithm.iteration : each; 7 8 public import appbase.gateway.smtp.attachment; 9 public import appbase.gateway.smtp.message; 10 11 /++ 12 Send a email. 13 recipients: a@xx.com;b@xx.com... 14 +/ 15 Tuple!(int, string) sendMail(const string server, 16 const string senderAccount, const string senderPassword, const string senderNicename, 17 const string recipients, const string subject, const string content) 18 { 19 Tuple!(int, string) result; 20 21 auto client = SMTP("smtp://" ~ server); 22 23 try 24 { 25 client.setAuthentication(senderAccount, senderPassword); 26 } 27 catch (Exception e) 28 { 29 client.shutdown(); 30 31 result[0] = -1; 32 result[1] = e.msg; 33 34 return result; 35 } 36 37 string[] addressees = recipients.split(";"); 38 Recipient[] _recipients; 39 addressees.each!((a) => (_recipients ~= Recipient(a, ""))); 40 addressees.each!((ref a) => (a = "<" ~ a ~ ">")); 41 client.mailTo = cast(const(char)[][])addressees; 42 client.mailFrom = "<" ~ senderAccount ~ ">"; 43 44 auto message = SmtpMessage( 45 Recipient(senderAccount, senderNicename), 46 _recipients, 47 subject, 48 content, 49 "", 50 ); 51 client.message = message.toString(); 52 53 int trys; 54 label_send: try 55 { 56 client.perform(); 57 } 58 catch (Exception e) 59 { 60 if (++trys < 3) 61 { 62 goto label_send; 63 } 64 65 client.shutdown(); 66 67 result[0] = -2; 68 result[1] = e.msg; 69 70 return result; 71 } 72 73 return result; 74 }