Mailgun API Variables

I’m trying to get the Mailgun api working, I can get it to send a plain text message with no issue but to dynamically add data to my mailgun templates I need to be able to send variables as in the X-Mailgun-Variables line below

I can’t figure out how I’d recreate that part of the curl request in Wappler…any one had any luck with this or similar?

@sbecks, this might help you out. API Connector components, how to use and connect REST/Curl API's

1 Like

Thanks @scott I looked at that page and it was helpful with laying out the bulk of the API, but there’s something specific in the fomratting of these variables that isn’t covered.

image

Adding as V:name Input Data, prefixed with an ‘&’ gets the variable recognised in Mailgun:

image
But the value seems a bit strange, I don;t see where the red text is coming from?..I’d like for some of these to be dynamic values from Wappler, but these return a blank in Mailgun if I put {{$POST_first_name}} for example?

They are escaped characters

" = extra quote
\r = carriage return
\n = new line

They are unwanted characters I assume you have inserted accidentally by pasting a value?

2 Likes

The variable has been defined by he input data Name (which seems to work fine), and it’s value set by the Input Data Value (which is just some typed text:

image

this posts to Mailgun as

image

There’s only a single typed word word in the value. I’ve remade the API schema from scratch and typed the word in. I don’t see how that can trigger accidental characters. It must be something added by Wappler on making the API call? I’ve no doubt I’m doing something wrong somewhere but I can’t see the link between the entries in Wappler and the additional characters in the output…

This looks totally wrong, what exactly have you typed in there, this should just be a parameter name

image

This is the API curl I’m trying to recreate:

the parameters from, to etc are set up as Query parameters in Wappler and work fine. I can send a plain text message through the api with no issue.
image

The user defined variables can be sent either in the X-Mailgun-Variables header, or prefixed with v: (which is what I’m doing here).

The user variables show here in the request:
image

is what’s posted from this in Wappler:
image

The input Name seems to work fine (the syntax sends a variable called name), but the value of name has the escaped characters added.

If I change the syntax (i.e. remove the &, or the =) the variable doesn’t get defined, because the post API request works there are no error messages to work from.

The variables link to mustaches in Mailgun templates, if they’re not present they’re just ignored and the message sends with the mustache label, rather than it’s value.

This is the response:
image

if I just type the parameter.
image

The variable doesn’t get passed in the request.

The syntax comes from using the same API in Bubble. I’ve only added in here after trial and error to try to get it to work. It gets me closer to it working than any other option I’ve tried!

I’ve also used the API in Laravel which doesn’t need any additional syntax around the parameter.

So, I think, some progress…

From the initial query posted, using the JSON editor:

&v:name={{$_POST.first_name}}&

works fine. I get a variable passed and the text of the first name comes through.
image

so an ‘&’ symbol after each field prevents the escape characters being generated.

Expanding on that to pass the other parameters I need:

&v:name={{$_POST.first_name}}&v:body=I am the body&v:url=https://somewhere.com/"
&v:link={{token}}&

Which passes:
image

Posting in this format means I get escape characters in the url and (sometimes, depending on what characters it contains) in the hashed token.

I’ve tried enclosing in ’ ', " ", { }, {{ }}, ( ) all change the output (but add a variety of different characters) or, if I enclose both the parameter and the value, stop the post from passing the variables .

For example:

&v:url=“https://somewhere.com/”&

gives:
image

and:

&“v:name”="{{$_POST.first_name}}"&

Doesn’t pass any parameter to the variables:

image

So, is there any way I can format this to avoid the escape characters being generated?

You really shouldn’t add any additional & and also leave the escaping as it is. This is json escaping and the eventual string will be passed right.

The outputs shown are what’s been passed, the screen shots are from the mailgun mail logs. So the escaping show in the emails as text, exactly as it’s shown in the user variables. If I take the extra ‘&’ out it adds escaping to the plain text entries too.

I know the structure isn’t correct, but it doesn’t work any other way!

After inspecting the Mailgun API - it turns out that Mailgun requires that you post all fields with the encoding multipart/form-data which is more common for upload files.

https://documentation.mailgun.com/en/latest/api-sending.html#sending

Currently we post our data as x-www-form-urlencoded which is a regular form post that most API work with.

Maybe @patrick can check it out and add multipart/form-data also as possible encoding, even if it is used without files.

Which server model are you using, I did a test with PHP and had no problems.

image

image

image