Home Replacing url in anchor tag inside email template html running automatically
Reply: 0

Replacing url in anchor tag inside email template html running automatically

user3500
1#
user3500 Published in June 23, 2018, 9:23 pm

I am facing an issue while sending an email with a template containing anchor tag with href setting dynamically before sending. Here is my code to replace placeholders.

private string PopulateBody(string userName, string title, string url, string description)
{
    string body = string.Empty;
    using (StreamReader reader = new StreamReader(Server.MapPath("~/EmailTemplate.htm")))
    {
        body = reader.ReadToEnd();
    }
    body = body.Replace("[UserName]", userName);
    body = body.Replace("[Title]", title);
    body = body.Replace("[Url]", url);
    body = body.Replace("[Description]", description);
    return body;
}

HTML template

<td style="width:85px;text-align:center;margin:0;padding:0;" width="85" align="center">
   <a href="{{feedback1}}" target="_blank" rel="noopener noreferrer" style="text-decoration:none;">
   <img src="{{Stars}}" alt="1 star" style="max-width:60px" border="0" />
   </a>
</td>
<td style="width:85px;text-align:center;margin:0;padding:0;" width="85" align="center">
   <a href="{{feedback2}}" target="_blank" rel="noopener noreferrer" style="text-decoration:none;">
   <img src="{{Stars}}" alt="2 star" style="max-width:60px" border="0" />
   </a>
</td>
<td style="width:85px;text-align:center;margin:0;padding:0;" width="85" align="center">
   <a href="{{feedback3}}" target="_blank" rel="noopener noreferrer" style="text-decoration:none;">
   <img src="{{Stars}}" alt="3 star" style="max-width:60px" width="60" border="0" />
   </a>
</td>
<td style="width:85px;text-align:center;margin:0;padding:0;" width="85" align="center">
   <a href="{{feedback4}}" target="_blank" rel="noopener noreferrer" style="text-decoration:none;">
   <img src="{{Stars}}" alt="4 star" style="max-width:60px" border="0" />
   </a>
</td>
<td style="width:85px;text-align:center;margin:0;padding:0;" width="85" align="center">
   <a href="{{feedback5}}" target="_blank" rel="noopener noreferrer" style="text-decoration:none;">
   <img src="{{Stars}}" alt="5 star" style="max-width:60px" border="0" />
   </a>
</td>

Content Replace Code

var templateDirectoryPath = string.Format("{0}EmailTemplates\\DST\\{1}", root, "survey-email-new.html");
     var feedback = string.Format("{0}", "http://www.example.com");
    var content = new StringBuilder(File.ReadAllText(templateDirectoryPath));        
    content.Replace("{{feedback1}}", feedback + "feedback?token=" + Base64Encode(query + "1"));
                        content.Replace("{{feedback2}}", feedback + "feedback?token=" + Base64Encode(query + "2"));
                        content.Replace("{{feedback3}}", feedback + "feedback?token=" + Base64Encode(query + "3"));
                        content.Replace("{{feedback4}}", feedback + "feedback?token=" + Base64Encode(query + "4"));
                        content.Replace("{{feedback5}}", feedback + "feedback?token=" + Base64Encode(query + "5"))

I have placed some code in the url action method when someone visit that link i.e change status etc. Problem is that when an email is sent automatically this link fires . I have logged to see what is happening but the url got hit as soon as email is received and it is invoking that action method.

Need help.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.30794 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO