feature: expression helpers - basic email sending

This commit is contained in:
Gary Sharp
2024-05-16 22:11:10 +10:00
parent bb846d14c5
commit 78b7b059ea
5 changed files with 47 additions and 2 deletions
+1 -1
View File
@@ -25,7 +25,7 @@ namespace Disco.Models.Services.Messaging
public Email(string to, string subject, string body) public Email(string to, string subject, string body)
: this() : this()
{ {
To.Add(to); To.AddRange(ParseEmailAddresses(to));
Subject = subject; Subject = subject;
Body = body; Body = body;
} }
@@ -4,5 +4,6 @@
{ {
public string Name { get; set; } public string Name { get; set; }
public byte[] Data { get; set; } public byte[] Data { get; set; }
public string MediaType { get; set; }
} }
} }
+7 -1
View File
@@ -38,9 +38,10 @@ namespace Disco.Services.Expressions
public static void InitializeExpressions() public static void InitializeExpressions()
{ {
TypeRegistry.RegisterType("DataExt", typeof(Extensions.DataExt)); TypeRegistry.RegisterType("DataExt", typeof(Extensions.DataExt));
TypeRegistry.RegisterType("UserExt", typeof(Extensions.UserExt));
TypeRegistry.RegisterType("DeviceExt", typeof(Extensions.DeviceExt)); TypeRegistry.RegisterType("DeviceExt", typeof(Extensions.DeviceExt));
TypeRegistry.RegisterType("EmailExt", typeof(Extensions.EmailExt));
TypeRegistry.RegisterType("ImageExt", typeof(Extensions.ImageExt)); TypeRegistry.RegisterType("ImageExt", typeof(Extensions.ImageExt));
TypeRegistry.RegisterType("UserExt", typeof(Extensions.UserExt));
} }
public T EvaluateFirst<T>(object ExpressionContext, IDictionary Variables) public T EvaluateFirst<T>(object ExpressionContext, IDictionary Variables)
@@ -297,6 +298,11 @@ namespace Disco.Services.Expressions
typeof(Extensions.DeviceExt).AssemblyQualifiedName typeof(Extensions.DeviceExt).AssemblyQualifiedName
}, },
{
"EmailExt",
typeof(Extensions.EmailExt).AssemblyQualifiedName
},
{ {
"ImageExt", "ImageExt",
typeof(Extensions.ImageExt).AssemblyQualifiedName typeof(Extensions.ImageExt).AssemblyQualifiedName
@@ -0,0 +1,32 @@
using Disco.Models.Repository;
using Disco.Models.Services.Messaging;
using Disco.Services.Messaging;
using System;
namespace Disco.Services.Expressions.Extensions
{
public static class EmailExt
{
public static void SendToUser(User user, string subject, string body)
{
var to = user.EmailAddress;
if (string.IsNullOrWhiteSpace(to))
throw new Exception($"User ({user.UserId}) does not have an email address");
Send(to, subject, body);
}
public static void Send(string to, string subject, string body)
{
var email = new Email()
{
Subject = subject,
Body = body,
};
email.To.Add(to);
EmailService.SendEmail(email);
}
}
}
+6
View File
@@ -2,6 +2,7 @@
using Disco.Data.Repository; using Disco.Data.Repository;
using Disco.Models.Services.Messaging; using Disco.Models.Services.Messaging;
using System; using System;
using System.IO;
using System.Net; using System.Net;
using System.Net.Mail; using System.Net.Mail;
@@ -98,6 +99,11 @@ namespace Disco.Services.Messaging
foreach (var recipient in email.BCC) foreach (var recipient in email.BCC)
message.Bcc.Add(recipient); message.Bcc.Add(recipient);
} }
if (email.Attachments.Count > 0)
{
foreach (var attachment in email.Attachments)
message.Attachments.Add(new Attachment(new MemoryStream(attachment.Data), attachment.Name, attachment.MediaType));
}
using (var smtpClient = new SmtpClient(smtpServer, smtpPort)) using (var smtpClient = new SmtpClient(smtpServer, smtpPort))
{ {