-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sending emails with attachments #7
Comments
Not possible at the moment but it is a good case since every e-mail provider has its own implementation. For example, SendGrid uses |
I think it should be? I'm am not very sure about how it will be implemented, but I think something similar to how MailKit handles attachments should be sufficient. |
I added a simple mechanism to add attachments to the e-mail request. A snippet from an SMTP test: SmtpCredentials credentials = new SmtpCredentials("", " ", "", "", "", "");
byte[] txtBytes = File.ReadAllBytes("Attachments\\Attachment.txt");
byte[] pdfBytes = File.ReadAllBytes("Attachments\\Attachment.pdf");
List<Attachment> attachments = new()
{
new() { ContentBytes = txtBytes, Name = "Attachment.txt" },
new() { ContentBytes = pdfBytes, Name = "Attachment.pdf" }
};
EmailComposer<TestMailModel> composer = new EmailComposer<TestMailModel>();
EmailRequest<TestMailModel> request = composer
.SetModel(new TestMailModel { Email = "guy.gadbois@facteur.com", Name = "Guy Gadbois" })
.SetSubject("Hello world")
.SetFrom("info@facteur.com")
.SetTo("tibipi@getnada.com")
.SetCc("tibipi@getnada.com")
.SetBcc("tibipi@getnada.com")
.Attach(attachments) // This is new
.Build();
IMailer mailer = new SmtpMailer(credentials);
IMailBodyBuilder builder = new MailBodyBuilder();
EmailRequest populatedRequest = await builder
.UseProvider(new AppDirectoryTemplateProvider("Templates", ".sbnhtml"))
.UseResolver(new ViewModelTemplateResolver())
.UseCompiler(new ScribanCompiler())
.BuildAsync(request);
await mailer.SendMailAsync(populatedRequest); To simplify the retrieval of attachments, you can use helper classes that will ultimately end up as separate NuGet packages. For example, Facteur.Attachments.IO is a simple library that allows you to fetch files using good old System.IO. It goes without saying that this mechanism can be expanded to other storage mediums like Azure Blobs. using Facteur.Attachments.IO;
...
IAttachmentSource fileAttachment = new Facteur.Attachments.IO.FileAttachment();
Attachment attachment = await fileAttachment.Fetch("Attachments\\Attachment.txt"); Thoughts? Enough in scope to publish a new version? |
I took a quick look at the code, looks good to me! Another relevant use case that I can think of is inlining/embedding attachment images directly into the message body (iirc by using I am not really familiar with other email providers, but I am guessing it would take quite some effort have a generalized interface for all of them, of course I might be wrong 😅 So the I think the current implementation is probably enough for now. Thanks! |
Great, I'll finalize and publish a new version soon. About the inline attachments, can't this be covered with the templates? You could provide a placeholder in the template which is then populated by a URL property in the mail model? Images could be converted into data uris as to not rely on external content. Haven't tested that yet but it could be an easier way than |
Yeah I agree. In most of the cases using image url is much simpler, but iirc base64-encoded image doesn't work reliably for most email clients (eg. gmail) - a lot of the solutions that I've seen seem to suggest using Anyway thanks again! |
Is your feature request related to a problem? Please describe.
I was trying to send emails that have attachments, but I wasn't able to find any methods for doing it. I briefly searched through the code and didn't find anything too.
Describe the solution you'd like
Able to add attachments to email body.
Is it possible to do this currently?
Thanks!
The text was updated successfully, but these errors were encountered: