From aee2139a9f7c528196a57911533c14a9a7be8635 Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Tue, 4 Jul 2023 11:57:26 -0400 Subject: [PATCH] Make e-mail host configurable --- src/PrayerTracker/Email.fs | 12 +++++++----- src/PrayerTracker/PrayerRequest.fs | 4 +++- src/PrayerTracker/SmallGroup.fs | 3 ++- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/PrayerTracker/Email.fs b/src/PrayerTracker/Email.fs index ce06ca6..c559910 100644 --- a/src/PrayerTracker/Email.fs +++ b/src/PrayerTracker/Email.fs @@ -2,10 +2,8 @@ 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 @@ -35,11 +33,13 @@ type EmailOptions = /// The e-mail address from which e-mail is sent let private fromAddress = "prayer@bitbadger.solutions" +open MailKit.Security +open Microsoft.Extensions.Configuration + /// Get an SMTP client connection -// FIXME: make host configurable -let getConnection () = task { +let getConnection (cfg : IConfiguration) = task { let client = new SmtpClient () - do! client.ConnectAsync ("127.0.0.1", 25, SecureSocketOptions.None) + do! client.ConnectAsync (cfg.GetConnectionString "SmtpServer", 25, SecureSocketOptions.None) return client } @@ -51,6 +51,8 @@ let createMessage opts = msg.ReplyTo.Add (MailboxAddress (opts.Group.Preferences.EmailFromName, opts.Group.Preferences.EmailFromAddress)) msg +open MimeKit.Text + /// Create an HTML-format e-mail message let createHtmlMessage opts = let bodyText = diff --git a/src/PrayerTracker/PrayerRequest.fs b/src/PrayerTracker/PrayerRequest.fs index aaca84f..527ff15 100644 --- a/src/PrayerTracker/PrayerRequest.fs +++ b/src/PrayerTracker/PrayerRequest.fs @@ -79,6 +79,8 @@ let edit reqId : HttpHandler = requireAccess [ User ] >=> fun next ctx -> task { | Result.Error e -> return! e } +open Microsoft.Extensions.Configuration + // GET /prayer-requests/email/[date] let email date : HttpHandler = requireAccess [ User ] >=> fun next ctx -> task { let s = ctx.Strings @@ -86,7 +88,7 @@ let email date : HttpHandler = requireAccess [ User ] >=> fun next ctx -> task { let! list = generateRequestList ctx listDate let group = ctx.Session.CurrentGroup.Value let! recipients = Members.forGroup group.Id - use! client = Email.getConnection () + use! client = Email.getConnection (ctx.GetService ()) do! Email.sendEmails { Client = client Recipients = recipients diff --git a/src/PrayerTracker/SmallGroup.fs b/src/PrayerTracker/SmallGroup.fs index c75dc42..3629d55 100644 --- a/src/PrayerTracker/SmallGroup.fs +++ b/src/PrayerTracker/SmallGroup.fs @@ -245,6 +245,7 @@ let savePreferences : HttpHandler = requireAccess [ User ] >=> validateCsrf >=> open Giraffe.ViewEngine open PrayerTracker.Views.CommonFunctions +open Microsoft.Extensions.Configuration // POST /small-group/announcement/send let sendAnnouncement : HttpHandler = requireAccess [ User ] >=> validateCsrf >=> fun next ctx -> task { @@ -268,7 +269,7 @@ let sendAnnouncement : HttpHandler = requireAccess [ User ] >=> validateCsrf >=> return users |> List.map (fun u -> { Member.empty with Name = u.Name; Email = u.Email }) else return! Members.forGroup group.Id } - use! client = Email.getConnection () + use! client = Email.getConnection (ctx.GetService ()) do! Email.sendEmails { Client = client Recipients = recipients