| | | 1 | | using System.Net.Http.Headers; |
| | | 2 | | using System.Text; |
| | | 3 | | using ArturRios.Output; |
| | | 4 | | using Microsoft.Extensions.Logging; |
| | | 5 | | |
| | | 6 | | namespace ArturRios.Messaging.Email; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Sends transactional e-mails through the <see href="https://www.mailgun.com/">Mailgun</see> HTTP API. |
| | | 10 | | /// </summary> |
| | | 11 | | /// <remarks> |
| | | 12 | | /// Configuration is read from environment variables at call time: |
| | | 13 | | /// <c>MAILGUN_API_KEY</c>, <c>MAILGUN_DOMAIN</c> and the optional <c>MAILGUN_API_VERSION</c>. |
| | | 14 | | /// </remarks> |
| | | 15 | | public class MailgunEmailService : IEmailService |
| | | 16 | | { |
| | | 17 | | private const string MailgunApiBaseUrl = "https://api.mailgun.net"; |
| | | 18 | | private const string MailgunMessagesEndpoint = "messages"; |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// The client used when the caller supplies none. One per process rather than one per service |
| | | 22 | | /// instance: a fresh <see cref="HttpClient"/> per instance holds its connections open after it goes |
| | | 23 | | /// out of scope, and nothing here ever disposed one. |
| | | 24 | | /// </summary> |
| | 0 | 25 | | private static readonly Lazy<HttpClient> SharedClient = new(() => new HttpClient(), isThreadSafe: true); |
| | | 26 | | |
| | | 27 | | private readonly ILogger<MailgunEmailService> _logger; |
| | | 28 | | private readonly HttpClient _httpClient; |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Name of the environment variable holding the Mailgun private API key. |
| | | 32 | | /// </summary> |
| | | 33 | | public const string ApiKeyVariable = "MAILGUN_API_KEY"; |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Name of the environment variable holding the Mailgun sending domain. |
| | | 37 | | /// </summary> |
| | | 38 | | public const string DomainVariable = "MAILGUN_DOMAIN"; |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Name of the environment variable holding the Mailgun API version to target. |
| | | 42 | | /// </summary> |
| | | 43 | | public const string ApiVersionVariable = "MAILGUN_API_VERSION"; |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// API version used when <see cref="ApiVersionVariable"/> is not set or is blank. |
| | | 47 | | /// </summary> |
| | | 48 | | public const string DefaultMailgunApiVersion = "v3"; |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Initializes a new instance of the <see cref="MailgunEmailService"/> class. |
| | | 52 | | /// </summary> |
| | | 53 | | /// <param name="logger">Logger used to record request and response details.</param> |
| | | 54 | | /// <param name="httpClient"> |
| | | 55 | | /// HTTP client used to reach the Mailgun API. When <see langword="null"/>, a client shared by the |
| | | 56 | | /// whole process is used. |
| | | 57 | | /// </param> |
| | | 58 | | /// <exception cref="ArgumentNullException"><paramref name="logger"/> is <see langword="null"/>.</exception> |
| | 25 | 59 | | public MailgunEmailService(ILogger<MailgunEmailService> logger, HttpClient? httpClient = null) |
| | | 60 | | { |
| | 25 | 61 | | ArgumentNullException.ThrowIfNull(logger); |
| | | 62 | | |
| | 24 | 63 | | _logger = logger; |
| | 24 | 64 | | _httpClient = httpClient ?? SharedClient.Value; |
| | 24 | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Sends a plain text e-mail through the Mailgun API. |
| | | 69 | | /// </summary> |
| | | 70 | | /// <param name="to">Recipient e-mail address.</param> |
| | | 71 | | /// <param name="subject">Subject line of the e-mail.</param> |
| | | 72 | | /// <param name="body">Plain text body of the e-mail.</param> |
| | | 73 | | /// <returns> |
| | | 74 | | /// A <see cref="ProcessOutput"/> without errors when Mailgun accepts the message, or carrying the status code and |
| | | 75 | | /// response body when it does not. A missing API key or sending domain is reported the same way rather |
| | | 76 | | /// than sent to Mailgun as an unauthenticated request. |
| | | 77 | | /// </returns> |
| | | 78 | | /// <remarks> |
| | | 79 | | /// The credential is attached to the request rather than to <see cref="HttpClient.DefaultRequestHeaders"/>. |
| | | 80 | | /// The client may be shared — the documented registration is <c>AddHttpClient</c>, which hands out a |
| | | 81 | | /// client this service does not own — and writing a credential onto its defaults both races with |
| | | 82 | | /// concurrent sends and leaves the Mailgun key attached to every later request that client makes. |
| | | 83 | | /// </remarks> |
| | | 84 | | public async Task<ProcessOutput> SendEmailAsync(string to, string subject, string body) |
| | | 85 | | { |
| | 24 | 86 | | var output = new ProcessOutput(); |
| | | 87 | | |
| | 24 | 88 | | var apiKey = Environment.GetEnvironmentVariable(ApiKeyVariable); |
| | 24 | 89 | | var domain = Environment.GetEnvironmentVariable(DomainVariable); |
| | | 90 | | |
| | 24 | 91 | | if (string.IsNullOrWhiteSpace(apiKey)) |
| | | 92 | | { |
| | 4 | 93 | | output.AddError($"Cannot send e-mail via Mailgun: the {ApiKeyVariable} environment variable is not set."); |
| | | 94 | | } |
| | | 95 | | |
| | 24 | 96 | | if (string.IsNullOrWhiteSpace(domain)) |
| | | 97 | | { |
| | 2 | 98 | | output.AddError($"Cannot send e-mail via Mailgun: the {DomainVariable} environment variable is not set."); |
| | | 99 | | } |
| | | 100 | | |
| | 24 | 101 | | if (!output.Success) |
| | | 102 | | { |
| | 5 | 103 | | _logger.LogError("Mailgun is not configured: {Errors}", string.Join(" ", output.Errors)); |
| | | 104 | | |
| | 5 | 105 | | return output; |
| | | 106 | | } |
| | | 107 | | |
| | 19 | 108 | | var apiVersion = GetApiVersion(); |
| | | 109 | | |
| | 19 | 110 | | using var request = new HttpRequestMessage( |
| | 19 | 111 | | HttpMethod.Post, |
| | 19 | 112 | | $"{MailgunApiBaseUrl}/{apiVersion}/{domain}/{MailgunMessagesEndpoint}") |
| | 19 | 113 | | { |
| | 19 | 114 | | Headers = |
| | 19 | 115 | | { |
| | 19 | 116 | | Authorization = new AuthenticationHeaderValue( |
| | 19 | 117 | | "Basic", |
| | 19 | 118 | | Convert.ToBase64String(Encoding.ASCII.GetBytes($"api:{apiKey}"))) |
| | 19 | 119 | | }, |
| | 19 | 120 | | Content = new FormUrlEncodedContent([ |
| | 19 | 121 | | new KeyValuePair<string, string>("from", $"Mailgun Sandbox <postmaster@{domain}>"), |
| | 19 | 122 | | new KeyValuePair<string, string>("to", to), |
| | 19 | 123 | | new KeyValuePair<string, string>("subject", subject), |
| | 19 | 124 | | new KeyValuePair<string, string>("text", body) |
| | 19 | 125 | | ]) |
| | 19 | 126 | | }; |
| | | 127 | | |
| | 19 | 128 | | _logger.LogInformation("Sending e-mail to {Recipient} via Mailgun...", to); |
| | | 129 | | |
| | 19 | 130 | | using var response = await _httpClient.SendAsync(request).ConfigureAwait(false); |
| | | 131 | | |
| | 19 | 132 | | var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false); |
| | | 133 | | |
| | 19 | 134 | | if (response.IsSuccessStatusCode) |
| | | 135 | | { |
| | 16 | 136 | | _logger.LogInformation("Mailgun accepted the message for {Recipient}.", to); |
| | | 137 | | |
| | 16 | 138 | | return output; |
| | | 139 | | } |
| | | 140 | | |
| | 3 | 141 | | _logger.LogError("Mailgun rejected the message for {Recipient}: {StatusCode} | {ResponseContent}", |
| | 3 | 142 | | to, response.StatusCode, responseContent); |
| | | 143 | | |
| | 3 | 144 | | output.AddError( |
| | 3 | 145 | | $"Failed to send e-mail via Mailgun. Status Code: {response.StatusCode} | Response: {responseContent}"); |
| | | 146 | | |
| | 3 | 147 | | return output; |
| | 24 | 148 | | } |
| | | 149 | | |
| | | 150 | | /// <summary> |
| | | 151 | | /// Reads the Mailgun API version from the environment. |
| | | 152 | | /// </summary> |
| | | 153 | | /// <returns> |
| | | 154 | | /// The value of <see cref="ApiVersionVariable"/>, or <see cref="DefaultMailgunApiVersion"/> when it is unset or bla |
| | | 155 | | /// </returns> |
| | | 156 | | private static string GetApiVersion() |
| | | 157 | | { |
| | 19 | 158 | | var apiVersion = Environment.GetEnvironmentVariable(ApiVersionVariable); |
| | | 159 | | |
| | 19 | 160 | | return string.IsNullOrWhiteSpace(apiVersion) ? DefaultMailgunApiVersion : apiVersion; |
| | | 161 | | } |
| | | 162 | | } |