Skip to content

Commit

Permalink
Merge pull request #13 from IowaComputerGurus/feature/api-fixes
Browse files Browse the repository at this point in the history
Updated to actually CC emails #11
  • Loading branch information
mitchelsellers authored Jun 14, 2022
2 parents de66c5b + 973fc30 commit de0ca9f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/NetCore.Utilities.Email.SendGrid.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30804.86
# Visual Studio Version 17
VisualStudioVersion = 17.2.32516.85
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NetCore.Utilities.Email.SendGrid", "NetCore.Utilities.Email.SendGrid\NetCore.Utilities.Email.SendGrid.csproj", "{48F8F9BE-5579-4B92-AF46-194F755ECAB0}"
EndProject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="SendGrid" Version="9.25.1" />
<PackageReference Include="SendGrid" Version="9.28.0" />
</ItemGroup>

</Project>
40 changes: 23 additions & 17 deletions src/NetCore.Utilities.Email.SendGrid/SendGridMessageBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,7 @@ public SendGridMessage CreateMessage(string from, string fromName, string to, IE
if (!string.IsNullOrEmpty(fromName))
fromAddress.Name = fromName;
var recipients = new List<EmailAddress> {new EmailAddress(to)};
if (cc != null)
{
foreach (var item in cc)
{
try
{
recipients.Add(new EmailAddress(item));
}
catch (Exception ex)
{
_logger.LogWarning(ex, $"Unable to add {item} to email copy list");
}
}
}


//Handle subjects
if (_serviceOptions.AddEnvironmentSuffix && !_hostingEnvironment.IsProduction())
Expand All @@ -115,11 +102,30 @@ public SendGridMessage CreateMessage(string from, string fromName, string to, IE
var plainTextBody = Regex.Replace(bodyHtml, "<[^>]*>", "");

//Build message
SendGridMessage message = null;
if (recipients.Count == 1)
return MailHelper.CreateSingleEmail(fromAddress, recipients[0], subject, plainTextBody, bodyHtml);
message = MailHelper.CreateSingleEmail(fromAddress, recipients[0], subject, plainTextBody, bodyHtml);
else
message = MailHelper.CreateSingleEmailToMultipleRecipients(fromAddress, recipients, subject, plainTextBody, bodyHtml);

//Add CC later based on limitation of SendGrid API
if (cc != null)
{
foreach (var item in cc)
{
try
{
var toAdd = new EmailAddress(item);
message.AddCc(toAdd);
}
catch (Exception ex)
{
_logger.LogWarning(ex, $"Unable to add {item} to email copy list");
}
}
}

return MailHelper.CreateSingleEmailToMultipleRecipients(fromAddress, recipients, subject, plainTextBody,
bodyHtml);
return message;
}

/// <inheritdoc />
Expand Down

0 comments on commit de0ca9f

Please sign in to comment.