Closed MisterJames closed 9 years ago
Can you paste in the exact cshtml of the email view please?
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.
Do you have any layouts assigned in a _viewstart.cshtml
which could be adding leading whitespace to the output?
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.
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?
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?
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:
Why is the rawEmailString
starting with \r\n\r\n?
? What's the ?
doing there?
Some kind of file encoding issue perhaps?
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\n
s are getting eating by the parser anyways, so it's just that ?
that is a mystery.
Here's what I found debugging the Parse:
line
= To: me@myself.com
when inspecting the value in the debugger, however, I don't see the ?
characterMatch
failsTo: me@myself.com
the Match
succeeds, as do the rest of the headers.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.
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
@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:
Ah, that's so evil!
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:
My view is essentially the same as from the sample.
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: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:Any suggestions as to why the headers aren't parsing correctly and are always null?