I'm using the Postal library to send emails from the contact page on the site I'm working on, it sends the email but not all items are being included in the email. I put a breakpoint in and stepped through the code and all the items are in the model being passed, but the template that's being emailed doesn't have all the data.
Here's the simple template (Contact.cshtml):
@model AccessorizeForLess.Models.ContactEmail
To: ****************@psychocoder.net
From: @Model.From
First Name: @Model.FirstName
Last Name: @Model.LastName
Subject: @Model.SelectedSubject
Message: @Model.Message
Here is the ContactEmail model
using AccessorizeForLess.ViewModels;
using Postal;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace AccessorizeForLess.Models
{
public class ContactEmail : Email
{
public string To { get; set; }
[DataType(DataType.EmailAddress)]
[DisplayName("Email Address")]
[RegularExpression(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$", ErrorMessage = "Please enter a valid email address!")]
[Required(ErrorMessage="Please provide your email address")]
public string From { get; set; }
[DisplayName("First Name")]
[Required(ErrorMessage="Please provide your first name")]
public string FirstName { get; set; }
[DisplayName("Last Name")]
[Required(ErrorMessage="Please provide your last name")]
public string LastName { get; set; }
[DisplayName("Subject")]
[Required(ErrorMessage="Please select a subject")]
public string SelectedSubject { get; set; }
[DisplayName("Message")]
[DataType(DataType.MultilineText)]
[Required(ErrorMessage="Please provide a message for the email")]
public string Message { get; set; }
public List<EmailSubjectViewModel> Subjects { get; set; }
}
}
And the Send method in the ContactController:
public ActionResult Send(ContactEmail email)
{
ContactEmail e = new ContactEmail();
e.From = email.From;
e.FirstName = email.FirstName;
e.LastName = email.LastName;
e.SelectedSubject = email.SelectedSubject;
e.Message = email.Message;
e.Send();
return RedirectToAction("Index");
}
I've stepped through Send and the model being passed contains all the data, but the email that arrives only has last name, subject and message. First name and email address are missing for some reason.
I changed Contact.cshtml (My email template) to look like this and everything is now being populated
Content-Type: text/html; charset=utf-8
@model AccessorizeForLess.Models.ContactEmail
To: **************@psychocoder.net
From: @Model.From
<html>
<head>
<title>Email from @Model.From</title>
<style>
hr
{
border: 1px solid #e50e8d;
box-shadow: 5px 5px 5px #888888;
}
</style>
</head>
<body>
<p>Dear Admin, you have received an email from @Model.FirstName @Model.LastName regarding @Model.SelectedSubject .</p>
<p>The content of the message is below:<br />
<hr />
Message: @Model.Message<br />
<hr /></p>
</body>
</html>