From ae2ea6bc18e8c1a0db24aa995bc453a93b2d5150 Mon Sep 17 00:00:00 2001 From: seamys Date: Sun, 30 Dec 2018 16:26:03 +0800 Subject: [PATCH] Change the code style and architecture. --- .../Actions/Email/EmailAction.cs | 37 ++++++--- .../Actions/Email/EmailActionHandler.cs | 33 +++++--- .../Actions/Email/EmailOptions.cs | 22 ------ src/Squidex/AppServices.cs | 2 - .../rules/actions/email-action.component.html | 76 ++++++++++++++++++- .../rules/actions/email-action.component.scss | 1 + .../rules/actions/email-action.component.ts | 27 ++++++- src/Squidex/appsettings.json | 10 --- 8 files changed, 150 insertions(+), 58 deletions(-) delete mode 100644 extensions/Squidex.Extensions/Actions/Email/EmailOptions.cs diff --git a/extensions/Squidex.Extensions/Actions/Email/EmailAction.cs b/extensions/Squidex.Extensions/Actions/Email/EmailAction.cs index 337f8a020..38a67eb8c 100644 --- a/extensions/Squidex.Extensions/Actions/Email/EmailAction.cs +++ b/extensions/Squidex.Extensions/Actions/Email/EmailAction.cs @@ -12,16 +12,35 @@ namespace Squidex.Extensions.Actions.Email { [RuleActionHandler(typeof(EmailActionHandler))] [RuleAction( - IconImage = "", - IconColor = "#333300", - Display = "Send an email", - Description = "Send an email, when some contents has changed", - ReadMore = "https://en.wikipedia.org/wiki/Email")] + IconImage = "", + IconColor = "#333300", + Display = "Send an email", + Description = "Send an email", + ReadMore = "https://en.wikipedia.org/wiki/Email")] public class EmailAction : RuleAction { [Required] - [EmailAddress] - [Display(Name = "From", Description = "The mail is sent from?")] + [Display(Name = "Host", Description = "The Name or IP address of the host used for SMTP transactions.")] + public string Host { get; set; } + + [Required] + [Display(Name = "Port", Description = "The port to be used on host.")] + public int Port { get; set; } + + [Required] + [Display(Name = "EnableSsl", Description = "Specify whether the smtp client uses Secure Sockets Layer (SSL) to encrypt the connection.")] + public bool EnableSsl { get; set; } + + [Required] + [Display(Name = "Username", Description = "The username used to authenticate the sender.")] + public string Username { get; set; } + + [Required] + [Display(Name = "Password", Description = "The password used to authenticate the sender.")] + public string Password { get; set; } + + [Required] + [Display(Name = "From", Description = "The email is sent from?")] public string From { get; set; } [Required] @@ -29,11 +48,11 @@ namespace Squidex.Extensions.Actions.Email public string To { get; set; } [Required] - [Display(Name = "Subject", Description = "The email Subject")] + [Display(Name = "Subject", Description = "The subject line for this e-mail message.")] public string Subject { get; set; } [Required] - [Display(Name = "Body", Description = "The email body")] + [Display(Name = "Body", Description = "The message body.")] public string Body { get; set; } } } diff --git a/extensions/Squidex.Extensions/Actions/Email/EmailActionHandler.cs b/extensions/Squidex.Extensions/Actions/Email/EmailActionHandler.cs index 3ede20836..6f5f9227d 100644 --- a/extensions/Squidex.Extensions/Actions/Email/EmailActionHandler.cs +++ b/extensions/Squidex.Extensions/Actions/Email/EmailActionHandler.cs @@ -22,33 +22,36 @@ namespace Squidex.Extensions.Actions.Email { private const string Description = "Send an Email"; - private readonly EmailOptions emailOptions; - - public EmailActionHandler(RuleEventFormatter formatter, IOptions emailOptions) + public EmailActionHandler(RuleEventFormatter formatter) : base(formatter) { - Guard.NotNull(emailOptions, nameof(emailOptions)); - this.emailOptions = emailOptions.Value; } protected override (string Description, EmailJob Data) CreateJob(EnrichedEvent @event, EmailAction action) { var ruleJob = new EmailJob { - From = action.From, - To = action.To, + Host = action.Host, + EnableSsl = action.EnableSsl, + Password = action.Password, + Port = action.Port, + Username = Format(action.Username, @event), + From = Format(action.From, @event), + To = Format(action.To, @event), Subject = Format(action.Subject, @event), Body = Format(action.Body, @event) }; + return (Description, ruleJob); } protected override async Task<(string Dump, Exception Exception)> ExecuteJobAsync(EmailJob job) { - using (SmtpClient client = new SmtpClient(emailOptions.Host, emailOptions.Port)) + using (var client = new SmtpClient(job.Host, job.Port)) { - client.EnableSsl = emailOptions.EnableSsl; - client.Credentials = new NetworkCredential(emailOptions.Username, emailOptions.Password); + client.EnableSsl = job.EnableSsl; + client.Credentials = new NetworkCredential(job.Username, job.Password); + using (var message = new MailMessage(job.From, job.To)) { message.Subject = job.Subject; @@ -63,6 +66,16 @@ namespace Squidex.Extensions.Actions.Email public class EmailJob { + public string Host { get; set; } + + public int Port { get; set; } + + public string Username { get; set; } + + public string Password { get; set; } + + public bool EnableSsl { get; set; } + public string From { get; set; } public string To { get; set; } diff --git a/extensions/Squidex.Extensions/Actions/Email/EmailOptions.cs b/extensions/Squidex.Extensions/Actions/Email/EmailOptions.cs deleted file mode 100644 index 5bd4e878d..000000000 --- a/extensions/Squidex.Extensions/Actions/Email/EmailOptions.cs +++ /dev/null @@ -1,22 +0,0 @@ -// ========================================================================== -// Squidex Headless CMS -// ========================================================================== -// Copyright (c) Squidex UG (haftungsbeschraenkt) -// All rights reserved. Licensed under the MIT license. -// ========================================================================== - -namespace Squidex.Extensions.Actions.Email -{ - public sealed class EmailOptions - { - public string Host { get; set; } - - public int Port { get; set; } - - public bool EnableSsl { get; set; } - - public string Username { get; set; } - - public string Password { get; set; } - } -} diff --git a/src/Squidex/AppServices.cs b/src/Squidex/AppServices.cs index 69e3ec706..eef4b3c9f 100644 --- a/src/Squidex/AppServices.cs +++ b/src/Squidex/AppServices.cs @@ -58,8 +58,6 @@ namespace Squidex config.GetSection("mode")); services.Configure( config.GetSection("twitter")); - services.Configure( - config.GetSection("smtpServer")); services.Configure( config.GetSection("robots")); services.Configure( diff --git a/src/Squidex/app/features/rules/pages/rules/actions/email-action.component.html b/src/Squidex/app/features/rules/pages/rules/actions/email-action.component.html index ee11337aa..9b7c57a77 100644 --- a/src/Squidex/app/features/rules/pages/rules/actions/email-action.component.html +++ b/src/Squidex/app/features/rules/pages/rules/actions/email-action.component.html @@ -1,4 +1,74 @@
+
+ + +
+ + + + + + The Name or IP address of the host used for SMTP transactions. + +
+
+ +
+ + +
+ + + + + + The port to be used on host. + +
+
+ +
+
+
+ + +
+ + Specify whether the smtp client uses Secure Sockets Layer (SSL) to encrypt the connection. + +
+
+ +
+ + +
+ + + + + + The username used to authenticate the sender. + +
+
+ +
+ + +
+ + + + + + The password used to authenticate the sender. + +
+
+
@@ -8,7 +78,7 @@ - The mail is sent from? + The email is sent from?
@@ -36,7 +106,7 @@ - The Email Subject. + The subject line for this e-mail message. @@ -51,7 +121,7 @@ - The Email body + The message body diff --git a/src/Squidex/app/features/rules/pages/rules/actions/email-action.component.scss b/src/Squidex/app/features/rules/pages/rules/actions/email-action.component.scss index 77a7c8da3..51468cba5 100644 --- a/src/Squidex/app/features/rules/pages/rules/actions/email-action.component.scss +++ b/src/Squidex/app/features/rules/pages/rules/actions/email-action.component.scss @@ -1,5 +1,6 @@ @import '_vars'; @import '_mixins'; + textarea { height: 250px; } \ No newline at end of file diff --git a/src/Squidex/app/features/rules/pages/rules/actions/email-action.component.ts b/src/Squidex/app/features/rules/pages/rules/actions/email-action.component.ts index 554d1541a..ca28aebd1 100644 --- a/src/Squidex/app/features/rules/pages/rules/actions/email-action.component.ts +++ b/src/Squidex/app/features/rules/pages/rules/actions/email-action.component.ts @@ -24,10 +24,33 @@ export class EmailActionComponent implements OnInit { public actionFormSubmitted = false; public ngOnInit() { + this.actionForm.setControl('host', + new FormControl(this.action.host || 'smtp.gmail.com', [ + Validators.required + ])); + + this.actionForm.setControl('port', + new FormControl(this.action.port || 465, [ + Validators.required, + Validators.pattern(/\d{2,6}/) + ])); + + this.actionForm.setControl('enableSsl', + new FormControl(this.action.enableSsl || true)); + + this.actionForm.setControl('username', + new FormControl(this.action.username || '', [ + Validators.required + ])); + + this.actionForm.setControl('password', + new FormControl(this.action.password || '', [ + Validators.required + ])); + this.actionForm.setControl('from', new FormControl(this.action.from || '', [ - Validators.required, - Validators.email + Validators.required ])); this.actionForm.setControl('to', diff --git a/src/Squidex/appsettings.json b/src/Squidex/appsettings.json index 97a7db0d5..1b571af54 100644 --- a/src/Squidex/appsettings.json +++ b/src/Squidex/appsettings.json @@ -304,15 +304,5 @@ * The client secret for twitter. */ "clientSecret": "Pdu9wdN72T33KJRFdFy1w4urBKDRzIyuKpc0OItQC2E616DuZD" - }, - "smtpServer": { - /* - * The hostname of the smtp server - */ - "host": "smtp.gmail.com", - "port": 465, - "enableSsl": true, - "username": "@gmail.com", - "password": "" } }