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 the ability to send an email with a LinkedResource #17863

Open
wants to merge 1 commit into
base: contrib
Choose a base branch
from
Open
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
10 changes: 8 additions & 2 deletions src/Umbraco.Core/Models/Email/EmailMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public class EmailMessage
{
public EmailMessage(string? from, string? to, string? subject, string? body, bool isBodyHtml)
: this(from, new[] { to }, null, null, null, subject, body, isBodyHtml, null)
: this(from, new[] { to }, null, null, null, subject, body, isBodyHtml, null, null)
{
}

Expand All @@ -16,21 +16,23 @@
string? subject,
string? body,
bool isBodyHtml,
IEnumerable<EmailMessageAttachment>? attachments)
IEnumerable<EmailMessageAttachment>? attachments,
IEnumerable<EmailMessageLinkedResource>? linkedResources)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a new parameter to an existing public constructor is a breaking change.
In order to make it non breaking, you can add an additional constructor, with the same signature as the previous one and call the updated one.
Like:

public EmailMessage(
    string? from,
    string?[] to,
    string[]? cc,
    string[]? bcc,
    string[]? replyTo,
    string? subject,
    string? body,
    bool isBodyHtml,
    IEnumerable<EmailMessageAttachment>? attachments)
    : this(from, to, cc, bcc, replyTo, subject, body, isBodyHtml, attachments, null)
{
}

{
ArgumentIsNotNullOrEmpty(to, nameof(to));
ArgumentIsNotNullOrEmpty(subject, nameof(subject));
ArgumentIsNotNullOrEmpty(body, nameof(body));

From = from;
To = to;
Cc = cc;
Bcc = bcc;
ReplyTo = replyTo;
Subject = subject;
Body = body;
IsBodyHtml = isBodyHtml;
Attachments = attachments?.ToList();
LinkedResources = linkedResources?.ToList();

Check notice on line 35 in src/Umbraco.Core/Models/Email/EmailMessage.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (contrib)

ℹ Getting worse: Constructor Over-Injection

EmailMessage increases from 9 to 10 arguments, threshold = 5. This constructor has too many arguments, indicating an object with low cohesion or missing function argument abstraction. Avoid adding more arguments.
}

public string? From { get; }
Expand All @@ -51,8 +53,12 @@

public IList<EmailMessageAttachment>? Attachments { get; }

public IList<EmailMessageLinkedResource>? LinkedResources { get; }

public bool HasAttachments => Attachments != null && Attachments.Count > 0;

public bool HasLinkedResources => LinkedResources != null && LinkedResources.Count > 0;

private static void ArgumentIsNotNullOrEmpty(string? arg, string argName)
{
if (arg == null)
Expand Down
11 changes: 11 additions & 0 deletions src/Umbraco.Core/Models/Email/EmailMessageLinkedResource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Umbraco.Cms.Core.Models.Email
{
public class EmailMessageLinkedResource(Stream stream, string fileName, string contentId)
{
public Stream Stream { get; } = stream;

public string FileName { get; } = fileName;

public string ContentId { get; } = contentId;
}
}
10 changes: 8 additions & 2 deletions src/Umbraco.Infrastructure/Extensions/EmailMessageExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,29 @@
AddAddresses(messageToSend, mailMessage.Bcc, x => x.Bcc);
AddAddresses(messageToSend, mailMessage.ReplyTo, x => x.ReplyTo);

if (mailMessage.HasAttachments)
if (mailMessage.HasAttachments || mailMessage.HasLinkedResources)
{
var builder = new BodyBuilder();
if (mailMessage.IsBodyHtml)
{
builder.HtmlBody = mailMessage.Body;
}
else
{
builder.TextBody = mailMessage.Body;
}

foreach (EmailMessageAttachment attachment in mailMessage.Attachments!)
foreach (EmailMessageAttachment attachment in mailMessage.Attachments ?? [])
{
builder.Attachments.Add(attachment.FileName, attachment.Stream);
}

foreach (EmailMessageLinkedResource linkedResource in mailMessage.LinkedResources ?? [])
{
MimeEntity lr = builder.LinkedResources.Add(linkedResource.FileName, linkedResource.Stream);
lr.ContentId = linkedResource.ContentId;
}

Check warning on line 48 in src/Umbraco.Infrastructure/Extensions/EmailMessageExtensions.cs

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (contrib)

❌ New issue: Complex Method

ToMimeMessage has a cyclomatic complexity of 11, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.
messageToSend.Body = builder.ToMessageBody();
}
else
Expand Down
Loading