Skip to content

Commit

Permalink
Set from name #11
Browse files Browse the repository at this point in the history
  • Loading branch information
hbulens committed Oct 21, 2021
1 parent 4634aec commit c0df69a
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 10 deletions.
1 change: 0 additions & 1 deletion src/Facteur.MsGraph/GraphMailer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ await graphClient.Users[Credentials.From]
public async t.Task SendMailAsync<T>(EmailRequest<T> request) where T : class
{
GraphServiceClient graphClient = await ConnectClient().ConfigureAwait(false);

Message message = new()
{
Subject = request.Subject,
Expand Down
10 changes: 10 additions & 0 deletions src/Facteur.SendGrid/EmailAddressConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using SendGrid.Helpers.Mail;

namespace Facteur.SendGrid
{
internal static class EmailAddressConverter
{
internal static EmailAddress ToEmailAddress(this Sender sender)
=> new(sender.Email, sender.Name);
}
}
2 changes: 1 addition & 1 deletion src/Facteur.SendGrid/SendGridBaseMailer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ protected SendGridBaseMailer(string apiKey)
public virtual Task SendMailAsync(EmailRequest request)
{
SendGridClient client = new(ApiKey);
EmailAddress sendFrom = new(request.From);
EmailAddress sendFrom = request.From.ToEmailAddress();
List<EmailAddress> sendTo = request.To.Select(x => new EmailAddress(x)).ToList();
SendGridMessage message = MailHelper
.CreateSingleEmailToMultipleRecipients(sendFrom, sendTo, request.Subject, null, request.Body)
Expand Down
4 changes: 2 additions & 2 deletions src/Facteur.SendGrid/SendGridPlainTextMailer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public SendGridPlainTextMailer(string key) : base(key)
public override Task SendMailAsync(EmailRequest request)
{
SendGridClient client = new(ApiKey);
EmailAddress sendFrom = new(request.From);
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);

Expand All @@ -39,7 +39,7 @@ public override Task SendMailAsync(EmailRequest request)
public Task SendMailAsync<T>(EmailRequest<T> request) where T : class
{
SendGridClient client = new(ApiKey);
EmailAddress sendFrom = new(request.From);
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);

Expand Down
2 changes: 1 addition & 1 deletion src/Facteur.Smtp.Tests/SmtpTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public async Task Smtp_SendTemplateMail_ShouldSend()
EmailRequest<TestMailModel> request = composer
.SetModel(new TestMailModel { Email = "guy.gadbois@facteur.com", Name = "Guy Gadbois" })
.SetSubject("Hello world")
.SetFrom("info@facteur.com")
.SetFrom("info@facteur.com", "Facteur")
.SetTo("tibipi@getnada.com")
.SetCc("tibipi@getnada.com")
.SetBcc("tibipi@getnada.com")
Expand Down
2 changes: 1 addition & 1 deletion src/Facteur.Smtp/SmtpMailer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public async Task SendMailAsync(EmailRequest request)
msg.Subject = request.Subject;
msg.Body = request.Body;
msg.IsBodyHtml = true;
msg.From = new MailAddress(request.From);
msg.From = request.From.ToMailAddress();

if (request.To != null && request.To.Any())
msg.To.Add(string.Join(",", request.To));
Expand Down
10 changes: 10 additions & 0 deletions src/Facteur.Smtp/ToMailAddressConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Net.Mail;

namespace Facteur.Smtp
{
internal static class ToMailAddressConverter
{
internal static MailAddress ToMailAddress(this Sender sender)
=> new(sender.Email, sender.Name);
}
}
13 changes: 10 additions & 3 deletions src/Facteur/Compose/BaseEmailComposer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@ public BaseEmailComposer<T> SetBody(string body)
return this;
}

public BaseEmailComposer<T> SetFrom(string from)
public BaseEmailComposer<T> SetFrom(Sender sender)
{
Request.From = from;
Request.From = sender;
return this;
}

public BaseEmailComposer<T> SetFrom(string email, string name = null)
{
Request.From = new Sender(email, name);
return this;
}

Expand Down Expand Up @@ -61,7 +67,8 @@ public BaseEmailComposer<T> Attach(IEnumerable<Attachment> attachments)

public T Build()
{
Guard.ThrowIfNullOrEmpty(Request.From, nameof(Request.From));
Guard.ThrowIfNull(Request.From, nameof(Request.From));
Guard.ThrowIfNullOrEmpty(Request.From.Email, nameof(Request.From.Email));
Guard.ThrowIfNullOrEmpty(Request.Subject, nameof(Request.Subject));
Guard.ThrowIfNullOrEmpty(Request.To, nameof(Request.To));

Expand Down
3 changes: 2 additions & 1 deletion src/Facteur/Compose/EmailRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

namespace Facteur
{

public class EmailRequest
{
public string Subject { get; set; }

public string Body { get; set; }

public string From { get; set; }
public Sender From { get; set; }

public IEnumerable<string> To { get; set; }

Expand Down
15 changes: 15 additions & 0 deletions src/Facteur/Compose/Sender.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Facteur
{
public struct Sender
{
public Sender(string email, string name)
{
Name = name;
Email = email;
}

public string Name { get; set; }

public string Email { get; set; }
}
}
6 changes: 6 additions & 0 deletions src/Facteur/Guard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ namespace Facteur
{
internal static class Guard
{
internal static void ThrowIfNull(object argumentValue, string argumentName)
{
if (argumentValue == null)
throw new ArgumentNullException(argumentName);
}

internal static void ThrowIfNullOrEmpty(string argumentValue, string argumentName)
{
if (string.IsNullOrEmpty(argumentValue))
Expand Down

0 comments on commit c0df69a

Please sign in to comment.