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

A recipient must be specified. #140

Closed daCodez closed 8 years ago

daCodez commented 8 years ago

No matter what I do I keep getting this. I've verified that a recipient is being specified. Even something as simple as this...

            Dim emailService as IEmailService = New Postal.EmailService

            Dim OrderEmail As Object = New Email("CustomerOrder")
            OrderEmail.Recipient = order.Email
            OrderEmail.Order = order

Also tried with a strongly typed class 'Dim OrderEmail = New CustomerOrderEmail() With { ' .Recipient = order.Email ' .Order = order '}

            emailService.Send(OrderEmail)

The View

Content-Type: text/plain; charset=utf-8 From: test@test.com To: John Smith john@example.org Subject: Simple email example

Email Example Hi @Model.Order.FirstName
jmsolutionsgroup commented 8 years ago

I had a similar issue when I started a new project. The issue boiled down to how it parses the headers. Below is the code it uses. `

    /// <summary>
    /// Headers are of the form "(key): (value)" e.g. "Subject: Hello, world".
    /// The headers block is terminated by an empty line.
    /// </summary>
    public static void ParseHeaders(TextReader reader, Action<string, string> useKeyAndValue)
    {
        string line;
        while (string.IsNullOrWhiteSpace(line = reader.ReadLine()))
        {
            // Skip over any empty lines before the headers.
        }

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

            var key = match.Groups[1].Value.ToLowerInvariant();
            var value = match.Groups[2].Value.TrimEnd();
            useKeyAndValue(key, value);
        } while (!string.IsNullOrWhiteSpace(line = reader.ReadLine()));
    }

` I fixed the issue by adding a _ViewStart.cshtml file in the same directory of my view. This is what is in my _ViewStart

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

daCodez commented 8 years ago

Just realized I forgot to close this. Thanks for your answer. That was just what I needed.

StefanoChiodino commented 7 years ago

I wanted to use a shared layout for 2 strongly typed emails, so I had to use the parent of the 2 view models, with the recipient, subject and sender.

It's a bit misleading because your email will start with the headers, but when you set Layout anywhere then that means that it will get wrapped around the layout that you select and the headers will not be the in the first line anymore.