Skip to content

Commit

Permalink
Merge pull request #508 from Niladri24dutta/SingleEmailToMultipleOver…
Browse files Browse the repository at this point in the history
…load

Single email to multiple recipients - Toggle display of recipients
  • Loading branch information
thinkingserious authored Aug 15, 2017
2 parents 45b79a5 + 7de2308 commit c3bc286
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/SendGrid/Helpers/Mail/MailHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,5 +146,48 @@ public static EmailAddress StringToEmailAddress(string rfc2822Email)
var name = match.Groups[NameGroup].Value.Trim();
return new EmailAddress(email, name);
}

/// <summary>
/// Send a single simple email to multiple recipients with option for displaying all the recipients present in "To" section of email
/// </summary>
/// <param name="from">An email object that may contain the recipient’s name, but must always contain the sender’s email.</param>
/// <param name="tos">A list of email objects that may contain the recipient’s name, but must always contain the recipient’s email.</param>
/// <param name="subject">The subject of your email. This may be overridden by SetGlobalSubject().</param>
/// <param name="plainTextContent">The text/plain content of the email body.</param>
/// <param name="htmlContent">The text/html content of the email body.</param>
/// <param name="showAllRecipients">Displays all the recipients present in the "To" section of email.The default value is false</param>
/// <returns>A SendGridMessage object.</returns>
public static SendGridMessage CreateSingleEmailToMultipleRecipients(
EmailAddress from,
List<EmailAddress> tos,
string subject,
string plainTextContent,
string htmlContent,
bool showAllRecipients = false)
{
var msg = new SendGridMessage();
if (showAllRecipients)
{
msg.SetFrom(from);
msg.SetGlobalSubject(subject);
if (!string.IsNullOrEmpty(plainTextContent))
{
msg.AddContent(MimeType.Text, plainTextContent);
}

if (!string.IsNullOrEmpty(htmlContent))
{
msg.AddContent(MimeType.Html, htmlContent);
}

msg.AddTos(tos);
}
else
{
msg = CreateSingleEmailToMultipleRecipients(from, tos, subject, plainTextContent, htmlContent);
}

return msg;
}
}
}
59 changes: 59 additions & 0 deletions tests/SendGrid.Tests/Integration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,65 @@ public void TestCreateSingleEmailToMultipleRecipients()
Assert.True(msg5.Serialize() == "{\"from\":{\"name\":\"Example User\",\"email\":\"[email protected]\"},\"subject\":\"Test Subject\",\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]}],\"content\":[{\"type\":\"text/plain\",\"value\":\"Plain Text Content\"}]}");
}

[Fact]
public void TestCreateSingleEmailToMultipleRecipientsToggleRecipientDisplay()
{
var emails = new List<EmailAddress>
{
new EmailAddress("[email protected]"),
new EmailAddress("[email protected]"),
new EmailAddress("[email protected]")
};
var msg = MailHelper.CreateSingleEmailToMultipleRecipients(new EmailAddress("[email protected]", "Example User"),
emails,
"Test Subject",
"Plain Text Content",
"HTML Content"
);
Assert.True(msg.Serialize() == "{\"from\":{\"name\":\"Example User\",\"email\":\"[email protected]\"},\"subject\":\"Test Subject\",\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]}],\"content\":[{\"type\":\"text/plain\",\"value\":\"Plain Text Content\"},{\"type\":\"text/html\",\"value\":\"HTML Content\"}]}");

var msg2 = MailHelper.CreateSingleEmailToMultipleRecipients(new EmailAddress("[email protected]", "Example User"),
emails,
"Test Subject",
null,
"HTML Content"
);
Assert.True(msg2.Serialize() == "{\"from\":{\"name\":\"Example User\",\"email\":\"[email protected]\"},\"subject\":\"Test Subject\",\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]}],\"content\":[{\"type\":\"text/html\",\"value\":\"HTML Content\"}]}");

var msg3 = MailHelper.CreateSingleEmailToMultipleRecipients(new EmailAddress("[email protected]", "Example User"),
emails,
"Test Subject",
"Plain Text Content",
null
);
Assert.True(msg3.Serialize() == "{\"from\":{\"name\":\"Example User\",\"email\":\"[email protected]\"},\"subject\":\"Test Subject\",\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]}],\"content\":[{\"type\":\"text/plain\",\"value\":\"Plain Text Content\"}]}");

var msg4 = MailHelper.CreateSingleEmailToMultipleRecipients(new EmailAddress("[email protected]", "Example User"),
emails,
"Test Subject",
"",
"HTML Content"
);
Assert.True(msg4.Serialize() == "{\"from\":{\"name\":\"Example User\",\"email\":\"[email protected]\"},\"subject\":\"Test Subject\",\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]}],\"content\":[{\"type\":\"text/html\",\"value\":\"HTML Content\"}]}");

var msg5 = MailHelper.CreateSingleEmailToMultipleRecipients(new EmailAddress("[email protected]", "Example User"),
emails,
"Test Subject",
"Plain Text Content",
""
);
Assert.True(msg5.Serialize() == "{\"from\":{\"name\":\"Example User\",\"email\":\"[email protected]\"},\"subject\":\"Test Subject\",\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]},{\"to\":[{\"email\":\"[email protected]\"}]}],\"content\":[{\"type\":\"text/plain\",\"value\":\"Plain Text Content\"}]}");

var msg6 = MailHelper.CreateSingleEmailToMultipleRecipients(new EmailAddress("[email protected]", "Example User"),
emails,
"Test Subject",
"Plain Text Content",
"HTML Content",
true
);
Assert.True(msg6.Serialize() == "{\"from\":{\"name\":\"Example User\",\"email\":\"[email protected]\"},\"subject\":\"Test Subject\",\"personalizations\":[{\"to\":[{\"email\":\"[email protected]\"},{\"email\":\"[email protected]\"},{\"email\":\"[email protected]\"}]}],\"content\":[{\"type\":\"text/plain\",\"value\":\"Plain Text Content\"},{\"type\":\"text/html\",\"value\":\"HTML Content\"}]}");
}

[Fact]
public void TestCreateMultipleEmailsToMultipleRecipients()
{
Expand Down

0 comments on commit c3bc286

Please sign in to comment.