I have an email body template for sending email to a customer with shipping tracking nos and links to the tracking site.
The logic enables me to use this template for including upto 5 tracking nos and links. If in case a customer has only 2 tracking nos, the logic should delete other three fields.
html template for email body:
<body>
Dear [FIRSTNAME]{LAST NAME},
Your package was shipped via USPS using tracking # [TRACKINGNUMBER1], [TRACKINGNUMBER2], [TRACKINGNUMBER3], [TRACKINGNUMBER4], [TRACKINGNUMBER5]
Track your package(s) : <a href="https://xUSPSx//[TRACKINGNUMBER1]">Click Here (#1)</a>,
<a href="https://xUSPSx//[TRACKINGNUMBER2]">Click Here (#2)</a>,
<a href="https://xUSPSx//[TRACKINGNUMBER3]">Click Here (#3)</a>,
<a href="https://xUSPSx//[TRACKINGNUMBER4]">Click Here (#4)</a>,
<a href="https://xUSPSx//[TRACKINGNUMBER5]">Click Here (#5)</a>
Thank You.
</body>
Controller logic:
If(orderinfo.shipmethod=="FedEx")
{
emailtemplate=emailtemplate.replace("Your package was shipped via USPS","Your package was shipped via FedEx");
emailtemplate=emailtemplate.replace("https://xUSPSx//","https://xFEDEXx//");
}
string trackingnos[]=// receives all tracking nos
int arraylength=trackingnos.length;
for(int i=0; i<arraylength;i )
{
emailtemplate=emailtemplate.replace("[TRACKINGNUMBER" (i 1) "], trackingnos[i]);
}
for(;arraylength<=5; arraylength )
{
emailtemplate=emailtemplate.replace(", [TRACKINGNUMBER" (i 1) "], "");
emailtemplate=emailtemplate.replace(", Click Here (#" arraylength ")", "" );
}
the following controller logic works for replacing the tracking nos but the hyperlinks are still not replaced for blank fields. Could you pls suggest a correction in my logic
CodePudding user response:
Why do you use a template if you are going to replace everything? Why not construct it from scratch?
This is how I do it in my program adjusted to your case.
private void SendEmail() //parameters if needed Client client, OrderInfo orderinfo
{
string trackingnos[] = ;// receives all tracking nos
int arraylength = trackingnos.length;
StringBuilder emailtemplate = new StringBuilder();
emailtemplate.Append("<body> Dear " client.FIRSTNAME " " client.LastNAME ", <br/>");
emailtemplate.Append("Your package was shipped via " orderinfo.shipmethod "using tracking # ");
for (int i = 0; i < arraylength; i )
{
if (i != arraylength - 1)
{
emailtemplate.Append(trackingnos[i] ", ");
}
else
{
emailtemplate.Append(trackingnos[i] ", <br/>");
}
}
emailtemplate.Append("Track your package(s) : <br/>");
for (int i = 0; i < arraylength; i )
{
emailtemplate.Append("<a href=\"https://x" orderinfo.shipmethod "x//" trackingnos[i] "\">Click Here (#" i ")</a> <br/>");
}
emailtemplate.Append("Thank you. <br/> </body> ");
Microsoft.Office.Interop.Outlook.MailItem eMail = OutlookApplication.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);
eMail.Subject = "Package shipped";
eMail.To = client.Email;
eMail.HTMLBody = emailtemplate.ToString();
eMail.Send();
}