|
|
|
|
@@ -3,17 +3,41 @@ module JobsJobsJobs.Email
|
|
|
|
|
open System.Net
|
|
|
|
|
open JobsJobsJobs.Domain
|
|
|
|
|
open MailKit.Net.Smtp
|
|
|
|
|
open MailKit.Security
|
|
|
|
|
open MimeKit
|
|
|
|
|
|
|
|
|
|
/// Options to use when sending e-mail
|
|
|
|
|
type EmailOptions() =
|
|
|
|
|
/// The hostname of the SMTP server
|
|
|
|
|
member val SmtpHost : string = "localhost" with get, set
|
|
|
|
|
|
|
|
|
|
/// The port over which SMTP communication should occur
|
|
|
|
|
member val Port : int = 25 with get, set
|
|
|
|
|
|
|
|
|
|
/// Whether to use SSL when communicating with the SMTP server
|
|
|
|
|
member val UseSsl : bool = false with get, set
|
|
|
|
|
|
|
|
|
|
/// The authentication to use with the SMTP server
|
|
|
|
|
member val Authentication : string = "" with get, set
|
|
|
|
|
|
|
|
|
|
/// The e-mail address from which messages should be sent
|
|
|
|
|
member val FromAddress : string = "nobody@noagendacareers.com" with get, set
|
|
|
|
|
|
|
|
|
|
/// The name from which messages should be sent
|
|
|
|
|
member val FromName : string = "Jobs, Jobs, Jobs" with get, set
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// The options for the SMTP server
|
|
|
|
|
let mutable options = EmailOptions ()
|
|
|
|
|
|
|
|
|
|
/// Private functions for sending e-mail
|
|
|
|
|
[<AutoOpen>]
|
|
|
|
|
module private Helpers =
|
|
|
|
|
|
|
|
|
|
/// Create an SMTP client
|
|
|
|
|
let smtpClient () = backgroundTask {
|
|
|
|
|
let createClient () = backgroundTask {
|
|
|
|
|
let client = new SmtpClient ()
|
|
|
|
|
do! client.ConnectAsync ("localhost", 25, SecureSocketOptions.None)
|
|
|
|
|
do! client.ConnectAsync (options.SmtpHost, options.Port, options.UseSsl)
|
|
|
|
|
do! client.AuthenticateAsync (options.FromAddress, options.Authentication)
|
|
|
|
|
return client
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -25,12 +49,18 @@ module private Helpers =
|
|
|
|
|
msg.Subject <- subject
|
|
|
|
|
msg
|
|
|
|
|
|
|
|
|
|
/// Send a message
|
|
|
|
|
let sendMessage msg = backgroundTask {
|
|
|
|
|
use! client = createClient ()
|
|
|
|
|
let! result = client.SendAsync msg
|
|
|
|
|
do! client.DisconnectAsync true
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Send an account confirmation e-mail
|
|
|
|
|
let sendAccountConfirmation citizen security = backgroundTask {
|
|
|
|
|
let token = WebUtility.UrlEncode security.Token.Value
|
|
|
|
|
use! client = smtpClient ()
|
|
|
|
|
use msg = createMessage citizen "Account Confirmation Request"
|
|
|
|
|
let token = WebUtility.UrlEncode security.Token.Value
|
|
|
|
|
use msg = createMessage citizen "Account Confirmation Request"
|
|
|
|
|
|
|
|
|
|
let text =
|
|
|
|
|
[ $"ITM, {Citizen.name citizen}!"
|
|
|
|
|
@@ -57,14 +87,13 @@ let sendAccountConfirmation citizen security = backgroundTask {
|
|
|
|
|
use msgText = new TextPart (Text = text)
|
|
|
|
|
msg.Body <- msgText
|
|
|
|
|
|
|
|
|
|
return! client.SendAsync msg
|
|
|
|
|
return! sendMessage msg
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Send a password reset link
|
|
|
|
|
let sendPasswordReset citizen security = backgroundTask {
|
|
|
|
|
let token = WebUtility.UrlEncode security.Token.Value
|
|
|
|
|
use! client = smtpClient ()
|
|
|
|
|
use msg = createMessage citizen "Reset Password for Jobs, Jobs, Jobs"
|
|
|
|
|
let token = WebUtility.UrlEncode security.Token.Value
|
|
|
|
|
use msg = createMessage citizen "Reset Password for Jobs, Jobs, Jobs"
|
|
|
|
|
|
|
|
|
|
let text =
|
|
|
|
|
[ $"ITM, {Citizen.name citizen}!"
|
|
|
|
|
@@ -90,5 +119,5 @@ let sendPasswordReset citizen security = backgroundTask {
|
|
|
|
|
use msgText = new TextPart (Text = text)
|
|
|
|
|
msg.Body <- msgText
|
|
|
|
|
|
|
|
|
|
return! client.SendAsync msg
|
|
|
|
|
return! sendMessage msg
|
|
|
|
|
}
|
|
|
|
|
|