From d1a72876d2dcdd0ce1c11a023c377746580cf2bc Mon Sep 17 00:00:00 2001 From: Jonas Date: Sun, 25 Jan 2026 13:43:25 +0100 Subject: [PATCH] Implement Plunk email sending service with HttpClient integration and DTO definition. --- .../Configurations/UserConfiguration.cs | 2 -- API/Models/Outgoing/PlunkEmailDto.cs | 9 +++++ API/Program.cs | 3 -- API/Services/PlunkEmailSender.cs | 33 ++++++++++++++++--- 4 files changed, 38 insertions(+), 9 deletions(-) create mode 100644 API/Models/Outgoing/PlunkEmailDto.cs diff --git a/API/Database/Configurations/UserConfiguration.cs b/API/Database/Configurations/UserConfiguration.cs index 9e426bd..1390253 100644 --- a/API/Database/Configurations/UserConfiguration.cs +++ b/API/Database/Configurations/UserConfiguration.cs @@ -9,7 +9,5 @@ public class UserConfiguration : IEntityTypeConfiguration public void Configure(EntityTypeBuilder entity) { entity.Property(e => e.Ininitals).HasMaxLength(5); - - } } \ No newline at end of file diff --git a/API/Models/Outgoing/PlunkEmailDto.cs b/API/Models/Outgoing/PlunkEmailDto.cs new file mode 100644 index 0000000..31eda62 --- /dev/null +++ b/API/Models/Outgoing/PlunkEmailDto.cs @@ -0,0 +1,9 @@ +namespace API.Models.Outgoing; + +public class PlunkEmailDto +{ + public required string To { get; set; } + public required string Subject { get; set; } + public required string Body { get; set; } + public string? Name { get; set; } +} \ No newline at end of file diff --git a/API/Program.cs b/API/Program.cs index f3b5e7e..2110c32 100644 --- a/API/Program.cs +++ b/API/Program.cs @@ -68,9 +68,6 @@ app.UseHttpsRedirection(); app.UseAuthentication(); app.UseAuthorization(); -// Map Auth Endpoints -app.MapIdentityApi(); - app.MapControllers(); // Frontend Fallback app.MapFallbackToFile("index.html"); diff --git a/API/Services/PlunkEmailSender.cs b/API/Services/PlunkEmailSender.cs index f0ccd3c..aef5f15 100644 --- a/API/Services/PlunkEmailSender.cs +++ b/API/Services/PlunkEmailSender.cs @@ -1,11 +1,36 @@ -using Microsoft.AspNetCore.Identity.UI.Services; +using System.Net.Http.Headers; +using System.Text; +using System.Text.Json; +using API.Models.Outgoing; +using Microsoft.AspNetCore.Identity.UI.Services; namespace API.Services; -public class PlunkEmailSender : IEmailSender +public class PlunkEmailSender(HttpClient httpClient, string plunkSecretKey) : IEmailSender { - public Task SendEmailAsync(string email, string subject, string htmlMessage) + public async Task SendEmailAsync(string email, string subject, string htmlMessage) { - throw new NotImplementedException(); + var requestBody = new PlunkEmailDto() + { + To = email, + Subject = subject, + Body = htmlMessage + }; + + var jsonContent = new StringContent( + JsonSerializer.Serialize(requestBody), + Encoding.UTF8, + "application/json" + ); + + httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", plunkSecretKey); + + var response = await httpClient.PostAsync("https://api.useplunk.com/v1/send", jsonContent); + + if (!response.IsSuccessStatusCode) + { + var error = await response.Content.ReadAsStringAsync(); + throw new Exception($"Fehler beim Senden der E-Mail über Plunk: {error}"); + } } } \ No newline at end of file