title: Custom Email Template Merge Fields when invoked from Apex
body: <p>I have an existing Custom Email Template that is referenced in an Apex Code.
  To this email template I added two merge fields Lead.Name, Lead.Company</p>
  <p>When I Click the 'Send Test and Verify Merge Fields' I can see the merged values.
  However when the email template is invoked from Apex Class, the field values are not merged.</p>
  <p>Attached is the code snippet.</p>
  <pre><code> public PageReference sendDoc(){
      Messaging.Singleemailmessage email = new Messaging.Singleemailmessage();
      email.setToAddresses(new List&lt;String&gt;{ReceiverEmail});
      EmailTemplate template = [Select id,HTMLValue,Subject from EmailTemplate where DeveloperName =: Label.Form_Submission_EmailTemplate];
      String str = template.HTMLvalue;
      str = str.replace('{leadurl}',Label.Sample_Site_URL+leadId+'&amp;sendDoc=true&quot; type=&quot;button&quot;&gt;&lt;b&gt;Submit &lt;/b&gt;&lt;/a&gt;');
              
      email.setHTMLBody(str);
      email.setSubject(template.Subject);
      Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email}); 
      return new PageReference('/' + leadId );
  
  }
  </code></pre>
  <p>The email that is received look like this</p>
  <p><a href="https://i.stack.imgur.com/I4dQR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/I4dQR.png" alt="enter image description here" /></a></p>
  <p>Email Template</p>