101 lines
3.3 KiB
Forth
101 lines
3.3 KiB
Forth
/// Methods for sending e-mails
|
|
module PrayerTracker.Email
|
|
|
|
open MailKit.Net.Smtp
|
|
open MailKit.Security
|
|
open Microsoft.Extensions.Localization
|
|
open MimeKit
|
|
open MimeKit.Text
|
|
open PrayerTracker.Entities
|
|
|
|
/// Parameters required to send an e-mail
|
|
type EmailOptions =
|
|
{ /// The SMTP client
|
|
Client : SmtpClient
|
|
|
|
/// The people who should receive the e-mail
|
|
Recipients : Member list
|
|
|
|
/// The small group for which this e-mail is being sent
|
|
Group : SmallGroup
|
|
|
|
/// The subject of the e-mail
|
|
Subject : string
|
|
|
|
/// The body of the e-mail in HTML
|
|
HtmlBody : string
|
|
|
|
/// The body of the e-mail in plain text
|
|
PlainTextBody : string
|
|
|
|
/// Use the current user's preferred language
|
|
Strings : IStringLocalizer
|
|
}
|
|
|
|
/// The e-mail address from which e-mail is sent
|
|
let private fromAddress = "prayer@bitbadger.solutions"
|
|
|
|
/// Get an SMTP client connection
|
|
// FIXME: make host configurable
|
|
let getConnection () = task {
|
|
let client = new SmtpClient ()
|
|
do! client.ConnectAsync ("127.0.0.1", 25, SecureSocketOptions.None)
|
|
return client
|
|
}
|
|
|
|
/// Create a mail message object, filled with everything but the body content
|
|
let createMessage opts =
|
|
let msg = new MimeMessage ()
|
|
msg.From.Add (MailboxAddress (opts.Group.Preferences.EmailFromName, fromAddress))
|
|
msg.Subject <- opts.Subject
|
|
msg.ReplyTo.Add (MailboxAddress (opts.Group.Preferences.EmailFromName, opts.Group.Preferences.EmailFromAddress))
|
|
msg
|
|
|
|
/// Create an HTML-format e-mail message
|
|
let createHtmlMessage opts =
|
|
let bodyText =
|
|
[ """<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><title></title></head><body>"""
|
|
opts.HtmlBody
|
|
"""<hr><div style="text-align:right;font-family:Arial,Helvetica,sans-serif;font-size:8pt;padding-right:10px;">"""
|
|
opts.Strings["Generated by P R A Y E R T R A C K E R"].Value
|
|
"<br><small>"
|
|
opts.Strings["from Bit Badger Solutions"].Value
|
|
"</small></div></body></html>"
|
|
]
|
|
|> String.concat ""
|
|
let msg = createMessage opts
|
|
msg.Body <- new TextPart (TextFormat.Html, Text = bodyText)
|
|
msg
|
|
|
|
/// Create a plain-text-format e-mail message
|
|
let createTextMessage opts =
|
|
let bodyText =
|
|
[ opts.PlainTextBody
|
|
"\n\n--\n"
|
|
opts.Strings["Generated by P R A Y E R T R A C K E R"].Value
|
|
"\n"
|
|
opts.Strings["from Bit Badger Solutions"].Value
|
|
]
|
|
|> String.concat ""
|
|
let msg = createMessage opts
|
|
msg.Body <- new TextPart (TextFormat.Plain, Text = bodyText)
|
|
msg
|
|
|
|
/// Send e-mails to a class
|
|
let sendEmails opts = task {
|
|
use htmlMsg = createHtmlMessage opts
|
|
use plainTextMsg = createTextMessage opts
|
|
|
|
for mbr in opts.Recipients do
|
|
let emailTo = MailboxAddress (mbr.Name, mbr.Email)
|
|
match defaultArg mbr.Format opts.Group.Preferences.DefaultEmailType with
|
|
| HtmlFormat ->
|
|
htmlMsg.To.Add emailTo
|
|
let! _ = opts.Client.SendAsync htmlMsg
|
|
htmlMsg.To.Clear ()
|
|
| PlainTextFormat ->
|
|
plainTextMsg.To.Add emailTo
|
|
let! _ = opts.Client.SendAsync plainTextMsg
|
|
plainTextMsg.To.Clear ()
|
|
}
|