/// <summary>
/// Creates an instance of <see cref="Message"/>
/// using the specified template for the body
/// </summary>
/// <param name="templateName">
/// Name of the template to load.
/// Will look in Views/mail for that template file.
/// </param>
/// <param name="context">Context that represents the current request</param>
/// <param name="controller">Controller instance</param>
/// <param name="doNotApplyLayout">If <c>true</c>, it will skip the layout</param>
/// <returns>An instance of <see cref="Message"/></returns>
public Message RenderMailMessage(String templateName, IRailsEngineContext context,
Controller controller, bool doNotApplyLayout)
{
// create a message object
Message message = new Message();
// use the template engine to generate the body of the message
StringWriter writer = new StringWriter();
String oldLayout = controller.LayoutName;
if (doNotApplyLayout)
{
controller.LayoutName = null;
}
if (templateName.StartsWith("/"))
{
controller.InPlaceRenderSharedView(writer, templateName);
}
else
{
controller.InPlaceRenderSharedView(writer, Path.Combine(Constants.EmailTemplatePath, templateName));
}
if (doNotApplyLayout)
{
controller.LayoutName = oldLayout;
}
String body = writer.ToString();
// process delivery addresses from template.
MatchCollection matches = Constants.readdress.Matches(body);
for(int i = 0; i < matches.Count; i++)
{
String header = matches[i].Groups[Constants.HeaderKey].ToString().ToLower();
String address = matches[i].Groups[Constants.ValueKey].ToString();
switch(header)
{
case Constants.To :
message.To = address;
break;
case Constants.Cc :
message.Cc = address;
break;
case Constants.Bcc :
message.Bcc = address;
break;
}
}
if (logger.IsDebugEnabled)
{
logger.DebugFormat("Rendering email message to {0} cc {1} bcc {2}", message.To, message.Cc, message.Bcc);
}
body = Constants.readdress.Replace(body, String.Empty);
// process from address from template
Match match = Constants.refrom.Match(body);
if (match.Success)
{
message.From = match.Groups[Constants.ValueKey].ToString();
body = Constants.refrom.Replace(body, String.Empty);
}
// process subject and X headers from template
matches = Constants.reheader.Matches(body);
for(int i=0; i< matches.Count; i++)
{
String header = matches[i].Groups[Constants.HeaderKey].ToString();
String strval = matches[i].Groups[Constants.ValueKey].ToString();
if (header.ToLower(System.Globalization.CultureInfo.InvariantCulture) == Constants.Subject)
{
message.Subject = strval;
}
else
{
message.Headers.Add(header, strval);
}
if (logger.IsDebugEnabled)
{
logger.DebugFormat("Adding header {0} value {1}", header, strval);
}
}
body = Constants.reheader.Replace(body, String.Empty);
message.Body = body;
if (logger.IsDebugEnabled)
{
logger.DebugFormat("Email message body {0}", body);
}
// a little magic to see if the body is html
if (message.Body.ToLower(System.Globalization.CultureInfo.InvariantCulture).IndexOf(Constants.HtmlTag) != -1)
{
message.Format = Format.Html;
if (logger.IsDebugEnabled)
{
logger.Debug("Content set to Html");
}
}
return message;
}