0

I want to use a SendGrid transactional template to format emails which I am sending using the wp_mail function.

I'm sending emails like this:

function mytheme_send_email($user, $message){
        $email = $user->user_email;
        $subject = "A message from my app!";
        $admin_email = get_option('admin_email');
        $headers = "From: ". $admin_email . "\r\n" ."Reply-To: " . $email . "\r\n";
        $sent = wp_mail($email, $subject, strip_tags($message), $headers);
}

I have an HTML template on SendGrid, which I have set as the default template in my wordpress sendgrid plugin. I am not sure how to actually write the template. Here is the relevant part:


<table border="0" cellpadding="0" cellspacing="0" width="100%" style="max-width: 600px;">
      <tr>
        <td bgcolor="#ffffff" align="left" style="padding: 24px; font-family: 'Source Sans Pro', Helvetica, Arial, sans-serif; font-size: 16px; line-height: 24px;">
          <h1 style="margin: 0 0 12px; font-size: 32px; font-weight: 400; line-height: 48px;"></h1>
          <p style="margin: 0;"> <%message%> </p>
        </td>
      </tr>
</table>

I don't know how to get the <%message%> to display. When I try sending an email, it literally says <%message%>. I want to be able to display the $message that I passed to mytheme_send_email within my template.

Any help is appreciated!

  • It looks like you configure this through the headers, possibly by passing a SendGrid helper object instead of a normal collection. See the example on this page - search for ‘setTemplateId’. – Rup Aug 29 '19 at 6:27
  • I have updated the settings in the plugin which is what I think that page is telling me to do - I will update above so that it's more clear – ellen Aug 29 '19 at 8:06
  • Oh, sorry, I missed that you'd said you'd set a default template. I'd guess it's addSubstitution you want then, although I can't see any obvious docs for that. – Rup Aug 29 '19 at 8:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.