umbraco / Umbraco.Forms.Issues

Public issue tracker for Umbraco Forms
29 stars 0 forks source link

Support danish letters in the email workflow containing file attachments #905

Closed vonlinaa closed 1 year ago

vonlinaa commented 1 year ago

When a form contains a file attachment which is send through the 'Send email' workflow. The returned email no longer support the danish letters æøå ÆØÅ. These letters get replaced with '?' If no attachment is selected it work fine and the letters are encoded correct.

The error might be in the workflowEmailService.SendEmailAsync method I think it is a bug in the encoding of the text danish letters æøå ÆØÅ when the form also has to support Content-Type: multipart/mixed;

AndyButland commented 1 year ago

Hi @vonlinaa - I've been unable to replicate this I'm afraid in my testing. It seems I can receive an email with those characters in whether or not an attachment is associated with the message.

I've read some evidence that this might be an issue specific to certain SMTP Servers (gmail I saw mentioned), so I'd be interested if you could replicate it with the following cut-down code. If you add this controller to your solution, create a test file at c:\temp\test.txt (or elsewhere if you adjust the path in the code), and then browse to /umbraco/api/test/test, this should send an email with the attachment.

I've been testing this locally using smtp4dev.

If you see the same issue then it will either be an issue in the CMS (as Forms is just delegating to this code that comes from the CMS for sending emails), or, as I say, perhaps something specific to your SMTP setup.

If you don't, then that suggests there is something specific to Forms that we'll need to dig into further.

using Microsoft.AspNetCore.Mvc;
using Umbraco.Cms.Core.Mail;
using Umbraco.Cms.Core.Models.Email;
using Umbraco.Cms.Web.Common.Controllers;

namespace Umbraco.Forms.Testsite.Controllers
{
    public class TestController : UmbracoApiController
    {
        private readonly IEmailSender _emailSender;

        public TestController(IEmailSender emailSender) => _emailSender = emailSender;

        public IActionResult Test()
        {
            var attachmentStream = new MemoryStream(System.IO.File.ReadAllBytes("c:\\temp\\test.txt"));
            var message = new EmailMessage(
                "sender@test.com",
                new string[] { "recipient@test.com" },
                null,
                null,
                null,
                "Subject",
                "<p>æøå ÆØÅ</p>",
                true,
                new List<EmailMessageAttachment>
                {
                    new EmailMessageAttachment(attachmentStream, "test.txt")
                });

            _emailSender.SendAsync(message, "Test");

            return Ok("Done");
        }
    }
}
vonlinaa commented 1 year ago

Hi @AndyButland,

Thank you very much for the test controller. I can confirm that the error exist when the mail is send to a gmail or office365 account. When sending local it works. I'm using Papercut

I dont think the error is in Umbraco Forms but somewhere in the Core. I'm not clever enough to figure that out.

I have testet with a native smtp client - se code below. This email is received correct. I suspect the encoding is wrong on the build in method.

   public IActionResult Native() {
        SmtpClient client = new SmtpClient(host: "127.0.0.1", port: 25);

        MailAddress from = new MailAddress("sender@test.com",
           "Jane " + (char)0xD8 + " Clayton",
        System.Text.Encoding.UTF8);
        // Set destinations for the email message.
        MailAddress to = new MailAddress("recipient@test.com");
        // Specify the message content.
        MailMessage message = new MailMessage(from, to);
        message.Body = "This is a test email message sent by an application. ÆØÅ æøå ";
        // Include some non-ASCII characters in body and subject.
        string someArrows = new string(new char[] { '\u2190', '\u2191', '\u2192', '\u2193' });
        message.Body += Environment.NewLine + someArrows;
        message.BodyEncoding = System.Text.Encoding.UTF8;
        message.Subject = "test message 1" + someArrows;
        message.SubjectEncoding = System.Text.Encoding.UTF8;

        var filePath = "c:\\test\\text.txt";
        Attachment attachment = new Attachment(filePath, MediaTypeNames.Application.Octet);

        message.Attachments.Add(attachment);

        client.Send(message);

        // Clean up.
        message.Dispose();

        return Ok("Done");

    }
AndyButland commented 1 year ago

Thanks for the update @vonlinaa. I'll close this issue since it doesn't appear to be Forms specific, and have created another one referencing this on the CMS issue tracker here.