diff --git a/TickAPI/TickAPI/Common/Mail/Abstractions/IMailService.cs b/TickAPI/TickAPI/Common/Mail/Abstractions/IMailService.cs new file mode 100644 index 0000000..b7f3965 --- /dev/null +++ b/TickAPI/TickAPI/Common/Mail/Abstractions/IMailService.cs @@ -0,0 +1,12 @@ +using TickAPI.Common.Mail.Models; +using TickAPI.Common.Results; + +namespace TickAPI.Common.Mail.Abstractions; + +public interface IMailService +{ + public Task SendTicketAsync(string toEmail, string toLogin, string eventName, byte[] pdfData); + + public Task SendMailAsync(string toEmail, string toLogin, string subject, string content, + List? attachments); +} \ No newline at end of file diff --git a/TickAPI/TickAPI/Common/Mail/Models/MailAttachment.cs b/TickAPI/TickAPI/Common/Mail/Models/MailAttachment.cs new file mode 100644 index 0000000..d59be62 --- /dev/null +++ b/TickAPI/TickAPI/Common/Mail/Models/MailAttachment.cs @@ -0,0 +1,8 @@ +namespace TickAPI.Common.Mail.Models; + +public class MailAttachment +{ + public string fileName { get; set; } + public string base64Content { get; set; } + public string fileType { get; set; } +} \ No newline at end of file diff --git a/TickAPI/TickAPI/Common/Mail/Services/MailService.cs b/TickAPI/TickAPI/Common/Mail/Services/MailService.cs new file mode 100644 index 0000000..1e5a1c9 --- /dev/null +++ b/TickAPI/TickAPI/Common/Mail/Services/MailService.cs @@ -0,0 +1,64 @@ +using System.Net; +using SendGrid; +using SendGrid.Helpers.Mail; +using TickAPI.Common.Mail.Abstractions; +using TickAPI.Common.Mail.Models; +using TickAPI.Common.Results; + +namespace TickAPI.Common.Mail.Services; + +public class MailService : IMailService +{ + private SendGridClient _client; + private EmailAddress _fromEmailAddress; + + public MailService(IConfiguration configuration) + { + var apiKey = configuration["SendGrid:ApiKey"]; + _client = new SendGridClient(apiKey); + var fromEmail = configuration["SendGrid:FromEmail"]; + var fromName = configuration["SendGrid:FromName"]; + _fromEmailAddress = new EmailAddress(fromEmail, fromName); + } + + public async Task SendTicketAsync(string toEmail, string toLogin, string eventName, byte[] pdfData) + { + var subject = $"Ticket for {eventName}"; + var htmlContent = "Download your ticket from attachments"; + var base64Content = Convert.ToBase64String(pdfData); + List attachments = + [ + new MailAttachment + { + base64Content = base64Content, + fileName = "ticket.pdf", + fileType = "application/pdf" + } + ]; + var res = await SendMailAsync(toEmail, toLogin, subject, htmlContent, attachments); + return res; + } + + public async Task SendMailAsync(string toEmail, string toLogin, string subject, string content, + List? attachments = null) + { + var toEmailAddress = new EmailAddress(toEmail, toLogin); + var msg = MailHelper.CreateSingleEmail(_fromEmailAddress, toEmailAddress, subject, + null, content); + if (attachments != null) + { + foreach (var a in attachments) + { + msg.AddAttachment(a.fileName, a.base64Content, a.fileType); + } + } + + var response = await _client.SendEmailAsync(msg).ConfigureAwait(false); + if (response.IsSuccessStatusCode) + { + return Result.Success(); + } + return Result.Failure(500, "Error sending email"); + } + +} \ No newline at end of file diff --git a/TickAPI/TickAPI/Program.cs b/TickAPI/TickAPI/Program.cs index 59b7d54..40523e4 100644 --- a/TickAPI/TickAPI/Program.cs +++ b/TickAPI/TickAPI/Program.cs @@ -38,6 +38,8 @@ using TickAPI.Common.Claims.Services; using TickAPI.Common.Redis.Abstractions; using TickAPI.Common.Redis.Services; +using TickAPI.Common.Mail.Abstractions; +using TickAPI.Common.Mail.Services; // Builder constants const string allowClientPolicyName = "AllowClient"; @@ -120,6 +122,7 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); @@ -202,4 +205,4 @@ app.MapControllers(); -app.Run(); \ No newline at end of file +app.Run(); diff --git a/TickAPI/TickAPI/TickAPI.csproj b/TickAPI/TickAPI/TickAPI.csproj index 2f1a9cc..a4ca3a8 100644 --- a/TickAPI/TickAPI/TickAPI.csproj +++ b/TickAPI/TickAPI/TickAPI.csproj @@ -18,6 +18,8 @@ runtime; build; native; contentfiles; analyzers; buildtransitive + + diff --git a/TickAPI/TickAPI/appsettings.example.json b/TickAPI/TickAPI/appsettings.example.json index 04cae82..bbb517d 100644 --- a/TickAPI/TickAPI/appsettings.example.json +++ b/TickAPI/TickAPI/appsettings.example.json @@ -25,5 +25,11 @@ "SecurityKey": "IH4xhBUKl3z51Gig5MFfg4kl0yLOulGk", "ExpirySeconds" : "3600" } + }, + + "SendGrid": { + "ApiKey": "ApiKey", + "FromEmail": "your_mail", + "FromName": "Resellio" } -} +} \ No newline at end of file