common: simplify email sender api

This commit is contained in:
Abdullah Atta
2026-07-08 09:05:58 +05:00
parent 99489b9b4c
commit a1dbd3f8b8
4 changed files with 26 additions and 13 deletions
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Net.Mail;
using System.Threading.Tasks;
using MimeKit;
using MimeKit.Cryptography;
@@ -11,7 +12,7 @@ namespace Streetwriters.Common.Interfaces
Task SendEmailAsync(
string email,
EmailTemplate template,
IClient client,
MailAddress from,
GnuPGContext? gpgContext = null,
Dictionary<string, byte[]>? attachments = null
);
@@ -93,6 +93,9 @@ namespace Streetwriters.Common.Models
[JsonPropertyName("trialsAvailed")]
public SubscriptionPlan[]? TrialsAvailed { get; set; }
[JsonPropertyName("extensionsAvailed")]
public SubscriptionExtension[]? ExtensionsAvailed { get; set; }
[JsonPropertyName("updatedAt")]
public long UpdatedAt { get; set; }
@@ -104,4 +107,16 @@ namespace Streetwriters.Common.Models
[JsonPropertyName("status")]
public SubscriptionStatus Status { get; set; }
}
public class SubscriptionExtension
{
[JsonPropertyName("timestamp")]
public required long Timestamp { get; set; }
[JsonPropertyName("expiry")]
public required long ExpiryDate { get; set; }
[JsonPropertyName("type")]
public required string Type { get; set; }
}
}
+4 -7
View File
@@ -27,7 +27,7 @@ namespace Streetwriters.Common.Services
public async Task SendEmailAsync(
string email,
EmailTemplate template,
IClient client,
System.Net.Mail.MailAddress from,
GnuPGContext? gpgContext = null,
Dictionary<string, byte[]>? attachments = null
)
@@ -55,8 +55,7 @@ namespace Streetwriters.Common.Services
);
var message = new MimeMessage();
var sender = new MailboxAddress(client.SenderName, client.SenderEmail);
message.From.Add(sender);
message.From.Add(new MailboxAddress(from.DisplayName, from.Address));
message.To.Add(new MailboxAddress("", email));
message.Subject = await Template.Parse(template.Subject).RenderAsync(template.Data);
@@ -65,8 +64,7 @@ namespace Streetwriters.Common.Services
message.Body = await GetEmailBodyAsync(
template,
client,
sender,
new MailboxAddress(from.DisplayName, from.Address),
gpgContext,
attachments
);
@@ -76,7 +74,6 @@ namespace Streetwriters.Common.Services
private async Task<MimeEntity> GetEmailBodyAsync(
EmailTemplate template,
IClient client,
MailboxAddress sender,
GnuPGContext? gpgContext = null,
Dictionary<string, byte[]>? attachments = null
@@ -107,7 +104,7 @@ namespace Streetwriters.Common.Services
}
outputStream.Seek(0, SeekOrigin.Begin);
builder.Attachments.Add(
$"{client.Id}_pub.asc",
$"pub.asc",
Encoding.ASCII.GetBytes(
Encoding.ASCII.GetString(outputStream.ToArray())
)
@@ -104,7 +104,7 @@ namespace Streetwriters.Identity.Services
Subject = Email2FATemplate.Subject,
Data = new { app_name = client.Name, code },
};
await EmailSender.SendEmailAsync(email, template, client, NNGnuPGContext);
await EmailSender.SendEmailAsync(email, template, new System.Net.Mail.MailAddress(client.SenderEmail, client.SenderName), NNGnuPGContext);
}
public async Task SendConfirmationEmailAsync(
@@ -120,7 +120,7 @@ namespace Streetwriters.Identity.Services
Subject = ConfirmEmailTemplate.Subject,
Data = new { app_name = client.Name, confirm_link = callbackUrl },
};
await EmailSender.SendEmailAsync(email, template, client, NNGnuPGContext);
await EmailSender.SendEmailAsync(email, template, new System.Net.Mail.MailAddress(client.SenderEmail, client.SenderName), NNGnuPGContext);
}
public async Task SendChangeEmailConfirmationAsync(
@@ -136,7 +136,7 @@ namespace Streetwriters.Identity.Services
Subject = ConfirmChangeEmailTemplate.Subject,
Data = new { app_name = client.Name, code },
};
await EmailSender.SendEmailAsync(email, template, client, NNGnuPGContext);
await EmailSender.SendEmailAsync(email, template, new System.Net.Mail.MailAddress(client.SenderEmail, client.SenderName), NNGnuPGContext);
}
public async Task SendPasswordResetEmailAsync(
@@ -152,7 +152,7 @@ namespace Streetwriters.Identity.Services
Subject = PasswordResetEmailTemplate.Subject,
Data = new { app_name = client.Name, reset_link = callbackUrl },
};
await EmailSender.SendEmailAsync(email, template, client, NNGnuPGContext);
await EmailSender.SendEmailAsync(email, template, new System.Net.Mail.MailAddress(client.SenderEmail, client.SenderName), NNGnuPGContext);
}
public async Task SendFailedLoginAlertAsync(string email, string deviceInfo, IClient client)
@@ -168,7 +168,7 @@ namespace Streetwriters.Identity.Services
device_info = deviceInfo.Replace("\n", "<br>"),
},
};
await EmailSender.SendEmailAsync(email, template, client, NNGnuPGContext);
await EmailSender.SendEmailAsync(email, template, new System.Net.Mail.MailAddress(client.SenderEmail, client.SenderName), NNGnuPGContext);
}
}