Thursday 23 September 2021

Mailgun: sending variables to template example - Fix ''Create and start coding'' greyed out issue.

First of all in Mailgun we'll add a template.   ( Keep reading for the ''Create and start coding'' greyed out issue fix tip ) 

So log into your account and on the dashboard you'll see all of your domains.  Click on the name of the domain that you want to add a template for.   Although there is a domain dropdown on the Template page itself, so no big deal if you want to change that later on. 

Next up, you'll need to click on 'Sending' in the left hand menu to see more options.  The 5th option down in this dropdown should be 'Templates' .



Click on this.  And then in the next page you'll get to 'Create Message template' 

You'll need to make sure the characters in the 'name' are valid and you have some sort of description.  Otherwise the 'Create and start coding' button is greyed out and you can't click on it. 


I’m sure this will trip someone else up , but the name input cannot be capitalised. If it is the Mailgun 'Create Message template' will remain greyed out.

In the HTML of template you’re setting up , you can add variable using the following.


1{{ firstname }} {{ lastname }}


How to send from Flask .

So here the function for sending a standard MIME email. With some HTML

1 def send_html_message(self): 2 return requests.post( 3 self.send_message_api, 4 auth=("api", self.mailgun_api_key), 5 data={ 6 "from": self.from_string, 7 "to": [self.to_address_2], 8 "cc": [self.to_address_1], 9 "subject": "HTML test", 10 "html": "<strong>Hello World</strong><br><p>Hi</p><p>This is an HTML test send </p>", 11 }, 12 )

 

And this is one where we use our ‘newsletter’ template and send through some variables.


1 def send_simple_message_template(self): 2 return requests.post( 3 self.send_message_api, 4 auth=("api", self.mailgun_api_key), 5 data={"from": self.from_string, 6 "to": "David Millward <dj@flowmo.co>", 7 "subject": self.subject, 8 "template": "newsletter", 9 "v:firstname":"David", 10 "v:lastname":"John", 11 })


This one will send our newsletter, with the clients name on it.






No comments: