ticket-system/source/ticketAPI/api/Services/EmailService.cs
Tara Wilson 84431ede36 Adding Emailing
Adding Email Verification on Patron Form
Addressing #1 - tickets will span next to ticket form
2024-12-18 15:59:36 -05:00

71 lines
3.4 KiB
C#

using System.Net;
using System.Net.Mail;
using System.Text;
using api.Interfaces;
using models.Core;
using models.Response;
namespace api.Services;
public class EmailService(IConfiguration config) : IEmailService
{
public void SendEmail(Ticket ticket, EventDetails @event)
{
using var client = new SmtpClient(config.GetSection("Email:Server").Value);
client.UseDefaultCredentials = false;
var auth = new NetworkCredential(config.GetSection("Email:UserName").Value,
config.GetSection("Email:Password").Value);
client.Credentials = auth;
var from = new MailAddress(config.GetSection("Email:From").Value);
var to = new MailAddress(ticket.Patron.Email);
var emailMessage = new MailMessage(from, to);
emailMessage.ReplyToList.Add(from);
emailMessage.Subject = $"Tickets - {@event.Name}";
emailMessage.SubjectEncoding = Encoding.UTF8;
emailMessage.IsBodyHtml = true;
emailMessage.Body = GenerateTicketBody(ticket, @event);
client.Send(emailMessage);
}
private string GenerateTicketBody(Ticket ticket, EventDetails @event)
{
var sb = new StringBuilder();
const string cardStyle = "margin: 15px; padding: 5px; backdrop-filter: blur(25px) saturate(112%); -webkit-backdrop-filter: blur(25px) saturate(112%); background-color: rgba(255, 255, 255, 0.11); border-radius: 12px; border: 1px solid rgba(255, 255, 255, 0.125); width: fit-content;";
const string columnStyle = "display: flex; flex-direction: column; justify-content: center;";
const string rowStyle = "display: flex; flex-direction: row; justify-content: center; align-items: baseline;";
const string qrCodeStyle = "height: 150px; width: 150px; padding: 10px;";
const string imagePath = "Assets/pso-logo.png";
sb.AppendLine($"<div #ticket style=\"{cardStyle} {columnStyle}\">");
sb.AppendLine($"<div #talentLogo style=\"{cardStyle} {rowStyle}\">");
sb.AppendLine($"<img src=\"{imagePath}\" width=\"250\" height=\"100\" alt=\"Parma Symphony Orchestra Logo\"/>");
sb.AppendLine("</div>"); //closes #talentLogo
sb.AppendLine($"<div #qrCode style=\"{rowStyle}\">");
sb.AppendLine($"<img style=\"{qrCodeStyle}\" alt=\"QR Code\" src=\"data:image/png;base64,{ticket.QrCode}\"/>");
sb.AppendLine("</div>"); //closes #qrCode
sb.AppendLine($"<div #venue style=\"{rowStyle}\">");
sb.AppendLine($"<div style=\"{cardStyle}\">");
sb.AppendLine($"<span style=\"{rowStyle}\">{@event.Talent.Name}</span>");
sb.AppendLine($"<span style=\"{rowStyle}\">{@event.Name}</span>");
sb.AppendLine($"<span style=\"{rowStyle}\">{@event.Description}</span>");
sb.AppendLine($"<span style=\"{rowStyle}\">{@event.Date.ToString("F")}</span>");
sb.AppendLine($"<span style=\"{rowStyle}\">{@event.Venue.Name}</span>");
sb.AppendLine($"<span style=\"{rowStyle}\">{@event.Venue.Description}</span>");
sb.AppendLine($"<span style=\"{rowStyle}\">{@event.Venue.AddressOne}</span>");
sb.AppendLine($"<span style=\"{rowStyle}\">{@event.Venue.City} {@event.Venue.State} {@event.Venue.Zip}</span>");
sb.AppendLine("</div>"); //closes #venue nested card
sb.AppendLine("</div>"); //closes #venue
sb.AppendLine("</div>"); //closes #ticket
return sb.ToString();
}
}