1 module yurai.external.services.vibemailservice;
2 
3 import yurai.core.settings;
4 
5 static if (Yurai_UseVibed_Mail)
6 {
7   import std.string : format;
8 
9   version (Windows)
10   {
11     import std.datetime : Clock, WindowsTimeZone;
12   }
13   else
14   {
15     import std.datetime : Clock, PosixTimeZone;
16   }
17 
18   import vibe.d : SMTPClientSettings,
19                   TLSContext, TLSPeerValidationMode, TLSVersion,
20                   Mail, sendMail, toRFC822DateTimeString;
21 
22   import yurai.services.imailservice;
23 
24   public final class VibeMailService : IMailService
25   {
26     private:
27     string _host;
28     ushort _port;
29     string _username;
30     string _password;
31     string _contentType;
32     string _timeZone;
33     string _senderName;
34     string _senderEmail;
35     SMTPClientSettings _settings;
36 
37     public:
38     final:
39     this(string host, ushort port, string username = null, string password = null)
40     {
41       _settings = new SMTPClientSettings(host, port);
42     }
43 
44     /// Sets the tls context setup.
45     void setTlsContextSetup(void delegate(scope TLSContext) @safe newContextSetup)
46     {
47       _settings.tlsContextSetup = newContextSetup;
48     }
49 
50     /// Sets the tls validation mode.
51     void setTlsValidationMode(TLSPeerValidationMode newValidationMode)
52     {
53       _settings.tlsValidationMode = newValidationMode;
54     }
55 
56     /// Sets the tls version.
57     void setTlsVersion(TLSVersion newVersion)
58     {
59       _settings.tlsVersion = newVersion;
60     }
61 
62     void setDefaultContentType(string contentType)
63     {
64       _contentType = contentType;
65     }
66 
67     void setTimeZone(string timeZone)
68     {
69       _timeZone = timeZone;
70     }
71 
72     void setDefaultSender(string senderEmail, string senderName)
73     {
74       _senderEmail = senderEmail;
75       _senderName = senderName;
76     }
77 
78     void send(string subject, string message, string senderEmail, string senderName, string fromEmail, string fromName, string toEmail, string toName)
79     {
80       send(subject, message, senderEmail, senderName, fromEmail, fromName, toEmail, toName, _contentType);
81     }
82     void send(string subject, string message, string fromEmail, string fromName, string toEmail, string toName)
83     {
84       send(subject, message, _senderEmail, _senderName, fromEmail, fromName, toEmail, toName, _contentType);
85     }
86     void send(string subject, string message, string fromEmail, string fromName, string toEmail, string toName, string contentType)
87     {
88       send(subject, message, _senderEmail, _senderName, fromEmail, fromName, toEmail, toName, contentType);
89     }
90 
91     void send(string subject, string message, string senderEmail, string senderName, string fromEmail, string fromName, string toEmail, string toName, string contentType)
92     {
93       auto mail = new Mail;
94 
95       version (Windows)
96       {
97         auto timeZone = WindowsTimeZone.getTimeZone(_timeZone);
98       }
99       else
100       {
101         auto timeZone = PosixTimeZone.getTimeZone(_timeZone);
102       }
103 
104       mail.headers["Date"] = Clock.currTime(timeZone).toRFC822DateTimeString();
105       mail.headers["Sender"] = "%s <%s>".format(senderName, senderEmail);
106       mail.headers["From"] = "%s <%s>".format(fromName, fromEmail);
107       mail.headers["To"] = toName ? "".format(toName, toEmail) : toEmail;
108       mail.headers["Subject"] = subject;
109       mail.headers["Content-Type"] = contentType;
110       mail.bodyText = message;
111 
112       sendMail(_settings, mail);
113     }
114   }
115 }