Skip to content

Commit 9385d27

Browse files
feat: Add support for multiple Reply-Tos (#1169)
1 parent d5aca0f commit 9385d27

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

src/SendGrid/Helpers/Mail/SendGridMessage.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,12 @@ public class SendGridMessage
138138
[JsonProperty(PropertyName = "reply_to")]
139139
public EmailAddress ReplyTo { get; set; }
140140

141+
/// <summary>
142+
/// 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.
143+
/// </summary>
144+
[JsonProperty(PropertyName = "reply_to_list", IsReference = false)]
145+
public List<EmailAddress> ReplyTos { get; set; }
146+
141147
/// <summary>
142148
/// Add a recipient email.
143149
/// </summary>
@@ -318,6 +324,50 @@ public void AddHeaders(Dictionary<string, string> headers, int personalizationIn
318324
personalization.Headers.Union(headers).ToDictionary(pair => pair.Key, pair => pair.Value);
319325
}
320326

327+
/// <summary>
328+
/// Add a reply-to email.
329+
/// </summary>
330+
/// <param name="email">Specify the recipient's email.</param>
331+
/// <param name="name">Specify the recipient's name.</param>
332+
/// <exception cref="System.ArgumentNullException">Thrown when the email parameter is null or whitespace</exception>
333+
public void AddReplyTo(string email, string name = null)
334+
{
335+
if (string.IsNullOrWhiteSpace(email))
336+
throw new ArgumentNullException("email");
337+
338+
this.AddReplyTo(new EmailAddress(email, name));
339+
}
340+
341+
/// <summary>
342+
/// Add a reply-to email.
343+
/// </summary>
344+
/// <param name="email">An email recipient that may contain the recipient’s name, but must always contain the recipient’s email.</param>
345+
/// <exception cref="System.ArgumentNullException">Thrown when the email parameter is null</exception>
346+
public void AddReplyTo(EmailAddress email)
347+
{
348+
if (email == null)
349+
throw new ArgumentNullException("email");
350+
351+
AddReplyTos(new List<EmailAddress> { email });
352+
}
353+
354+
/// <summary>
355+
/// Add reply-to recipients.
356+
/// </summary>
357+
/// <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>
358+
/// <exception cref="System.ArgumentNullException">Thrown when the emails parameter is null</exception>
359+
/// <exception cref="System.InvalidOperationException">Thrown when the emails parameter is empty</exception>
360+
public void AddReplyTos(List<EmailAddress> emails)
361+
{
362+
if (emails == null)
363+
throw new ArgumentNullException("emails");
364+
if (emails.Count == 0)
365+
throw new InvalidOperationException("Sequence contains no elements");
366+
367+
if (ReplyTos == null) ReplyTos = new List<EmailAddress>();
368+
ReplyTos.AddRange(emails);
369+
}
370+
321371
/// <summary>
322372
/// Add a substitution to the email.
323373
/// 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.

tests/SendGrid.Tests/Integration.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2217,6 +2217,23 @@ public void TestSetReplyTo()
22172217
Assert.Equal("{\"reply_to\":{\"name\":\"Test User2\",\"email\":\"[email protected]\"}}", msg.Serialize());
22182218
}
22192219

2220+
[Fact]
2221+
public void TestAddReplyTos()
2222+
{
2223+
var msg = new SendGridMessage();
2224+
msg.AddReplyTo("[email protected]", "Test User2");
2225+
Assert.Equal("{\"reply_to_list\":[{\"name\":\"Test User2\",\"email\":\"[email protected]\"}]}", msg.Serialize());
2226+
2227+
msg = new SendGridMessage();
2228+
msg.AddReplyTo("[email protected]", "Test User1");
2229+
msg.AddReplyTo("[email protected]", "Test User2");
2230+
Assert.Equal("{\"reply_to_list\":[{\"name\":\"Test User1\",\"email\":\"[email protected]\"},{\"name\":\"Test User2\",\"email\":\"[email protected]\"}]}", msg.Serialize());
2231+
2232+
msg = new SendGridMessage();
2233+
msg.AddReplyTos(new List<EmailAddress> { new EmailAddress() { Email = "[email protected]" }, new EmailAddress() { Email = "[email protected]", Name = "Test User2" } });
2234+
Assert.Equal("{\"reply_to_list\":[{\"email\":\"[email protected]\"},{\"name\":\"Test User2\",\"email\":\"[email protected]\"}]}", msg.Serialize());
2235+
}
2236+
22202237
[Fact]
22212238
public void TestSetGlobalSubject()
22222239
{

tests/SendGrid.Tests/PreSendEmailValidation/WhenCreatingASendGridMessage.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,19 @@ public void WithANullCarbonCopyThenAnExceptionIsThrown()
4848
var sendGridMessage = MailHelper.CreateSingleEmail(new EmailAddress(), new EmailAddress(), string.Empty, string.Empty, string.Empty);
4949
Assert.Throws<ArgumentNullException>(() => { sendGridMessage.AddCc(null, 0); });
5050
}
51+
52+
[Fact]
53+
public void WithAnEmptyListOfReplyTosThenAnExceptionIsThrown()
54+
{
55+
var sendGridMessage = MailHelper.CreateSingleEmail(new EmailAddress(), new EmailAddress(), string.Empty, string.Empty, string.Empty);
56+
Assert.Throws<InvalidOperationException>(() => { sendGridMessage.AddReplyTos(new List<EmailAddress>()); });
57+
}
58+
59+
[Fact]
60+
public void WithANullListOfReplyTosThenAnExceptionIsThrown()
61+
{
62+
var sendGridMessage = MailHelper.CreateSingleEmail(new EmailAddress(), new EmailAddress(), string.Empty, string.Empty, string.Empty);
63+
Assert.Throws<ArgumentNullException>(() => { sendGridMessage.AddReplyTos(null); });
64+
}
5165
}
5266
}

0 commit comments

Comments
 (0)