|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text.RegularExpressions; |
| 4 | +using Microsoft.Extensions.Hosting; |
| 5 | +using Microsoft.Extensions.Logging; |
| 6 | +using Microsoft.Extensions.Options; |
| 7 | +using SendGrid.Helpers.Mail; |
| 8 | + |
| 9 | +namespace ICG.NetCore.Utilities.Email.SendGrid |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// The service takes an incoming request and build the proper SendGrid message structure for composing the messages |
| 13 | + /// </summary> |
| 14 | + public interface ISendGridMessageBuilder |
| 15 | + { |
| 16 | + /// <summary> |
| 17 | + /// Creates a simple message for sending |
| 18 | + /// </summary> |
| 19 | + /// <param name="from">Who the message is from</param> |
| 20 | + /// <param name="to">Who the message is to</param> |
| 21 | + /// <param name="subject">The subject of the message</param> |
| 22 | + /// <param name="bodyHtml">The Email's HTML content</param> |
| 23 | + /// <returns></returns> |
| 24 | + SendGridMessage CreateMessage(string from, string to, string subject, string bodyHtml); |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Creates a simple message for sending with a custom template |
| 28 | + /// </summary> |
| 29 | + /// <param name="from">Who the message is from</param> |
| 30 | + /// <param name="to">Who the message is to</param> |
| 31 | + /// <param name="cc">An optional listing of CC addresses</param> |
| 32 | + /// <param name="subject">The subject of the message</param> |
| 33 | + /// <param name="bodyHtml">The Email's HTML content</param> |
| 34 | + /// <param name="templateName">The name of the template to use</param> |
| 35 | + /// <returns></returns> |
| 36 | + SendGridMessage CreateMessage(string from, string to, IEnumerable<string> cc, string subject, string bodyHtml, |
| 37 | + string templateName = ""); |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Creates a simple message for sending with a custom template and attachment |
| 41 | + /// </summary> |
| 42 | + /// <param name="from">Who the message is from</param> |
| 43 | + /// <param name="to">Who the message is to</param> |
| 44 | + /// <param name="cc">An optional listing of CC addresses</param> |
| 45 | + /// <param name="fileContent">The content of the attachment in bytes</param> |
| 46 | + /// <param name="fileName">The desired name for the file attachment</param> |
| 47 | + /// <param name="subject">The subject of the message</param> |
| 48 | + /// <param name="bodyHtml">The Email's HTML content</param> |
| 49 | + /// <param name="templateName">The name of the template to use</param> |
| 50 | + SendGridMessage CreateMessageWithAttachment(string from, string to, IEnumerable<string> cc, |
| 51 | + byte[] fileContent, string fileName, string subject, string bodyHtml, string templateName = ""); |
| 52 | + } |
| 53 | + |
| 54 | + /// <inheritdoc cref="ISendGridMessageBuilder"/> |
| 55 | + public class SendGridMessageBuilder : ISendGridMessageBuilder |
| 56 | + { |
| 57 | + private readonly IHostingEnvironment _hostingEnvironment; |
| 58 | + private readonly IEmailTemplateFactory _emailTemplateFactory; |
| 59 | + private readonly SendGridServiceOptions _serviceOptions; |
| 60 | + private readonly ILogger _logger; |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Default constructor |
| 64 | + /// </summary> |
| 65 | + /// <param name="hostingEnvironment"></param> |
| 66 | + /// <param name="emailTemplateFactory"></param> |
| 67 | + /// <param name="options"></param> |
| 68 | + /// <param name="loggerFactory"></param> |
| 69 | + public SendGridMessageBuilder(IHostingEnvironment hostingEnvironment, IEmailTemplateFactory emailTemplateFactory, |
| 70 | + IOptions<SendGridServiceOptions> options, ILoggerFactory loggerFactory) |
| 71 | + { |
| 72 | + _hostingEnvironment = hostingEnvironment; |
| 73 | + _emailTemplateFactory = emailTemplateFactory; |
| 74 | + _serviceOptions = options.Value; |
| 75 | + _logger = loggerFactory.CreateLogger<SendGridMessageBuilder>(); |
| 76 | + } |
| 77 | + |
| 78 | + /// <inheritdoc /> |
| 79 | + public SendGridMessage CreateMessage(string from, string to, string subject, string bodyHtml) |
| 80 | + { |
| 81 | + return CreateMessage(from, to, null, subject, bodyHtml); |
| 82 | + } |
| 83 | + |
| 84 | + /// <inheritdoc /> |
| 85 | + public SendGridMessage CreateMessage(string from, string to, IEnumerable<string> cc, string subject, string bodyHtml, string templateName = "") |
| 86 | + { |
| 87 | + //Validate inputs |
| 88 | + if (string.IsNullOrEmpty(from)) |
| 89 | + throw new ArgumentNullException(nameof(from)); |
| 90 | + if (string.IsNullOrEmpty(to)) |
| 91 | + throw new ArgumentNullException(nameof(to)); |
| 92 | + if (string.IsNullOrEmpty(subject)) |
| 93 | + throw new ArgumentNullException(nameof(subject)); |
| 94 | + if (string.IsNullOrEmpty(bodyHtml)) |
| 95 | + throw new ArgumentNullException(nameof(bodyHtml)); |
| 96 | + |
| 97 | + //Get addresses |
| 98 | + var fromAddress = new EmailAddress(from); |
| 99 | + var recipients = new List<EmailAddress> {new EmailAddress(to)}; |
| 100 | + if (cc != null) |
| 101 | + { |
| 102 | + foreach (var item in cc) |
| 103 | + { |
| 104 | + try |
| 105 | + { |
| 106 | + recipients.Add(new EmailAddress(item)); |
| 107 | + } |
| 108 | + catch (Exception ex) |
| 109 | + { |
| 110 | + _logger.LogWarning(ex, $"Unable to add {item} to email copy list"); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + //Handle subjects |
| 116 | + if (_serviceOptions.AddEnvironmentSuffix && !_hostingEnvironment.IsProduction()) |
| 117 | + subject = $"{subject} ({_hostingEnvironment.EnvironmentName})"; |
| 118 | + |
| 119 | + //Perform templating |
| 120 | + if (_serviceOptions.AlwaysTemplateEmails && string.IsNullOrEmpty(templateName)) |
| 121 | + bodyHtml = _emailTemplateFactory.BuildEmailContent(subject, bodyHtml); |
| 122 | + else if (!string.IsNullOrEmpty(templateName)) |
| 123 | + bodyHtml = _emailTemplateFactory.BuildEmailContent(subject, bodyHtml, |
| 124 | + templateName: templateName); |
| 125 | + |
| 126 | + //Get body text |
| 127 | + var plainTextBody = Regex.Replace(bodyHtml, "<[^>]*>", ""); |
| 128 | + |
| 129 | + //Build message |
| 130 | + if (recipients.Count == 1) |
| 131 | + return MailHelper.CreateSingleEmail(fromAddress, recipients[0], subject, plainTextBody, bodyHtml); |
| 132 | + |
| 133 | + return MailHelper.CreateSingleEmailToMultipleRecipients(fromAddress, recipients, subject, plainTextBody, |
| 134 | + bodyHtml); |
| 135 | + } |
| 136 | + |
| 137 | + public SendGridMessage CreateMessageWithAttachment(string from, string to, IEnumerable<string> cc, |
| 138 | + byte[] fileContent, string fileName, string subject, string bodyHtml, string templateName = "") |
| 139 | + { |
| 140 | + //Build the basic message |
| 141 | + var toSend = CreateMessage(from, to, cc, subject, bodyHtml, templateName); |
| 142 | + |
| 143 | + //Attach file |
| 144 | + toSend.Attachments = new List<Attachment> |
| 145 | + { |
| 146 | + new Attachment |
| 147 | + { |
| 148 | + Content = Convert.ToBase64String(fileContent), |
| 149 | + Filename = fileName, |
| 150 | + Disposition = "attachment" |
| 151 | + } |
| 152 | + }; |
| 153 | + return toSend; |
| 154 | + } |
| 155 | + |
| 156 | + } |
| 157 | +} |
0 commit comments