Skip to content

Commit

Permalink
mailjet fix
Browse files Browse the repository at this point in the history
  • Loading branch information
coronabytes committed Feb 7, 2024
1 parent eb3209f commit 5fda59b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
22 changes: 16 additions & 6 deletions Core.Email.Provider.Mailjet/MailjetProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ public MailjetProvider(IConfiguration configuration, [ServiceKey] string key)
public async Task<List<CoreEmailStatus>> SendBatchAsync(List<CoreEmailMessage> messages,
CancellationToken cancellationToken = default)
{
var res = await _mailjet.SendTransactionalEmailsAsync(messages.Select(x => new TransactionalEmail
var batch = messages.Select(x => new TransactionalEmail
{
CustomID = x.Id.ToString("N"),
To = x.To.Select(y => new SendContact(y)).ToList(),
From = new SendContact(x.From),
Cc = x.Cc.Select(y => new SendContact(y)).ToList(),
Bcc = x.Bcc.Select(y => new SendContact(y)).ToList(),
ReplyTo = string.IsNullOrEmpty(x.ReplyTo) ? null : new SendContact(x.ReplyTo),
Expand All @@ -37,13 +38,22 @@ public async Task<List<CoreEmailStatus>> SendBatchAsync(List<CoreEmailMessage> m
Attachments = x.Attachments
.Select(y => new Attachment(y.Name, y.ContentType, Convert.ToBase64String(y.Content)))
.ToList()
})).ConfigureAwait(false);
}).ToList();

var res = await _mailjet.SendTransactionalEmailsAsync(batch).ConfigureAwait(false);

return res.Messages.Select(x => new CoreEmailStatus
return res.Messages.Select(x =>
{
Id = Guid.Parse(x.CustomID),
IsSuccess = x.Errors.Count == 0,
Error = string.Join("\n", x.Errors.Select(y => y.ErrorMessage))
Guid.TryParse(x.CustomID, out var id);
var status = new CoreEmailStatus
{
Id = id,
IsSuccess = x.Errors == null || x.Errors.Count == 0,
Error = x.Errors != null ? string.Join("\n", x.Errors.Select(y => y.ErrorMessage)) : string.Empty
};
return status;
}).ToList();
}

Expand Down
6 changes: 3 additions & 3 deletions Core.Email.Tests/EmailTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async Task Test1()
serviceCollection.AddSmtpProvider("SMTP");
serviceCollection.AddPostmarkProvider("Postmark");
serviceCollection.AddSendGridProvider("SendGrid");
serviceCollection.AddMailjetProvider("MailJet");
serviceCollection.AddMailjetProvider("Mailjet");
serviceCollection.AddSimpleEmailServiceProvider("SES");
var serviceProvider = serviceCollection.BuildServiceProvider();

Expand All @@ -40,8 +40,8 @@ public async Task Test1()
{
To = [to!],
From = from!,
Subject = "Transactional Mail Test 5",
TextBody = "Transactional Mail Test 5",
Subject = "Transactional Mail Test 7",
TextBody = "Transactional Mail Test 7",
Attachments =
[
new CoreEmailAttachment
Expand Down

0 comments on commit 5fda59b

Please sign in to comment.