Skip to content

Commit

Permalink
Sendgrid: CC and BCC
Browse files Browse the repository at this point in the history
  • Loading branch information
hbulens committed Sep 5, 2024
1 parent 485089f commit 94afdf8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Facteur.SendGrid/Facteur.SendGrid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageTags>Facteur;Email;Mail;SendGrid</PackageTags>
<Description>Send emails using the SendGrid endpoint</Description>
<Version>2.1.0</Version>
<Version>2.1.1</Version>
<Authors>Dime Software</Authors>
<Company>Dime Software</Company>
<Product>Facteur</Product>
Expand Down
12 changes: 10 additions & 2 deletions src/Facteur.SendGrid/SendGridBaseMailer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected SendGridBaseMailer(string apiKey)
/// </summary>
/// <param name="request">The subject.</param>
/// <returns></returns>
public virtual Task SendMailAsync(EmailRequest request)
public virtual async Task SendMailAsync(EmailRequest request)
{
SendGridClient client = new(ApiKey);
EmailAddress sendFrom = request.From.ToEmailAddress();
Expand All @@ -42,7 +42,15 @@ public virtual Task SendMailAsync(EmailRequest request)
.CreateSingleEmailToMultipleRecipients(sendFrom, sendTo, request.Subject, null, request.Body)
.AddAttachments(request);

return client.SendEmailAsync(message);
IEnumerable<string> sendCc = request.Cc.Where(x => !request.To.Contains(x));
foreach (string cc in sendCc)
message.AddCc(cc);

IEnumerable<string> sendBcc = request.Bcc.Where(x => !request.To.Contains(x) && !request.Cc.Contains(x));
foreach (string bcc in sendBcc)
message.AddBcc(bcc);

Response res = await client.SendEmailAsync(message);
}
}
}
12 changes: 10 additions & 2 deletions src/Facteur.SendGrid/SendGridPlainTextMailer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,22 @@ public SendGridPlainTextMailer(string key, IEmailComposer composer = null) : bas
/// </summary>
/// <param name="request">The subject</param>
/// <returns>An instance of <see cref="System.Threading.Tasks.Task"/></returns>
public override Task SendMailAsync(EmailRequest request)
public override async Task SendMailAsync(EmailRequest request)
{
SendGridClient client = new(ApiKey);
EmailAddress sendFrom = request.From.ToEmailAddress();
List<EmailAddress> sendTo = request.To.Select(x => new EmailAddress(x)).ToList();
SendGridMessage message = MailHelper.CreateSingleEmailToMultipleRecipients(sendFrom, sendTo, request.Subject, request.Body, null);

return client.SendEmailAsync(message);
IEnumerable<string> sendCc = request.Cc.Where(x => !request.To.Contains(x));
foreach (string cc in sendCc)
message.AddCc(cc);

IEnumerable<string> sendBcc = request.Bcc.Where(x => !request.To.Contains(x) && !request.Cc.Contains(x));
foreach (string bcc in sendBcc)
message.AddBcc(bcc);

Response res = await client.SendEmailAsync(message);
}

public async Task SendMailAsync(Func<IEmailComposer, Task<EmailRequest>> compose)
Expand Down

0 comments on commit 94afdf8

Please sign in to comment.