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

Support both Text & HTML in the same email #14715

Open
wants to merge 20 commits into
base: main
Choose a base branch
from

Conversation

hishamco
Copy link
Member

@hishamco hishamco commented Nov 17, 2023

/cc @MikeKry

@hishamco hishamco mentioned this pull request Nov 17, 2023
@hishamco
Copy link
Member Author

@Mike4tel could you please try to send via EmailTask all the 3 cases to ensure it was similar to the old behavior. Also, I might add unit tests too

@MikeKry
Copy link
Contributor

MikeKry commented Nov 25, 2023

@hishamco
Thanks for looking at this. Code looks good and I have tested all 3 scenarios.

Html - OK
Text - OK
"All" (may be renamed to multipart/alternative or something) - Works OK (as seen below). Just one more thing. There is need to have two Body fields - text and html (similar as it was before). Because you need to have different formatting in text and html.

image

@hishamco
Copy link
Member Author

hishamco commented Nov 25, 2023

"All" (may be renamed to multipart/alternative or something)

All or Both make sense to me

There is need to have two Body fields - text and html (similar as it was before). Because you need to have different formatting in text and HTML.

This will revert the changes and let the body TextBody & HtmlBody

@hishamco
Copy link
Member Author

@sebastienros are we fine to add two bodies into the MailMessage or shall we let the sender decide if the body is HTML or Text

One option if we use MailMessageFormat.All is to supply the HTML body and then stripe all the HTML tags to make it fine for the plain-text body. This way we can use a single body instead of two

@Mike4tel
Copy link

Mike4tel commented Nov 25, 2023

Just pointing out that only stripping html will not let users format text nicely for plain text.. it is needed for good marketing.

But I can see using stripped html version as another feature just to fight spam filters.

@MikeKry
Copy link
Contributor

MikeKry commented Jan 20, 2024

Is it possible to merge this one? :)

@hishamco
Copy link
Member Author

We forgot this one :) I will do a final review and then merge

@hishamco hishamco requested review from MikeAlhayek and removed request for agriffard January 20, 2024 21:29
Copy link
Member

@MikeAlhayek MikeAlhayek left a comment

Choose a reason for hiding this comment

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

@hishamco what is the problem that you are trying to solve? Why would we need All as an enum value?

Maybe we should have an interface for the message like INotificationMessage which we already have. Or something similar

public interface IEmailMessage 
{
    string Message { get; }
    bool IsHtml { get; }
}

@hishamco
Copy link
Member Author

hishamco commented Jan 21, 2024

Maybe we should have an interface for the message like INotificationMessage which we already have. Or something similar

I don't think so, the issue was the email can be sent in both formats. In the past we already had IsHtml but it didn't fit anymore, that's why I introduced something similar to the MimiKit mail body

Copy link
Contributor

This pull request has merge conflicts. Please resolve those before requesting a review.

Copy link
Contributor

This pull request has merge conflicts. Please resolve those before requesting a review.

# Conflicts:
#	src/OrchardCore.Modules/OrchardCore.Email/Controllers/AdminController.cs
#	src/OrchardCore.Modules/OrchardCore.Users/Controllers/EmailAuthenticatorController.cs
#	src/OrchardCore/OrchardCore.Email.Core/Services/SmtpService.cs
@hishamco
Copy link
Member Author

hishamco commented Mar 14, 2024

@MikeKry could you please do a last test, because we made a massive changes in the OC.Email module

@@ -76,15 +76,15 @@ public WorkflowExpression<string> Subject
set => SetProperty(value);
}

public WorkflowExpression<string> Body
public WorkflowExpression<string> TextBody
Copy link
Member

Choose a reason for hiding this comment

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

Since GetProperty() below uses this as the name by default, this will break existing Email Tasks, since they'll be empty. Add a migration that reads out the bodies of all Email Tasks, and depending on IsHtmlBody, updates one of the fields with the old body value.

Copy link
Member Author

Choose a reason for hiding this comment

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

The idea is to generate both bodies, so isHtmlBody is not make sense here

Copy link
Member

Choose a reason for hiding this comment

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

For the consumers of the old API, nothing will change. For the consumers of the new API, Obsolete will tell that they shouldn't use this.

mode: { name: "liquid" },
});

$('select').onchange(function () {
Copy link
Member

Choose a reason for hiding this comment

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

I get this when opening a previously existing Email Task:

image

Comment on lines +30 to +31
activity.TextBody = new WorkflowExpression<string>(model.TextBody);
activity.HtmlBody = new WorkflowExpression<string>(model.HtmlBody);
Copy link
Member

Choose a reason for hiding this comment

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

With this logic, if you switch from Text to HTML (not All), for example, then the old Text value will still exist, and will be used, even though you won't see it and won't be able to edit it on the UI.

Comment on lines 52 to 57
/// <remarks>This property is work in conjunction with <see cref="IsHtmlBody"/> to determine the body type..</remarks>
public string Body { get; set; }

/// <summary>
/// Gets or sets the message content as plain text.
/// </summary>
[Obsolete("This property is deprecated, please use Body instead.")]
public string BodyText
{
get => Body;
set
{
Body = value;
IsHtmlBody = false;
}
}

/// <summary>
/// Gets or sets whether the message body is an HTML.
/// </summary>
[Obsolete("This property is deprecated, please use IsHtmlBody instead.")]
public bool IsBodyHtml
{
get => IsHtmlBody;
set => IsHtmlBody = value;
}

/// <summary>
/// Gets or sets whether the message body is plain text.
/// </summary>
[Obsolete("This property is deprecated, please use IsHtmlBody instead.")]
public bool IsBodyText
{
get => !IsHtmlBody;
set => IsHtmlBody = !value;
}

/// <summary>
/// Gets or sets whether the message body is an HTML or not. Default is <c>false</c> which is plain text.
/// </summary>
public bool IsHtmlBody { get; set; }
Copy link
Member

Choose a reason for hiding this comment

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

Since this is a published interface, keep the old properties with Obsolete, and make them use the new Body behind the scenes.

Comment on lines +15 to +17
### Email

The `MailMessage.Body` now returns `MailMessageBody` instead of `string` to support both plain text & HTML formats.
Copy link
Member

Choose a reason for hiding this comment

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

This isn't needed in the 1.8 release notes.

Copy link
Member Author

Choose a reason for hiding this comment

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

It was planned for 1.8 :)

src/docs/releases/1.9.0.md Outdated Show resolved Hide resolved
/// <summary>
/// Gets or sets the body in HTML format.
/// </summary>
public string Html { get; set; }
Copy link
Member

Choose a reason for hiding this comment

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

Maybe IHtmlContent instead to be better typed? With a convenience conversion method to/from string.

Copy link
Member Author

Choose a reason for hiding this comment

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

For simplicity, I prefer string instead, like other email messaging APIs

Copy link
Member

Choose a reason for hiding this comment

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

Yeah but string is really overused. IHtmlContent is for when you want to store a string that's actually HTML.

@@ -14,7 +14,7 @@ public async Task SendEmail_WithToHeader()
{
To = "info@oc.com",
Subject = "Test",
Body = "Test Message"
Body = new MailMessageBody { Text = "Test Message" }
Copy link
Member

Choose a reason for hiding this comment

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

How about an implicit cast operator in MailMessageBody from string (fills Text) and IHtmlContent (fills Html)? Then here you could write Body = "Test Message".

/// <summary>
/// Gets or sets the body in plain text format.
/// </summary>
public string Text { get; set; }
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
public string Text { get; set; }
public string PlainText { get; set; }

Copy link
Member Author

Choose a reason for hiding this comment

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

I used Text something similar to BodyBuilder.Text

Copy link
Member

Choose a reason for hiding this comment

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

I just think that in contrast to "HTML" it's clearer to have "plain text". It's also a term commonly used for this when talking about e-mail.

Copy link
Member Author

@hishamco hishamco Jun 17, 2024

Choose a reason for hiding this comment

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

No problem we can rename it, but I usually "PlainText" in security :)

Comment on lines 120 to 121
Text = textBody?.Trim(),
Html = htmlBody?.Trim()
Copy link
Member

Choose a reason for hiding this comment

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

Have you checked that actually both are sent if both are provided?

hishamco and others added 2 commits March 23, 2024 00:20
Copy link
Contributor

github-actions bot commented Apr 1, 2024

This pull request has merge conflicts. Please resolve those before requesting a review.

Copy link
Contributor

github-actions bot commented Jun 5, 2024

It seems that this pull request didn't really move for quite a while. Is this something you'd like to revisit any time soon or should we close? Please comment if you'd like to pick it up.

@github-actions github-actions bot added the stale label Jun 5, 2024
@hishamco
Copy link
Member Author

hishamco commented Jun 5, 2024

I will revise this soon

@hishamco hishamco removed the stale label Jun 5, 2024
# Conflicts:
#	src/OrchardCore.Modules/OrchardCore.Users/Controllers/EmailAuthenticatorController.cs
#	src/OrchardCore.Modules/OrchardCore.Users/Extensions/ControllerExtensions.cs
#	src/OrchardCore.Modules/OrchardCore.Users/Workflows/Activities/RegisterUserTask.cs
#	src/OrchardCore/OrchardCore.Email.Abstractions/MailMessage.cs
#	src/OrchardCore/OrchardCore.Notifications.Core/Services/EmailNotificationProvider.cs
#	src/docs/releases/1.9.0.md
#	test/OrchardCore.Tests/Email/EmailTests.cs
@hishamco
Copy link
Member Author

@MikeKry could you please do one more test if you have time

Copy link
Contributor

This pull request has merge conflicts. Please resolve those before requesting a review.

Copy link
Contributor

This pull request has merge conflicts. Please resolve those before requesting a review.

@MikeAlhayek
Copy link
Member

for instructions on how to resolve the merge conflicts due to #16572 please follow the step listed in this comment.

Copy link
Contributor

It seems that this pull request didn't really move for quite a while. Is this something you'd like to revisit any time soon or should we close? Please comment if you'd like to pick it up.

@github-actions github-actions bot added the stale label Oct 14, 2024
@hishamco
Copy link
Member Author

Seems I need to revisit this soon

@github-actions github-actions bot removed the stale label Oct 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants