-
Notifications
You must be signed in to change notification settings - Fork 519
Expand file tree
/
Copy pathEmailActionHandler.cs
More file actions
90 lines (67 loc) · 2.96 KB
/
EmailActionHandler.cs
File metadata and controls
90 lines (67 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// ==========================================================================
// Squidex Headless CMS
// ==========================================================================
// Copyright (c) Squidex UG (haftungsbeschraenkt)
// All rights reserved. Licensed under the MIT license.
// ==========================================================================
using MailKit.Net.Smtp;
using MimeKit;
using MimeKit.Text;
using Squidex.Domain.Apps.Core.HandleRules;
using Squidex.Domain.Apps.Core.Rules.EnrichedEvents;
#pragma warning disable MA0048 // File name must match type name
namespace Squidex.Extensions.Actions.Email;
public sealed class EmailActionHandler(RuleEventFormatter formatter) : RuleActionHandler<EmailAction, EmailJob>(formatter)
{
protected override async Task<(string Description, EmailJob Data)> CreateJobAsync(EnrichedEvent @event, EmailAction action)
{
var ruleJob = new EmailJob
{
ServerHost = action.ServerHost,
ServerPassword = action.ServerPassword,
ServerPort = action.ServerPort,
ServerUsername = (await FormatAsync(action.ServerUsername, @event))!,
MessageFrom = (await FormatAsync(action.MessageFrom, @event))!,
MessageTo = (await FormatAsync(action.MessageTo, @event))!,
MessageSubject = (await FormatAsync(action.MessageSubject, @event))!,
MessageBody = (await FormatAsync(action.MessageBody, @event))!,
};
var description = $"Send an email to {ruleJob.MessageTo}";
return (description, ruleJob);
}
protected override async Task<Result> ExecuteJobAsync(EmailJob job,
CancellationToken ct = default)
{
using (var smtpClient = new SmtpClient())
{
await smtpClient.ConnectAsync(job.ServerHost, job.ServerPort, cancellationToken: ct);
if (!string.IsNullOrWhiteSpace(job.ServerUsername) && !string.IsNullOrWhiteSpace(job.ServerPassword))
{
await smtpClient.AuthenticateAsync(job.ServerUsername, job.ServerPassword, ct);
}
var smtpMessage = new MimeMessage();
smtpMessage.From.Add(MailboxAddress.Parse(
job.MessageFrom));
smtpMessage.To.Add(MailboxAddress.Parse(
job.MessageTo));
smtpMessage.Body = new TextPart(TextFormat.Html)
{
Text = job.MessageBody,
};
smtpMessage.Subject = job.MessageSubject;
await smtpClient.SendAsync(smtpMessage, ct);
}
return Result.Complete();
}
}
public sealed class EmailJob
{
public int ServerPort { get; set; }
public string ServerHost { get; set; }
public string ServerUsername { get; set; }
public string ServerPassword { get; set; }
public string MessageFrom { get; set; }
public string MessageTo { get; set; }
public string MessageSubject { get; set; }
public string MessageBody { get; set; }
}