How do I pull an email template and send an email?
How do I pull an email template and send an email?
I would like to make use of the AC7 templates and sendmail engine to send emails within my code.
Any chance you could point me towards the right functions to call so I don't have to dig through a bunch of controls? I have my own controls but I'd rather be as integrated with AC7 as possible.
Ideally I want to retrieve an email template into a string variable. For emailing, I need to be able to specify the to, cc and bcc as well as a file attachment. The super-duper bonus would be knowing the call to write a notes entry to the user notes of a given order.
I know you guys are busy but if it's a quick tidbit I could sure use the help.
I write in VB but I can translate C-Sharp well enough to get the point. Sorta.
Any chance you could point me towards the right functions to call so I don't have to dig through a bunch of controls? I have my own controls but I'd rather be as integrated with AC7 as possible.
Ideally I want to retrieve an email template into a string variable. For emailing, I need to be able to specify the to, cc and bcc as well as a file attachment. The super-duper bonus would be knowing the call to write a notes entry to the user notes of a given order.
I know you guys are busy but if it's a quick tidbit I could sure use the help.
I write in VB but I can translate C-Sharp well enough to get the point. Sorta.
Joe Payne
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com
Code: Select all
EmailTemplate template = [load your email template in question];
//Set all the required velocity parameters used in the template
template.Parameters[key1] = value1;
template.Parameters[key2] = value2;
template.Parameters[key3] = value3;
//Just send :)
template.Send();
That looks great. How do I retrieve a specific template from the table though i.e. by templateId?
Joe Payne
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com
Ok, I can pull up a template, send it etc.
But I don't see a method for including a file attachment. Is that possible or should I code my own send routine that can handle file attachments?
But I don't see a method for including a file attachment. Is that possible or should I code my own send routine that can handle file attachments?
Joe Payne
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com
Ok, no problem. The info helps tremendously, thank you very much - it's going to be very nice leveraging the AC7 template structure.
Joe Payne
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com
Oh Man, I'm like 99% there!!
I'm trying to do a substitute in the template on $order.OrderId using the Template.Parameters method you suggested.
But the method isn't real clear on what it wants as a parameter. The description just says "Item(Key as Object) as Object".
I don't understand that - is it expecting me to declare a variable?
I thought it would be something like
But that no workie....thoughts?
I'm trying to do a substitute in the template on $order.OrderId using the Template.Parameters method you suggested.
But the method isn't real clear on what it wants as a parameter. The description just says "Item(Key as Object) as Object".
I don't understand that - is it expecting me to declare a variable?
I thought it would be something like
Code: Select all
Template.Parameters("$order.Orderid") = SolunarOrder.Order.OrderId
Joe Payne
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com
Code: Select all
Template.Parameters("OrderId") = SolunarOrder.Order.OrderId;
if you put
Code: Select all
Template.Parameters("Order") = SolunarOrder.Order;
including $Order.OrderId
Ohhhhhhh 

Joe Payne
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com
Re: You can add attachments to email templates
But there is a work-aroundsohaib wrote:EmailTemplate class does not support attachments

Code: Select all
EmailTemplate et = emailTemplates[0];
System.Net.Mail.MailMessage mm = et.GenerateMailMessages()[0];
mm.Attachments.Add(new System.Net.Mail.Attachment("D:\\WebSites\\MyWebSite\\App_Data\\XmlFiles\\sample.xml"));
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("127.0.0.1");
client.Send(mm);
Code: Select all
emailTemplates[0].Send();
Re: How do I pull an email template and send an email?
Won't that bypass all the nVelocity scripting found in every email template?
Joe Payne
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com
Re: How do I pull an email template and send an email?
Yep. Here's a generic method I use send mail:AbleMods wrote:Won't that bypass all the nVelocity scripting found in every email template?
Code: Select all
public static void SendSiteMail(string templateName, Hashtable parameters)
{
User customer;
Vendor vendor;
Order order;
// by default, we only send from a production context
if (SiteInformation.Current.IsProduction)
{
// hashtable contains objects for template replacements:
// store, customer(user), basket, order, shipments, vendor, etc.
// see template source for details (velocity references for "objectname" are ${objectname})
NVelocityEngine nVengine = new NVelocityEngine();
// load up the template
EmailTemplate template = Token.Instance.Store.EmailTemplates.Find(delegate(EmailTemplate t) { return t.Name == templateName; });
if (template != null)
{
// load up the order, address info required
if (parameters.ContainsKey("order"))
{
order = parameters["order"] as Order;
if (order != null)
{
template.ToAddress = template.ToAddress.Replace("customer", String.Format("{0}", order.BillToEmail));
template.CCList = template.CCList.Replace("customer", String.Format("{0}", order.BillToEmail));
template.BCCList = template.BCCList.Replace("customer", String.Format("{0}", order.BillToEmail));
}
}
// do other address replacements
if (parameters.ContainsKey("customer"))
{
customer = parameters["customer"] as User;
if (customer != null)
{
// if an order is present, we've already replaced customer with billtoemail...
template.ToAddress = template.ToAddress.Replace("customer", String.Format("{0}", customer.LoweredEmail));
template.CCList = template.CCList.Replace("customer", String.Format("{0}", customer.LoweredEmail));
template.BCCList = template.BCCList.Replace("customer", String.Format("{0}", customer.LoweredEmail));
}
}
if (parameters.ContainsKey("vendor"))
{
vendor = parameters["vendor"] as Vendor;
if (vendor != null)
{
template.ToAddress = template.ToAddress.Replace("vendor", String.Format("{0}", vendor.Email));
template.CCList = template.CCList.Replace("vendor", String.Format("{0}", vendor.Email));
template.BCCList = template.BCCList.Replace("vendor", String.Format("{0}", vendor.Email));
}
}
// invoke velocity engine processing and send mail
nVengine.Init();
template.ToAddress = nVengine.Process(parameters, template.ToAddress);
template.FromAddress = nVengine.Process(parameters, template.FromAddress);
template.CCList = nVengine.Process(parameters, template.CCList);
template.BCCList = nVengine.Process(parameters, template.BCCList);
template.Subject = nVengine.Process(parameters, template.Subject);
template.Body = nVengine.Process(parameters, template.Body);
template.Send();
}
}
}
Nick Cole
http://www.ethofy.com
http://www.ethofy.com
Re: How do I pull an email template and send an email?
I hate to disagree with those who have more experience with AC than I but my code generates an email and doesn't include any of the special merchant fields that I use nVelocity to exclude. For example,nickc wrote:Yep. Here's a generic method I use send mail:AbleMods wrote:Won't that bypass all the nVelocity scripting found in every email template?
Code: Select all
#if ($orderItem.Inputs.Count > 0)
#foreach( $input in $orderItem.Inputs )
#if(!$input.Name.StartsWith("Regex") && $input.Name.Replace(" ", "") != "RequiredFieldsList" && !$input.Name.StartsWith("SG_"))
<br /><b>$input.Name:</b> $input.InputValue
$input.InputValue
#end
#end
#end
Code: Select all
EmailTemplate et = emailTemplates[0];
System.Net.Mail.MailMessage mm = et.GenerateMailMessages()[0];
I don't know if one of you is willing to try out my code and let me know the error of my ways but I'm curious to see if I've screwed up somewhere. Attached is a screenshot of the generated email.
Regards.
Re: How do I pull an email template and send an email?
Ahh ok - I missed the call to the generatemailmessages() method. Yeah as long as you call generatemailmessages on the template, the resulting object will include any nvelocity processing contained in the original email template.
Joe Payne
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com
AbleCommerce Custom Programming and Modules http://www.AbleMods.com/
AbleCommerce Hosting http://www.AbleModsHosting.com/
Precise Fishing and Hunting Time Tables http://www.Solunar.com