Skip to content

feat: Add support for multiple Reply-Tos #1169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/SendGrid/Helpers/Mail/SendGridMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ public class SendGridMessage
[JsonProperty(PropertyName = "reply_to")]
public EmailAddress ReplyTo { get; set; }

/// <summary>
/// Gets or sets a list of objects of email objects containing the email address and name of the individuals who should receive responses to your email.
/// </summary>
[JsonProperty(PropertyName = "reply_to_list", IsReference = false)]
public List<EmailAddress> ReplyTos { get; set; }

/// <summary>
/// Add a recipient email.
/// </summary>
Expand Down Expand Up @@ -318,6 +324,50 @@ public void AddHeaders(Dictionary<string, string> headers, int personalizationIn
personalization.Headers.Union(headers).ToDictionary(pair => pair.Key, pair => pair.Value);
}

/// <summary>
/// Add a reply-to email.
/// </summary>
/// <param name="email">Specify the recipient's email.</param>
/// <param name="name">Specify the recipient's name.</param>
/// <exception cref="System.ArgumentNullException">Thrown when the email parameter is null or whitespace</exception>
public void AddReplyTo(string email, string name = null)
{
if (string.IsNullOrWhiteSpace(email))
throw new ArgumentNullException("email");

this.AddReplyTo(new EmailAddress(email, name));
}

/// <summary>
/// Add a reply-to email.
/// </summary>
/// <param name="email">An email recipient that may contain the recipient’s name, but must always contain the recipient’s email.</param>
/// <exception cref="System.ArgumentNullException">Thrown when the email parameter is null</exception>
public void AddReplyTo(EmailAddress email)
{
if (email == null)
throw new ArgumentNullException("email");

AddReplyTos(new List<EmailAddress> { email });
}

/// <summary>
/// Add reply-to recipients.
/// </summary>
/// <param name="emails">A list of reply-to recipients. Each email object within this array may contain the recipient’s name, but must always contain the recipient’s email.</param>
/// <exception cref="System.ArgumentNullException">Thrown when the emails parameter is null</exception>
/// <exception cref="System.InvalidOperationException">Thrown when the emails parameter is empty</exception>
public void AddReplyTos(List<EmailAddress> emails)
{
if (emails == null)
throw new ArgumentNullException("emails");
if (emails.Count == 0)
throw new InvalidOperationException("Sequence contains no elements");

if (ReplyTos == null) ReplyTos = new List<EmailAddress>();
ReplyTos.AddRange(emails);
}

/// <summary>
/// Add a substitution to the email.
/// You may not include more than 100 substitutions per personalization object, and the total collective size of your substitutions may not exceed 10,000 bytes per personalization object.
Expand Down
17 changes: 17 additions & 0 deletions tests/SendGrid.Tests/Integration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2217,6 +2217,23 @@ public void TestSetReplyTo()
Assert.Equal("{\"reply_to\":{\"name\":\"Test User2\",\"email\":\"[email protected]\"}}", msg.Serialize());
}

[Fact]
public void TestAddReplyTos()
{
var msg = new SendGridMessage();
msg.AddReplyTo("[email protected]", "Test User2");
Assert.Equal("{\"reply_to_list\":[{\"name\":\"Test User2\",\"email\":\"[email protected]\"}]}", msg.Serialize());

msg = new SendGridMessage();
msg.AddReplyTo("[email protected]", "Test User1");
msg.AddReplyTo("[email protected]", "Test User2");
Assert.Equal("{\"reply_to_list\":[{\"name\":\"Test User1\",\"email\":\"[email protected]\"},{\"name\":\"Test User2\",\"email\":\"[email protected]\"}]}", msg.Serialize());

msg = new SendGridMessage();
msg.AddReplyTos(new List<EmailAddress> { new EmailAddress() { Email = "[email protected]" }, new EmailAddress() { Email = "[email protected]", Name = "Test User2" } });
Assert.Equal("{\"reply_to_list\":[{\"email\":\"[email protected]\"},{\"name\":\"Test User2\",\"email\":\"[email protected]\"}]}", msg.Serialize());
}

[Fact]
public void TestSetGlobalSubject()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,19 @@ public void WithANullCarbonCopyThenAnExceptionIsThrown()
var sendGridMessage = MailHelper.CreateSingleEmail(new EmailAddress(), new EmailAddress(), string.Empty, string.Empty, string.Empty);
Assert.Throws<ArgumentNullException>(() => { sendGridMessage.AddCc(null, 0); });
}

[Fact]
public void WithAnEmptyListOfReplyTosThenAnExceptionIsThrown()
{
var sendGridMessage = MailHelper.CreateSingleEmail(new EmailAddress(), new EmailAddress(), string.Empty, string.Empty, string.Empty);
Assert.Throws<InvalidOperationException>(() => { sendGridMessage.AddReplyTos(new List<EmailAddress>()); });
}

[Fact]
public void WithANullListOfReplyTosThenAnExceptionIsThrown()
{
var sendGridMessage = MailHelper.CreateSingleEmail(new EmailAddress(), new EmailAddress(), string.Empty, string.Empty, string.Empty);
Assert.Throws<ArgumentNullException>(() => { sendGridMessage.AddReplyTos(null); });
}
}
}