Skip to content

Commit

Permalink
Simplify email body (#13426)
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamco committed Apr 22, 2023
1 parent 97b2faa commit 898eb51
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,32 +52,18 @@

<div class="mb-3">
<div class="form-check">
<input type="checkbox" class="form-check-input" asp-for="IsBodyHtml" />
<label class="form-check-label" asp-for="IsBodyHtml">@T["Does the Body contain HTML?"]</label>
<input type="checkbox" class="form-check-input" asp-for="IsHtmlBody" />
<label class="form-check-label" asp-for="IsHtmlBody">@T["Does the Body contain HTML?"]</label>
<span class="hint dashed">@T["If checked, indicates the body of the email message will be sent as HTML."]</span>
</div>
</div>

<div class="mb-3">
<div class="form-check">
<input type="checkbox" class="form-check-input" asp-for="IsBodyText" />
<label class="form-check-label" asp-for="IsBodyText">@T["Does the Body contain plain text?"]</label>
<span class="hint dashed">@T["If checked, indicates the body of the email message will be sent as plain text."]</span>
</div>
</div>

<div class="mb-3" id="body">
<label asp-for="Body">@T["Body"]</label>
<textarea asp-for="Body" rows="5" class="form-control"></textarea>
<span class="hint">@T["The body of the email message. With Liquid support."]</span>
</div>

<div class="mb-3" id="body-text">
<label asp-for="Body">@T["Body Plain Text"]</label>
<textarea asp-for="BodyText" rows="5" class="form-control"></textarea>
<span class="hint">@T["The plain text body of the email message. With Liquid support."]</span>
</div>

<style asp-name="codemirror"></style>
<script asp-name="codemirror" depends-on="admin" at="Foot"></script>
<script asp-name="codemirror-mode-javascript" at="Foot"></script>
Expand All @@ -88,59 +74,11 @@

<script at="Foot">
$(function () {
var htmlBodyEditor = CodeMirror.fromTextArea(document.getElementById('@Html.IdFor(x => x.Body)'), {
lineNumbers: true,
styleActiveLine: true,
matchBrackets: true,
mode: { name: "liquid" },
});
var textBodyEditor = CodeMirror.fromTextArea(document.getElementById('@Html.IdFor(x => x.BodyText)'), {
var bodyEditor = CodeMirror.fromTextArea(document.getElementById('@Html.IdFor(x => x.Body)'), {
lineNumbers: true,
styleActiveLine: true,
matchBrackets: true,
mode: { name: "liquid" },
});
var chkIsBodyHtml = $('#@Html.IdFor(x => x.IsBodyHtml)');
var chkIsBodyText = $('#@Html.IdFor(x => x.IsBodyText)');
var body = $('#body');
var bodyText = $('#body-text');
if(chkIsBodyHtml.is(':checked')) {
body.show();
body.find('input').attr('required', true);
} else {
body.hide();
body.find('input').attr('required', false);
}
if(chkIsBodyText.is(':checked')) {
bodyText.show();
bodyText.find('input').attr('required', true);
} else {
bodyText.hide();
bodyText.find('input').attr('required', false);
}
chkIsBodyHtml.on('click', function() {
if($(this).is(':checked')) {
body.show();
body.find('input').attr('required', true);
} else {
body.hide();
body.find('input').attr('required', false);
}
});
chkIsBodyText.on('click', function() {
if($(this).is(':checked')) {
bodyText.show();
bodyText.find('input').attr('required', true);
} else {
bodyText.hide();
bodyText.find('input').attr('required', false);
}
});
});
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ public WorkflowExpression<string> BodyText
set => SetProperty(value);
}

public bool IsHtmlBody
{
get => GetProperty(() => true);
set => SetProperty(value);
}

public bool IsBodyHtml
{
get => GetProperty(() => true);
Expand All @@ -115,8 +121,7 @@ public override async Task<ActivityExecutionResult> ExecuteAsync(WorkflowExecuti
var cc = await _expressionEvaluator.EvaluateAsync(Cc, workflowContext, null);
var bcc = await _expressionEvaluator.EvaluateAsync(Bcc, workflowContext, null);
var subject = await _expressionEvaluator.EvaluateAsync(Subject, workflowContext, null);
var body = await _expressionEvaluator.EvaluateAsync(Body, workflowContext, _htmlEncoder);
var bodyText = await _expressionEvaluator.EvaluateAsync(BodyText, workflowContext, null);
var body = await _expressionEvaluator.EvaluateAsync(BodyText, workflowContext, IsHtmlBody ? _htmlEncoder : null);

var message = new MailMessage
{
Expand All @@ -129,9 +134,7 @@ public override async Task<ActivityExecutionResult> ExecuteAsync(WorkflowExecuti
ReplyTo = replyTo?.Trim(),
Subject = subject.Trim(),
Body = body?.Trim(),
BodyText = bodyText?.Trim(),
IsBodyHtml = IsBodyHtml,
IsBodyText = IsBodyText
IsHtmlBody = IsHtmlBody
};

if (!String.IsNullOrWhiteSpace(sender))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ protected override void EditActivity(EmailTask activity, EmailTaskViewModel mode
model.ReplyToExpression = activity.ReplyTo.Expression;
model.SubjectExpression = activity.Subject.Expression;
model.Body = activity.Body.Expression;
model.BodyText = activity.BodyText.Expression;
model.IsBodyHtml = activity.IsBodyHtml;
model.IsBodyText = activity.IsBodyText;
model.IsHtmlBody = activity.IsHtmlBody;
model.BccExpression = activity.Bcc.Expression;
model.CcExpression = activity.Cc.Expression;
}
Expand All @@ -30,9 +28,7 @@ protected override void UpdateActivity(EmailTaskViewModel model, EmailTask activ
activity.ReplyTo = new WorkflowExpression<string>(model.ReplyToExpression);
activity.Subject = new WorkflowExpression<string>(model.SubjectExpression);
activity.Body = new WorkflowExpression<string>(model.Body);
activity.IsBodyHtml = model.IsBodyHtml;
activity.BodyText = new WorkflowExpression<string>(model.BodyText);
activity.IsBodyText = model.IsBodyText;
activity.IsHtmlBody = model.IsHtmlBody;
activity.Bcc = new WorkflowExpression<string>(model.BccExpression);
activity.Cc = new WorkflowExpression<string>(model.CcExpression);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ public class EmailTaskViewModel

public string Body { get; set; }

public string BodyText { get; set; }

public bool IsBodyHtml { get; set; }

public bool IsBodyText { get; set; }
public bool IsHtmlBody { get; set; }
}
}
32 changes: 29 additions & 3 deletions src/OrchardCore/OrchardCore.Email.Abstractions/MailMessage.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;

namespace OrchardCore.Email
Expand Down Expand Up @@ -48,22 +49,47 @@ public class MailMessage
/// <summary>
/// Gets or sets the message content aka body.
/// </summary>
/// <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>
public string BodyText { get; set; }
[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>
public bool IsBodyHtml { get; set; }
[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>
public bool IsBodyText { get; set; }
[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; }

/// <summary>
/// The collection of message attachments.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,14 +217,13 @@ private MimeMessage FromMailMessage(MailMessage message, IList<LocalizedString>

var body = new BodyBuilder();

if (message.IsBodyHtml)
if (message.IsHtmlBody)
{
body.HtmlBody = message.Body;
}

if (message.IsBodyText)
else
{
body.TextBody = message.BodyText;
body.TextBody = message.Body;
}

foreach (var attachment in message.Attachments)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ public async Task<bool> TrySendAsync(object notify, INotificationMessage message
if (message.IsHtmlPreferred && !String.IsNullOrWhiteSpace(message.HtmlBody))
{
mailMessage.Body = message.HtmlBody;
mailMessage.IsBodyHtml = true;
mailMessage.IsHtmlBody = true;
}
else
{
mailMessage.BodyText = message.TextBody;
mailMessage.IsBodyText = true;
mailMessage.Body = message.TextBody;
mailMessage.IsHtmlBody = false;
}

var result = await _smtpService.SendAsync(mailMessage);
Expand Down

0 comments on commit 898eb51

Please sign in to comment.