Skip to content

Commit

Permalink
Merge pull request #6351 from elsa-workflows/improvement/email-attach…
Browse files Browse the repository at this point in the history
…ment-from-expando

Add support for ExpandoObject attachments in email sending
  • Loading branch information
sfmskywalker authored Jan 29, 2025
2 parents 5d653b9 + 4d7b551 commit e995cbf
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/modules/Elsa.Email/Activities/SendEmail.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections;
using System.Dynamic;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.Json;
Expand Down Expand Up @@ -183,6 +184,22 @@ private async Task AddAttachmentsAsync(ActivityExecutionContext context, BodyBui
else if (emailAttachment.Content is Stream stream)
await bodyBuilder.Attachments.AddAsync(fileName, stream, parsedContentType, cancellationToken);

break;
}
case ExpandoObject expandoObject:
{
var dictionary = new Dictionary<string, object>(expandoObject, StringComparer.OrdinalIgnoreCase);
var fileName = dictionary.GetValue<string>("FileName") ?? $"Attachment-{++index}";
var contentType = dictionary.GetValue<string>("ContentType") ?? "application/binary";
var parsedContentType = ContentType.Parse(contentType);
var content = dictionary.GetValue<object>("Content");

if (content is byte[] bytes)
bodyBuilder.Attachments.Add(fileName, bytes, parsedContentType);

else if (content is Stream stream)
await bodyBuilder.Attachments.AddAsync(fileName, stream, parsedContentType, cancellationToken);

break;
}
default:
Expand Down

0 comments on commit e995cbf

Please sign in to comment.