Skip to content
This repository was archived by the owner on Jun 29, 2024. It is now read-only.

Commit 39af0fb

Browse files
Implement e-mail changes
- Add canonical domain handling - Bump version
1 parent dd5f32e commit 39af0fb

File tree

7 files changed

+43
-18
lines changed

7 files changed

+43
-18
lines changed

src/.dockerignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
**/bin/*
2-
**/obj/*
2+
**/obj/*
3+
**/appsettings.*

src/Directory.Build.props

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<Project>
22
<PropertyGroup>
33
<TargetFramework>net7.0</TargetFramework>
4-
<AssemblyVersion>8.1.0.0</AssemblyVersion>
5-
<FileVersion>8.1.0.0</FileVersion>
4+
<AssemblyVersion>8.2.0.0</AssemblyVersion>
5+
<FileVersion>8.2.0.0</FileVersion>
66
<Authors>danieljsummers</Authors>
77
<Company>Bit Badger Solutions</Company>
8-
<Version>8.1.0</Version>
8+
<Version>8.2.0</Version>
99
<DebugType>Embedded</DebugType>
1010
</PropertyGroup>
1111
</Project>

src/PrayerTracker/App.fs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ module Configure =
7070
let _ = dsb.UseNodaTime()
7171
Configuration.useDataSource (dsb.Build ())
7272

73+
let emailCfg = cfg.GetSection "Email"
74+
if (emailCfg.GetChildren >> Seq.isEmpty >> not) () then ConfigurationBinder.Bind(emailCfg, Email.smtpOptions)
75+
7376
let _ = svc.AddSingleton<IDistributedCache, DistributedCache> ()
7477
let _ = svc.AddSession ()
7578
let _ = svc.AddAntiforgery ()
@@ -182,19 +185,21 @@ module Configure =
182185
|> function l -> l.AddConsole().AddDebug()
183186
|> ignore
184187

188+
open BitBadger.AspNetCore.CanonicalDomains
185189
open Microsoft.Extensions.Localization
186190
open Microsoft.Extensions.Options
187191

188192
/// Configure the application
189193
let app (app : IApplicationBuilder) =
190-
let env = app.ApplicationServices.GetRequiredService<IWebHostEnvironment>()
194+
let env = app.ApplicationServices.GetRequiredService<IWebHostEnvironment> ()
191195
if env.IsDevelopment () then
192-
let _ = app.UseDeveloperExceptionPage ()
193-
()
196+
app.UseDeveloperExceptionPage ()
194197
else
195-
let _ = app.UseGiraffeErrorHandler errorHandler
196-
()
198+
app.UseGiraffeErrorHandler errorHandler
199+
|> ignore
197200

201+
let _ = app.UseForwardedHeaders ()
202+
let _ = app.UseCanonicalDomains ()
198203
let _ = app.UseStatusCodePagesWithReExecute "/error/{0}"
199204
let _ = app.UseStaticFiles ()
200205
let _ = app.UseCookiePolicy (CookiePolicyOptions (MinimumSameSitePolicy = SameSiteMode.Strict))

src/PrayerTracker/Email.fs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,39 @@ type EmailOptions =
3030
Strings : IStringLocalizer
3131
}
3232

33-
/// The e-mail address from which e-mail is sent
34-
let private fromAddress = "[email protected]"
33+
/// Options to use when sending e-mail
34+
type SmtpServerOptions() =
35+
/// The hostname of the SMTP server
36+
member val SmtpHost : string = "localhost" with get, set
3537

36-
open MailKit.Security
37-
open Microsoft.Extensions.Configuration
38+
/// The port over which SMTP communication should occur
39+
member val Port : int = 25 with get, set
40+
41+
/// Whether to use SSL when communicating with the SMTP server
42+
member val UseSsl : bool = false with get, set
43+
44+
/// The authentication to use with the SMTP server
45+
member val Authentication : string = "" with get, set
46+
47+
/// The e-mail address from which messages should be sent
48+
member val FromAddress : string = "[email protected]" with get, set
49+
50+
51+
/// The options for the SMTP server
52+
let smtpOptions = SmtpServerOptions ()
3853

3954
/// Get an SMTP client connection
40-
let getConnection (cfg : IConfiguration) = task {
55+
let getConnection () = task {
4156
let client = new SmtpClient ()
42-
do! client.ConnectAsync (cfg.GetConnectionString "SmtpServer", 25, SecureSocketOptions.None)
57+
do! client.ConnectAsync (smtpOptions.SmtpHost, smtpOptions.Port, smtpOptions.UseSsl)
58+
do! client.AuthenticateAsync (smtpOptions.FromAddress, smtpOptions.Authentication)
4359
return client
4460
}
4561

4662
/// Create a mail message object, filled with everything but the body content
4763
let createMessage opts =
4864
let msg = new MimeMessage ()
49-
msg.From.Add (MailboxAddress (opts.Group.Preferences.EmailFromName, fromAddress))
65+
msg.From.Add (MailboxAddress (opts.Group.Preferences.EmailFromName, smtpOptions.FromAddress))
5066
msg.Subject <- opts.Subject
5167
msg.ReplyTo.Add (MailboxAddress (opts.Group.Preferences.EmailFromName, opts.Group.Preferences.EmailFromAddress))
5268
msg

src/PrayerTracker/PrayerRequest.fs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ let email date : HttpHandler = requireAccess [ User ] >=> fun next ctx -> task {
8888
let! list = generateRequestList ctx listDate
8989
let group = ctx.Session.CurrentGroup.Value
9090
let! recipients = Members.forGroup group.Id
91-
use! client = Email.getConnection (ctx.GetService<IConfiguration> ())
91+
use! client = Email.getConnection ()
9292
do! Email.sendEmails
9393
{ Client = client
9494
Recipients = recipients
@@ -98,6 +98,7 @@ let email date : HttpHandler = requireAccess [ User ] >=> fun next ctx -> task {
9898
PlainTextBody = list.AsText s
9999
Strings = s
100100
}
101+
do! client.DisconnectAsync true
101102
return!
102103
viewInfo ctx
103104
|> Views.PrayerRequest.email { list with Recipients = recipients }

src/PrayerTracker/PrayerTracker.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
</ItemGroup>
2525

2626
<ItemGroup>
27+
<PackageReference Include="BitBadger.AspNetCore.CanonicalDomains" Version="1.0.0" />
2728
<PackageReference Include="Giraffe.Htmx" Version="1.9.2" />
2829
<PackageReference Include="NodaTime.Serialization.JsonNet" Version="3.0.1" />
2930
<PackageReference Update="FSharp.Core" Version="7.0.300" />

src/PrayerTracker/SmallGroup.fs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ let sendAnnouncement : HttpHandler = requireAccess [ User ] >=> validateCsrf >=>
269269
return users |> List.map (fun u -> { Member.empty with Name = u.Name; Email = u.Email })
270270
else return! Members.forGroup group.Id
271271
}
272-
use! client = Email.getConnection (ctx.GetService<IConfiguration> ())
272+
use! client = Email.getConnection ()
273273
do! Email.sendEmails
274274
{ Client = client
275275
Recipients = recipients
@@ -280,6 +280,7 @@ let sendAnnouncement : HttpHandler = requireAccess [ User ] >=> validateCsrf >=>
280280
PlainTextBody = plainText
281281
Strings = s
282282
}
283+
do! client.DisconnectAsync true
283284
// Add to the request list if desired
284285
match model.SendToClass, model.AddToRequestList with
285286
| "N", _

0 commit comments

Comments
 (0)