Browse Source

Merge pull request #340 from seamys/rule-email

Add a new rule for sending email
pull/342/head
Sebastian Stehle 7 years ago
committed by GitHub
parent
commit
6c95915697
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 58
      extensions/Squidex.Extensions/Actions/Email/EmailAction.cs
  2. 87
      extensions/Squidex.Extensions/Actions/Email/EmailActionHandler.cs
  3. 1
      src/Squidex/app/features/rules/declarations.ts
  4. 2
      src/Squidex/app/features/rules/module.ts
  5. 128
      src/Squidex/app/features/rules/pages/rules/actions/email-action.component.html
  6. 6
      src/Squidex/app/features/rules/pages/rules/actions/email-action.component.scss
  7. 71
      src/Squidex/app/features/rules/pages/rules/actions/email-action.component.ts
  8. 7
      src/Squidex/app/features/rules/pages/rules/rule-wizard.component.html

58
extensions/Squidex.Extensions/Actions/Email/EmailAction.cs

@ -0,0 +1,58 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System.ComponentModel.DataAnnotations;
using Squidex.Domain.Apps.Core.Rules;
namespace Squidex.Extensions.Actions.Email
{
[RuleActionHandler(typeof(EmailActionHandler))]
[RuleAction(
IconImage = "<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'><path d='M28 5h-24c-2.209 0-4 1.792-4 4v13c0 2.209 1.791 4 4 4h24c2.209 0 4-1.791 4-4v-13c0-2.208-1.791-4-4-4zM2 10.25l6.999 5.25-6.999 5.25v-10.5zM30 22c0 1.104-0.898 2-2 2h-24c-1.103 0-2-0.896-2-2l7.832-5.875 4.368 3.277c0.533 0.398 1.166 0.6 1.8 0.6 0.633 0 1.266-0.201 1.799-0.6l4.369-3.277 7.832 5.875zM30 20.75l-7-5.25 7-5.25v10.5zM17.199 18.602c-0.349 0.262-0.763 0.4-1.199 0.4s-0.851-0.139-1.2-0.4l-12.8-9.602c0-1.103 0.897-2 2-2h24c1.102 0 2 0.897 2 2l-12.801 9.602z'/></svg>",
IconColor = "#333300",
Display = "Send an email",
Description = "Send an email",
ReadMore = "https://en.wikipedia.org/wiki/Email")]
public class EmailAction : RuleAction
{
[Required]
[Display(Name = "ServerHost", Description = "The Name or IP address of the host used for SMTP transactions.")]
public string ServerHost { get; set; }
[Required]
[Display(Name = "ServerPort", Description = "The port to be used on host.")]
public int ServerPort { get; set; }
[Required]
[Display(Name = "ServerUseSsl", Description = "Specify whether the smtp client uses Secure Sockets Layer (SSL) to encrypt the connection.")]
public bool ServerUseSsl { get; set; }
[Required]
[Display(Name = "ServerUsername", Description = "The username used to authenticate the sender.")]
public string ServerUsername { get; set; }
[Required]
[Display(Name = "ServerPassword", Description = "The password used to authenticate the sender.")]
public string ServerPassword { get; set; }
[Required]
[Display(Name = "MessageFrom", Description = "The email sending address.")]
public string MessageFrom { get; set; }
[Required]
[Display(Name = "MessageTo", Description = "The email message will be sent to.")]
public string MessageTo { get; set; }
[Required]
[Display(Name = "MessageSubject", Description = "The subject line for this email message.")]
public string MessageSubject { get; set; }
[Required]
[Display(Name = "MessageBody", Description = "The message body.")]
public string MessageBody { get; set; }
}
}

87
extensions/Squidex.Extensions/Actions/Email/EmailActionHandler.cs

@ -0,0 +1,87 @@
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using Squidex.Domain.Apps.Core.HandleRules;
using Squidex.Domain.Apps.Core.HandleRules.EnrichedEvents;
using Squidex.Infrastructure;
namespace Squidex.Extensions.Actions.Email
{
public sealed class EmailActionHandler : RuleActionHandler<EmailAction, EmailJob>
{
private const string Description = "Send an Email";
public EmailActionHandler(RuleEventFormatter formatter)
: base(formatter)
{
}
protected override (string Description, EmailJob Data) CreateJob(EnrichedEvent @event, EmailAction action)
{
var ruleJob = new EmailJob
{
ServerHost = action.ServerHost,
ServerUseSsl = action.ServerUseSsl,
ServerPassword = action.ServerPassword,
ServerPort = action.ServerPort,
ServerUsername = Format(action.ServerUsername, @event),
MessageFrom = Format(action.MessageFrom, @event),
MessageTo = Format(action.MessageTo, @event),
MessageSubject = Format(action.MessageSubject, @event),
MessageBody = Format(action.MessageBody, @event)
};
return (Description, ruleJob);
}
protected override async Task<(string Dump, Exception Exception)> ExecuteJobAsync(EmailJob job)
{
using (var client = new SmtpClient(job.ServerHost, job.ServerPort))
{
client.EnableSsl = job.ServerUseSsl;
client.Credentials = new NetworkCredential(job.ServerUsername, job.ServerPassword);
using (var message = new MailMessage(job.MessageFrom, job.MessageTo))
{
message.Subject = job.MessageSubject;
message.Body = job.MessageBody;
await client.SendMailAsync(message);
}
}
return ("Completed", null);
}
}
public class EmailJob
{
public string ServerHost { get; set; }
public int ServerPort { get; set; }
public string ServerUsername { get; set; }
public string ServerPassword { get; set; }
public bool ServerUseSsl { get; set; }
public string MessageFrom { get; set; }
public string MessageTo { get; set; }
public string MessageSubject { get; set; }
public string MessageBody { get; set; }
}
}

1
src/Squidex/app/features/rules/declarations.ts

@ -9,6 +9,7 @@ export * from './pages/rules/actions/algolia-action.component';
export * from './pages/rules/actions/azure-queue-action.component';
export * from './pages/rules/actions/discourse-action.component';
export * from './pages/rules/actions/elastic-search-action.component';
export * from './pages/rules/actions/email-action.component';
export * from './pages/rules/actions/fastly-action.component';
export * from './pages/rules/actions/medium-action.component';
export * from './pages/rules/actions/prerender-action.component';

2
src/Squidex/app/features/rules/module.ts

@ -21,6 +21,7 @@ import {
ContentChangedTriggerComponent,
DiscourseActionComponent,
ElasticSearchActionComponent,
EmailActionComponent,
FastlyActionComponent,
MediumActionComponent,
PrerenderActionComponent,
@ -66,6 +67,7 @@ const routes: Routes = [
AzureQueueActionComponent,
ContentChangedTriggerComponent,
DiscourseActionComponent,
EmailActionComponent,
ElasticSearchActionComponent,
FastlyActionComponent,
MediumActionComponent,

128
src/Squidex/app/features/rules/pages/rules/actions/email-action.component.html

@ -0,0 +1,128 @@
<div [formGroup]="actionForm" class="form-horizontal">
<div class="form-group row">
<label class="col-3 col-form-label" for="serverHost">ServerHost</label>
<div class="col-9">
<sqx-control-errors for="serverHost" [submitted]="actionFormSubmitted"></sqx-control-errors>
<input type="text" class="form-control" id="serverHost" formControlName="serverHost" />
<small class="form-text text-muted">
The Name or IP address of the host used for SMTP transactions.
</small>
</div>
</div>
<div class="form-group row">
<label class="col-3 col-form-label" for="serverPort">ServerPort</label>
<div class="col-9">
<sqx-control-errors for="serverPort" [submitted]="actionFormSubmitted"></sqx-control-errors>
<input type="text" class="form-control" id="serverPort" formControlName="serverPort" />
<small class="form-text text-muted">
The port to be used on host.
</small>
</div>
</div>
<div class="form-group row">
<div class="col-9 offset-3">
<div class="form-check">
<input class="form-check-input" type="checkbox" id="serverUseSsl" formControlName="serverUseSsl" />
<label class="form-check-label" for="serverUseSsl">
ServerUseSsl
</label>
</div>
<small class="form-text text-muted">
Specify whether the smtp client uses Secure Sockets Layer (SSL) to encrypt the connection.
</small>
</div>
</div>
<div class="form-group row">
<label class="col-3 col-form-label" for="serverUsername">ServerUsername</label>
<div class="col-9">
<sqx-control-errors for="serverUsername" [submitted]="actionFormSubmitted"></sqx-control-errors>
<input type="text" class="form-control" id="serverUsername" formControlName="serverUsername" />
<small class="form-text text-muted">
The username used to authenticate the sender. Read the <a routerLink="help">help</a> section for information about advanced formatting.
</small>
</div>
</div>
<div class="form-group row">
<label class="col-3 col-form-label" for="serverPassword">ServerPassword</label>
<div class="col-9">
<sqx-control-errors for="serverPassword" [submitted]="actionFormSubmitted"></sqx-control-errors>
<input type="password" class="form-control" id="serverPassword" formControlName="serverPassword" />
<small class="form-text text-muted">
The password used to authenticate the sender.
</small>
</div>
</div>
<div class="form-group row">
<label class="col-3 col-form-label" for="messageFrom">MessageFrom</label>
<div class="col-9">
<sqx-control-errors for="messageFrom" [submitted]="actionFormSubmitted"></sqx-control-errors>
<input type="text" class="form-control" id="messageFrom" formControlName="messageFrom" />
<small class="form-text text-muted">
The email sending address. Read the <a routerLink="help">help</a> section for information about advanced formatting.
</small>
</div>
</div>
<div class="form-group row">
<label class="col-3 col-form-label" for="messageTo">MessageTo</label>
<div class="col-9">
<sqx-control-errors for="messageTo" [submitted]="actionFormSubmitted"></sqx-control-errors>
<input type="text" class="form-control" id="messageTo" formControlName="messageTo" />
<small class="form-text text-muted">
The email message will be sent to. Read the <a routerLink="help">help</a> section for information about advanced formatting.
</small>
</div>
</div>
<div class="form-group row">
<label class="col-3 col-form-label" for="messageSubject">MessageSubject</label>
<div class="col-9">
<sqx-control-errors for="messageSubject" [submitted]="actionFormSubmitted"></sqx-control-errors>
<input type="text" class="form-control" id="messageSubject" formControlName="messageSubject" />
<small class="form-text text-muted">
The subject line for this email message. Read the <a routerLink="help">help</a> section for information about advanced formatting.
</small>
</div>
</div>
<div class="form-group row">
<label class="col-3 col-form-label" for="messageBody">MessageBody</label>
<div class="col-9">
<sqx-control-errors for="messageBody" [submitted]="actionFormSubmitted"></sqx-control-errors>
<textarea class="form-control" id="messageBody" formControlName="messageBody"></textarea>
<small class="form-text text-muted">
The message body. Read the <a routerLink="help">help</a> section for information about advanced formatting.
</small>
</div>
</div>
</div>

6
src/Squidex/app/features/rules/pages/rules/actions/email-action.component.scss

@ -0,0 +1,6 @@
@import '_vars';
@import '_mixins';
textarea {
height: 250px;
}

71
src/Squidex/app/features/rules/pages/rules/actions/email-action.component.ts

@ -0,0 +1,71 @@
/*
* Squidex Headless CMS
*
* @license
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved.
*/
import { Component, Input, OnInit } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
@Component({
selector: 'sqx-email-action',
styleUrls: ['./email-action.component.scss'],
templateUrl: './email-action.component.html'
})
export class EmailActionComponent implements OnInit {
@Input()
public action: any;
@Input()
public actionForm: FormGroup;
@Input()
public actionFormSubmitted = false;
public ngOnInit() {
this.actionForm.setControl('serverHost',
new FormControl(this.action.serverHost || 'smtp.gmail.com', [
Validators.required
]));
this.actionForm.setControl('serverPort',
new FormControl(this.action.serverPort || 465, [
Validators.required,
Validators.pattern(/\d{2,6}/)
]));
this.actionForm.setControl('serverUseSsl',
new FormControl(this.action.serverUseSsl || true));
this.actionForm.setControl('serverUsername',
new FormControl(this.action.serverUsername || '', [
Validators.required
]));
this.actionForm.setControl('serverPassword',
new FormControl(this.action.serverPassword || '', [
Validators.required
]));
this.actionForm.setControl('messageFrom',
new FormControl(this.action.messageFrom || '', [
Validators.required
]));
this.actionForm.setControl('messageTo',
new FormControl(this.action.messageTo || '', [
Validators.required
]));
this.actionForm.setControl('messageSubject',
new FormControl(this.action.messageSubject || '', [
Validators.required
]));
this.actionForm.setControl('messageBody',
new FormControl(this.action.messageBody || '', [
Validators.required
]));
}
}

7
src/Squidex/app/features/rules/pages/rules/rule-wizard.component.html

@ -94,6 +94,13 @@
[actionFormSubmitted]="actionForm.submitted | async">
</sqx-elastic-search-action>
</ng-container>
<ng-container *ngSwitchCase="'Email'">
<sqx-email-action
[action]="action"
[actionForm]="actionForm.form"
[actionFormSubmitted]="actionForm.submitted | async">
</sqx-email-action>
</ng-container>
<ng-container *ngSwitchCase="'Fastly'">
<sqx-fastly-action
[action]="action"

Loading…
Cancel
Save