Working with Mailgun Email Templates

The tutorial explains how to modify Server Code MailgunEmail plugin to work with Mailgun email templates.


Mailgun allows storing predefined templates using their Templates API with further using them to send emails with just providing template name(s).

In this tutorial, you will learn how to modify MailgunEmail Server Code plugin from and create a custom Mailgun email template of uniform stylistic design that will allow sending messages with different content. This is very convenient since the templates are predefined and there is no need to modify full html and/or css separately.

Creating Mailgun Email Template


Mailgun Account

Before you you can send emails with the Mailgun plugin, , you will need to create a Mailgun Account and get a domain and API Key ID.
Please check out this section to learn how to do it.

First, you will need to create your Mailgun email template.

  1. To do it, go to Sending >Templates, click the Create message template button and select Blank for a template:
  1. Name your template, add the template code, and confirm creating it by clicking Create and start coding:


To get your first html Mailgun template, you may also use our sample template (named my_template):

<html  style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px;">
    <link href=',700'>
            padding: 18px 4%;
<div style="display:block;max-width:875px;margin:auto;">
    <div class="header">
        <img src="" width="auto" height="40"
    <div style="padding:20px 4% 0 4%">
        <div style="">
                Hello {{username}},
                This is your first message sent from server code script, that uses mailgun email template.
                And here is the text you've entered: <span style="color:red">{{message_text}}</span>
            Thanks for choosing!
    <div class="footer">
        Check our <a href="">documentation</a>.

The created template will be added under the Sending > Templates page of your Mailgun active domain:




Our sample template (my_template) and the variables names (here, {{username}} and {{message_text}}) will be used in the script later.

Creating Server Code Script

Now, you can proceed with creating a Server Code script.

  1. To do it, go to the Server Code page.
  2. Open the Plugins tab.
  3. Import the MailgunEmail plugin:
  1. Click the imported script to open it and set up your Mailgun domain (line 8), api key (line 12), from (line10) parameters, and save your work:


Please check out this section to learn where to look for the needed parameters.

  1. Now, pay attention to the parameters that will be sent to the Mailgun service (lines 26-30).
    Sending text is a common case, but we now want to use the created Mailgun template so let's replace the text parameter with template and add all the variables that were added to your template as the template parameters.
    If following the example above, you will need to type in the below code and save:
"v:username": requestBody.username,
"v:message_text": requestBody.message

Your code should look like this:


Testing Script

It is a good idea to test your script now.

To do it, you can replace the default script values to, subject, username, and message in the script or open the Script Parameters tab, select JSON for Request body, and provide the needed values there, for example, { "to": "[email protected]", "subject":" test", "username":"John Doe", "message": "Hello world!" }:


Finally, switch from the Script Parameters tab to the Run tab and click the Save and run button.



Please be informed that with a free Mailgun plan, you will be able to send messages to authorized recipients only:


If your authorized recipient user checks the inbox now, the email will be there (if it is not, please check the Spam folder):


If the test email is received, your script is ready and you can use it in your app or create a scheduled job to send emails automatically.
To do it, switch to the Jobs tab and specify the needed data for your MailgunEmail_Template script: