andrewdavey / postal

Email sending for asp.net mvc using the view engine system to render emails.
http://aboutcode.net/postal
MIT License
536 stars 168 forks source link

Headers from View Not Parsed Into Mail Object #97

Closed MisterJames closed 9 years ago

MisterJames commented 9 years ago

I'm in VS2013, Update 4. I've created a new MVC project and installed the package (Postal.Mvc5 1.2.0). I've also set up my web.config per the docs.

I've created a controller and corresponding view. When I try to send the email, I'm getting an error that from is required. When I inspect the email object, From and To are null. My controller method looks like this:

        dynamic email = new Email("Simple");
        email.Date = DateTime.UtcNow.ToString();
        email.Send();

        return new EmailViewResult(email);

My view is essentially the same as from the sample.

To: me@myself.com
From: me@myself.com
Subject: Simple email example

Hello, world!

The date is: @ViewBag.Date

The view is being properly generated. If I comment out the call to Send() then I get the view back like this in the browser:

To: me@myself.com
From: me@myself.com
Subject: Something

Hello, world!

The date is: 2014-12-02 10:41:51 PM

I added Ninject and injected the IEmailService into the controller and I also get the same result when I try to send via the injected service (inspecting the source of Postal suggests that is expected as it's the same as the default created service).

If I add the following to my code in my controller before the call to Send, everything works:

        email.From = "me@myself.com";
        email.To = "me@myself.com";
        email.Subject = "Something";

Any suggestions as to why the headers aren't parsing correctly and are always null?

andrewdavey commented 9 years ago

Can you paste in the exact cshtml of the email view please?

MisterJames commented 9 years ago

Thanks for looking at this @andrewdavey.

The view is as I pasted above:

To: me@myself.com
From: me@myself.com
Subject: Simple email example

Hello, world!

The date is: @ViewBag.Date

The only difference is I swapped my email address, but even then, I've re-typed it and made sure that the @ symbol wasn't some funky unicode character.

andrewdavey commented 9 years ago

Do you have any layouts assigned in a _viewstart.cshtml which could be adding leading whitespace to the output?

MisterJames commented 9 years ago

No, and I have the following in my _ViewStart in the Views\Emails folder:

@{ Layout = null; /* Overrides the Layout set for regular page views. */ }

...which was added when I installed the package.

andrewdavey commented 9 years ago

Hmm, very odd. I can't think of anything obviously wrong.

Can you try downloading the Postal repo and loading up src/Samples/WebSample to see if the problem is reproducible?

MisterJames commented 9 years ago

So, I've pulled the source down and referenced the DLL directly from the project. I have a breakpoint set in EmailService.CreateMailMessage. When I hit the call to Parse the rawEmailString contains the following:

"\r\n\r\n?To: me@myself.com\r\nFrom: me@myself.com\r\nSubject: Simple email example\r\n\r\nHello, world!\r\n\r\nThe date is: 2014-12-03 1:16:24 PM\r\n\r\n"

I'm just reviewing what the parser is doing now.

Btw, the WebSamples project works fine here when sending to smtp4dev, but I had some updates to do in order to get it running in VS2012. Did you want me to throw you a PR?

MisterJames commented 9 years ago

So, after the call to headerStart.Match(line), match.Success is always false. In this code block:

        var headerStart = new Regex(@"^\s*([A-Za-z\-]+)\s*:\s*(.*)");
        do
        {
            var match = headerStart.Match(line);
            if (!match.Success) break;
            //...
        }

...line contains To: me@myself.com and it always hits the break. I'm staring at the RegEx and I can't see why this is the case.

:grimacing:

andrewdavey commented 9 years ago

Why is the rawEmailString starting with \r\n\r\n?? What's the ? doing there?

andrewdavey commented 9 years ago

Some kind of file encoding issue perhaps?

MisterJames commented 9 years ago

Maybe an encoding thing? I've updated my view to set the layout to null inline so it's not even using the _ViewStart right now. I looks like those two \r\ns are getting eating by the parser anyways, so it's just that ? that is a mystery.

Here's what I found debugging the Parse:

So, I think you're on to something there, but I'm not sure where the ? is coming from. I also don't know if it's a null or what. I'll try to dig into that now.

MisterJames commented 9 years ago

Oy.

Okay, I'm not sure how this happened, but it turns out that in the very first line of my view (just ahead of the word "To:") there was a char in there with a dec value of 65279 (A "zero-width no break space - http://www.fileformat.info/info/unicode/char/FEFF/index.htm).

Remove that char, and everything works fine.

Sorry for wasting your time. If Visual Studio showed some kind of indicator that there was another character in there I wouldn't have ended up here. Thanks for the prompts.

Cheers, -James

MisterJames commented 9 years ago

@andrewdavey I just found that if you copy and paste (as I did) from here:

https://github.com/andrewdavey/postal/blob/master/src/Samples/WebSample/Views/Emails/Simple.cshtml

...then you get that char in your file. Try it out...select it all, copy and paste that from GitHub to Notepad and you'll see the offending character. Crazy. And yet, when you download the source and run it local, that char isn't there.

I blame GitHub. :stuck_out_tongue:

andrewdavey commented 9 years ago

Ah, that's so evil!