Variable in href in an email button

Your software
My Mautic version is: v4.4.6
My PHP version is: 8.0.27
My Database type and version is: 10.5.19-MariaDB-0+deb11u2

Your problem
My problem is:
I am trying to use a variable in a href of a button, but it just redirects me to a 404.

The way I was doing the integration is as follows:

The button has to have the url that is sent from the form as a custom field to send it to a specific url, the data arrives to mautic correctly, the variables in the mail work fine, the problem is the variable in the href.

I attach screenshots.

Can you go to the code and check if the link is properly saved (url encoding fine, no spaces)

The data I get from the form arrives well to Mautic, in the email code that should reach the customer after filling out the form is the one that has the problem that the variable does not take the value of the url and sends to a 404.

What happens if you url encode the link itself?

The data arrives well from the form as attached above, the problem is in the variable inserted in the href of the <a> tag of the mail that I send as a response after filling out the form, mautic removes the variable and redirects to an internal URL 404 or if I put the variable in a <p> tag shows it only as text, it means that the variable is displayed, only in the href does not work and is not clickable.

All urls are replaced by mautic urls

We have it working currently, where we set the URL as a fixed part and only add the variable as a parameter:


This worked fine.

but when I put the url as a variable in href it works for you ? or is it some server side configuration that is giving me problems ?

Also do the test by passing a URL type variable to the href or only a text type variable only with the part of the search that I want it to redirect to leaving the rest of the URL defined, but it still doesn’t work, mautic changes those URLs to those of the domain and redirecting to a 404 as you said above.

Here are some examples of how I’m implementing it:
<a style="text-decoration: none; margin: 0;" href="{contactfield=support_general_href}"><span style="color: #c71618;">GET DETAILS <br></span></a>

This one here has been tried with the development URL or with a production URL, so they are validated URLs:
<a style="text-decoration: none; margin: 0;" href="{contactfield=general_support}"><span style="color: #c71618 ;">GET DETAILS <br/></span></a>

This is how it works, it displays the variable, in this case the URL, but it’s not clickable, it just displays as text.
<p>url: {contactfield=support_general_href}</p>

Is there any update on the subject ?

Could you maybe test with a text field instead of an URL field?
Just a gut feeling… what you replace in your html is a string. Maybe the URL formatted field may be the issue?

I have already tried to pass this data both as url and as text but in both ways the same thing always happens. I know this is a bit late but I ran into a similar issue today where my contactfield variables were showing in my email body but disappearing when included in an href.

In my case, I was referring to my variables in camelcase, and this worked:

But <a href="{contactfield=customUnsubscribeLink}">Unsubscribe</a> failed. The solution in my case was using the correct lower alias in the href, <a href="{contactfield=customunsubscribelink}">Unsubscribe</a>.

In your screenshots it looked like you were doing snake_case variables so not sure if this is the same issue. It does seem like Mautic uses a tighter set of rules for variables in href tags compared to the rest of the email body.

Thank you very much, I tried another approach to the problem that did not involve Mautic, but I am going to do it with Mautic to see if that is a bug and report it as such.

Hi, I would like to know ho did you add the customer unsubscribe link as one of the contact field, I did try to add a custom field but not able to add any URL To it.

Are you editing the code directly?

I was planning to create something {contactfield=customUnsubscribeLink} like this, is this just a normal custom fields? and if so how is it able to unsub a customer.

@jichodalen In our case we created a custom field of “URL” data type. We have a unique setup where we’re unsubscribing people through our own server (which also sends some direct email, not via Mautic). As part of our process we call Mautic via REST API to complete the Mautic-side unsubscribes.

We’re using javascript to encode the user’s email address as part of that “1-click unsubscribe link”, so the customUnsubscribeLink field has a different value for each user such as User clicks and gets a “You’ve been unsubscribed” message from our server, which removes the user all lists (Mautic and non-Mautic). Your requirements might be different, hope this example helps!

1 Like

This Helps a lot thank you so much

1 Like

My example might have been unclear. If you’re working in Javascript, you’ll want to call encodeURIComponent() on the email so it’s encoded as a parameter. For example, encodeURIComponent(“”) results in

By any chance do you have any idea where we can locate this :


on the server, I was trying to edit something to it on how it call the message.html.twig

That is the default URL on the unsublink.