Skip to content
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

Add ISmtpService extension methods #14772

Merged
merged 15 commits into from
May 18, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,7 @@ internal static async Task<bool> SendEmailAsync(this Controller controller, stri
body = sw.ToString();
}

var message = new MailMessage()
{
To = email,
Subject = subject,
Body = body,
IsHtmlBody = true
};

var result = await emailService.SendAsync(message);
var result = await emailService.SendAsync(email, subject, body);

return result.Succeeded;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,10 @@ public async Task<IActionResult> RequestCode()
var code = await UserManager.GenerateEmailConfirmationTokenAsync(user);

var settings = (await SiteService.GetSiteSettingsAsync()).As<EmailAuthenticatorLoginSettings>();
var message = new MailMessage()
{
To = await UserManager.GetEmailAsync(user),
Subject = await GetSubjectAsync(settings, user, code),
Body = await GetBodyAsync(settings, user, code),
IsHtmlBody = true,
};

var result = await _emailService.SendAsync(message);
var to = await UserManager.GetEmailAsync(user);
var subjectAndBody = await GetSubjectAndBodyAsync(settings, user, code);
var result = await _emailService.SendAsync(to, subjectAndBody.Subject, subjectAndBody.Body);

if (!result.Succeeded)
{
Expand Down Expand Up @@ -170,15 +165,9 @@ public async Task<IActionResult> SendCode()
var settings = (await SiteService.GetSiteSettingsAsync()).As<EmailAuthenticatorLoginSettings>();
var code = await UserManager.GenerateTwoFactorTokenAsync(user, TokenOptions.DefaultEmailProvider);

var message = new MailMessage()
{
To = await UserManager.GetEmailAsync(user),
Subject = await GetSubjectAsync(settings, user, code),
Body = await GetBodyAsync(settings, user, code),
IsHtmlBody = true,
};

var result = await _emailService.SendAsync(message);
var to = await UserManager.GetEmailAsync(user);
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved
var subjectAndBody = await GetSubjectAndBodyAsync(settings, user, code);
var result = await _emailService.SendAsync(to, subjectAndBody.Subject, subjectAndBody.Body);

return Ok(new
{
Expand All @@ -188,36 +177,31 @@ public async Task<IActionResult> SendCode()
});
}

private Task<string> GetSubjectAsync(EmailAuthenticatorLoginSettings settings, IUser user, string code)
private async Task<(string Subject, string Body)> GetSubjectAndBodyAsync(EmailAuthenticatorLoginSettings settings, IUser user, string code)
{
var message = string.IsNullOrWhiteSpace(settings.Subject)
? EmailAuthenticatorLoginSettings.DefaultSubject
: settings.Subject;
var subject = string.IsNullOrWhiteSpace(settings.Subject)
? EmailAuthenticatorLoginSettings.DefaultSubject
: settings.Subject;

return GetContentAsync(message, user, code);
}

private Task<string> GetBodyAsync(EmailAuthenticatorLoginSettings settings, IUser user, string code)
{
var message = string.IsNullOrWhiteSpace(settings.Body)
? EmailAuthenticatorLoginSettings.DefaultBody
: settings.Body;
var body = string.IsNullOrWhiteSpace(settings.Body)
? EmailAuthenticatorLoginSettings.DefaultBody
: settings.Body;

return GetContentAsync(message, user, code);
}
return (await GetContentAsync(subject), await GetContentAsync(body));

private async Task<string> GetContentAsync(string message, IUser user, string code)
{
var result = await _liquidTemplateManager.RenderHtmlContentAsync(message, _htmlEncoder, null,
new Dictionary<string, FluidValue>()
{
["User"] = new ObjectValue(user),
["Code"] = new StringValue(code),
});
async Task<string> GetContentAsync(string message)
{
var result = await _liquidTemplateManager.RenderHtmlContentAsync(message, _htmlEncoder, null,
new Dictionary<string, FluidValue>()
{
["User"] = new ObjectValue(user),
["Code"] = new StringValue(code),
});

using var writer = new StringWriter();
result.WriteTo(writer, _htmlEncoder);
using var writer = new StringWriter();
result.WriteTo(writer, _htmlEncoder);

return writer.ToString();
return writer.ToString();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,7 @@ public override async Task<ActivityExecutionResult> ExecuteAsync(WorkflowExecuti

var body = await _expressionEvaluator.EvaluateAsync(ConfirmationEmailTemplate, workflowContext, _htmlEncoder);

var message = new MailMessage()
{
To = email,
Subject = subject,
Body = body,
IsHtmlBody = true
};

var result = await _emailService.SendAsync(message);
var result = await _emailService.SendAsync(email, subject, body);

if (!result.Succeeded)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.Threading.Tasks;

namespace OrchardCore.Email;

/// <summary>
/// Provides extension methods to <see cref="ISmtpService"/>.
/// </summary>
public static class EmailServiceExtensions
{
/// <summary>
/// Sends the specified message to an SMTP server for delivery.
/// </summary>
/// <param name="emailService">The <see cref="IEmailService"/>.</param>
/// <param name="to">The email recipients.</param>
/// <param name="subject">The email subject.</param>
/// <param name="body">The email body.</param>
/// <param name="isHtmlBody">Whether the <paramref name="body"/> is in HTML format or not. Defaults to <c>true</c>.</param>
/// <returns></returns>
/// <exception cref="System.ArgumentException"></exception>
public static Task<EmailResult> SendAsync(this IEmailService emailService, string to, string subject, string body, bool isHtmlBody = true)
{
var message = new MailMessage
{
To = to,
Subject = subject,
Body = body,
IsHtmlBody = isHtmlBody
};

return emailService.SendAsync(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,21 @@ public async Task<bool> TrySendAsync(object notify, INotificationMessage message
return false;
}

var mailMessage = new MailMessage()
{
To = user.Email,
Subject = message.Subject,
};

string body;
bool isHtmlBody;
MikeAlhayek marked this conversation as resolved.
Show resolved Hide resolved

if (message.IsHtmlPreferred && !string.IsNullOrWhiteSpace(message.HtmlBody))
{
mailMessage.Body = message.HtmlBody;
mailMessage.IsHtmlBody = true;
body = message.HtmlBody;
isHtmlBody = true;
}
else
{
mailMessage.Body = message.TextBody;
mailMessage.IsHtmlBody = false;
body = message.TextBody;
isHtmlBody = false;
}

var result = await _emailService.SendAsync(mailMessage);
var result = await _emailService.SendAsync(user.Email, message.Subject, body, isHtmlBody);

return result.Succeeded;
}
Expand Down