Allow fallback to configured email

This commit is contained in:
Mole
2021-06-04 14:18:49 +02:00
parent baf13adef8
commit a56de298a3
2 changed files with 26 additions and 6 deletions

View File

@@ -10,11 +10,7 @@ namespace Umbraco.Cms.Infrastructure.Extensions
{
public static MimeMessage ToMimeMessage(this EmailMessage mailMessage, string configuredFromAddress)
{
var fromEmail = mailMessage.From;
if (string.IsNullOrEmpty(fromEmail))
{
fromEmail = configuredFromAddress;
}
var fromEmail = string.IsNullOrEmpty(mailMessage.From) ? configuredFromAddress : mailMessage.From;
if (!InternetAddress.TryParse(fromEmail, out InternetAddress fromAddress))
{
@@ -62,7 +58,9 @@ namespace Umbraco.Cms.Infrastructure.Extensions
public static NotificationEmailModel ToNotificationEmail(this EmailMessage emailMessage,
string configuredFromAddress)
{
NotificationEmailAddress from = ToNotificationAddress(emailMessage.From);
var fromEmail = string.IsNullOrEmpty(emailMessage.From) ? configuredFromAddress : emailMessage.From;
NotificationEmailAddress from = ToNotificationAddress(fromEmail);
return new NotificationEmailModel(from,
GetNotificationAddresses(emailMessage.To),

View File

@@ -122,6 +122,28 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Infrastructure.Extensions
Assert.IsFalse(result.HasAttachments);
}
[Test]
public void Can_Construct_NotificationEmailModel_From_Simple_MailMessage_With_Configured_Sender()
{
const string to = "to@email.com";
const string subject = "Subject";
const string body = "<p>Message</p>";
const bool isBodyHtml = true;
var emailMessage = new EmailMessage(null, to, subject, body, isBodyHtml);
NotificationEmailModel result = emailMessage.ToNotificationEmail(ConfiguredSender);
Assert.AreEqual(ConfiguredSender, result.From.Adress);
Assert.AreEqual("", result.From.DisplayName);
Assert.AreEqual(1, result.To.Count());
Assert.AreEqual(to, result.To.First().Adress);
Assert.AreEqual("", result.To.First().DisplayName);
Assert.AreEqual(subject, result.Subject);
Assert.AreEqual(body, result.Body);
Assert.IsTrue(result.IsBodyHtml);
Assert.IsFalse(result.HasAttachments);
}
[Test]
public void Can_Construct_NotificationEmailModel_From_Simple_MailMessage_With_DisplayName()
{