Skip to content

Commit

Permalink
fix for Domain to be taken from the appsettings.json
Browse files Browse the repository at this point in the history
  • Loading branch information
mihaicm93 committed Jun 19, 2024
1 parent 281df50 commit dd82252
Showing 1 changed file with 3 additions and 16 deletions.
19 changes: 3 additions & 16 deletions Components/Account/EmailSender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public async Task SendEmailAsync(string toEmail, string subject, string message)
{
try
{
// Check if Mailgun credentials are configured
if (string.IsNullOrEmpty(_options.ApiKey) || string.IsNullOrEmpty(_options.Domain))
{
throw new Exception("Mailgun ApiKey or Domain is not set");
Expand All @@ -54,30 +53,18 @@ public async Task SendEmailAsync(string toEmail, string subject, string message)

// Create RestRequest for sending message
var request = new RestRequest("messages", Method.Post);
request.AddParameter("from", "SIT Controller <noreply@mg.sit.publicvm.com>");
request.AddParameter("from", $"SIT Controller <noreply@{_options.Domain}>");
request.AddParameter("to", toEmail);
request.AddParameter("subject", subject);
request.AddParameter("html", message); // Use 'html' instead of 'text' if sending HTML content
request.AddParameter("html", message);

// Log Mailgun API Key and Domain
_logger.LogInformation("Mailgun API Key: {ApiKey}", _options.ApiKey);
_logger.LogInformation("Mailgun Domain: {Domain}", _options.Domain);

// Set Authorization header
request.AddHeader("Authorization", "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes($"api:{_options.ApiKey}")));

// Log request details
_logger.LogInformation("Sending email request to Mailgun API");

// Execute the request asynchronously
var response = await client.ExecuteAsync(request);

// Log response details
_logger.LogInformation("Received response from Mailgun API");
_logger.LogInformation("Response Status Code: {StatusCode}", response.StatusCode);
_logger.LogInformation("Response Content: {Content}", response.Content);
_logger.LogInformation("Response Error Message: {ErrorMessage}", response.ErrorMessage);
_logger.LogInformation("Response Headers: {Headers}", response.Headers);

// Handle response status
if (response.IsSuccessful)
Expand All @@ -93,7 +80,7 @@ public async Task SendEmailAsync(string toEmail, string subject, string message)
catch (Exception ex)
{
_logger.LogError(ex, "Exception occurred while sending email to {EmailAddress}", toEmail);
throw; // Re-throw the exception to propagate it further if needed
throw;
}
}
}
Expand Down

0 comments on commit dd82252

Please sign in to comment.