# Variable in href in an email button

**URL:** <https://forum.mautic.org/t/variable-in-href-in-an-email-button/27907>\
**Category:** Product Support\
**Created:** [May 4, 2023, 5:26pm UTC](https://forum.mautic.org/t/variable-in-href-in-an-email-button/27907 "2023-05-04T17:26:14Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![aantonio.tech](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/aantonio.tech/32/9326_2.png) [@aantonio.tech](https://forum.mautic.org/u/aantonio.tech)\
**Post date:** [May 4, 2023, 5:26pm UTC](https://forum.mautic.org/t/variable-in-href-in-an-email-button/27907/1 "2023-05-04T17:26:14Z")

</div>

**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:  
GET DETAILS

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.

 ![Mautic href variable](https://us1.discourse-cdn.com/flex020/uploads/mautic/original/2X/d/d4982276efd8048fe98c13cf81eea40e53fbf46b.png)

---

<div class="post-metadata">

**Author:** ![joeyk](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/joeyk/32/11164_2.png) [@joeyk](https://forum.mautic.org/u/joeyk)\
**Post date:** [May 5, 2023, 7:59am UTC](https://forum.mautic.org/t/variable-in-href-in-an-email-button/27907/2 "2023-05-05T07:59:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![aantonio.tech](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/aantonio.tech/32/9326_2.png) [@aantonio.tech](https://forum.mautic.org/u/aantonio.tech)\
**Post date:** [May 5, 2023, 4:48pm UTC](https://forum.mautic.org/t/variable-in-href-in-an-email-button/27907/3 "2023-05-05T16:48:34Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex020/uploads/mautic/original/2X/e/efbe45933807ff561e39cf1751b62a1624dfd961.png)  
 ![image](https://us1.discourse-cdn.com/flex020/uploads/mautic/original/2X/a/af6092eb44f833135b44543fed2f0a3ea177e767.png)  
 ![image](https://us1.discourse-cdn.com/flex020/uploads/mautic/original/2X/6/616eee132344cd9dfdbf4dc28042c90267a1ef82.png)

---

<div class="post-metadata">

**Author:** ![joeyk](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/joeyk/32/11164_2.png) [@joeyk](https://forum.mautic.org/u/joeyk)\
**Post date:** [May 6, 2023, 5:46am UTC](https://forum.mautic.org/t/variable-in-href-in-an-email-button/27907/4 "2023-05-06T05:46:52Z")

</div>

What happens if you url encode the link itself?

---

<div class="post-metadata">

**Author:** ![aantonio.tech](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/aantonio.tech/32/9326_2.png) [@aantonio.tech](https://forum.mautic.org/u/aantonio.tech)\
**Post date:** [May 18, 2023, 5:53pm UTC](https://forum.mautic.org/t/variable-in-href-in-an-email-button/27907/5 "2023-05-18T17:53:53Z")

</div>

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.

 ![image](https://us1.discourse-cdn.com/flex020/uploads/mautic/original/2X/e/e728f6832f9a287346278f4d59307e6f7d6d7adf.png)  
 ![image](https://us1.discourse-cdn.com/flex020/uploads/mautic/original/2X/4/4f4f2dc18bc0d6affb0cd9400cfadd2929770500.png)

All urls are replaced by mautic urls

 ![image](https://us1.discourse-cdn.com/flex020/uploads/mautic/original/2X/b/b4d41c8403baabb80f6620fb4288e43c464cd1b4.png)

---

<div class="post-metadata">

**Author:** ![dirk\_s](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/dirk_s/32/748_2.png) [@dirk\_s](https://forum.mautic.org/u/dirk_s)\
**Post date:** [May 19, 2023, 9:47am UTC](https://forum.mautic.org/t/variable-in-href-in-an-email-button/27907/6 "2023-05-19T09:47:33Z")

</div>

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

[https://REDACTED-MAUTIC-URL/whatever?email={contactfield=email}](https://REDACTED-MAUTIC-URL/whatever?email=%7Bcontactfield=email%7D)

This worked fine.

---

<div class="post-metadata">

**Author:** ![aantonio.tech](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/aantonio.tech/32/9326_2.png) [@aantonio.tech](https://forum.mautic.org/u/aantonio.tech)\
**Post date:** [May 19, 2023, 4:55pm UTC](https://forum.mautic.org/t/variable-in-href-in-an-email-button/27907/7 "2023-05-19T16:55:01Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![aantonio.tech](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/aantonio.tech/32/9326_2.png) [@aantonio.tech](https://forum.mautic.org/u/aantonio.tech)\
**Post date:** [May 22, 2023, 4:44pm UTC](https://forum.mautic.org/t/variable-in-href-in-an-email-button/27907/8 "2023-05-22T16:44:37Z")

</div>

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="http://domain-sandbox.s3-website.us-east-2.amazonaws.com/EN/general_support.html?message={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>`

---

<div class="post-metadata">

**Author:** ![aantonio.tech](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/aantonio.tech/32/9326_2.png) [@aantonio.tech](https://forum.mautic.org/u/aantonio.tech)\
**Post date:** [June 5, 2023, 11:42pm UTC](https://forum.mautic.org/t/variable-in-href-in-an-email-button/27907/9 "2023-06-05T23:42:16Z")

</div>

Is there any update on the subject ?

---

<div class="post-metadata">

**Author:** ![dirk\_s](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/dirk_s/32/748_2.png) [@dirk\_s](https://forum.mautic.org/u/dirk_s)\
**Post date:** [June 6, 2023, 7:44am UTC](https://forum.mautic.org/t/variable-in-href-in-an-email-button/27907/10 "2023-06-06T07:44:17Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![aantonio.tech](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/aantonio.tech/32/9326_2.png) [@aantonio.tech](https://forum.mautic.org/u/aantonio.tech)\
**Post date:** [June 6, 2023, 5:24pm UTC](https://forum.mautic.org/t/variable-in-href-in-an-email-button/27907/11 "2023-06-06T17:24:15Z")

</div>

I have already tried to pass this data both as url and as text but in both ways the same thing always happens.

---

<div class="post-metadata">

**Author:** ![thmclellan](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/thmclellan/32/10282_2.png) [@thmclellan](https://forum.mautic.org/u/thmclellan)\
**Post date:** [September 20, 2023, 1:06am UTC](https://forum.mautic.org/t/variable-in-href-in-an-email-button/27907/12 "2023-09-20T01:06:16Z")

</div>

@aantonio.tech 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:  
`<p>{contactfield=customUnsubscribeLink}</p>`

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.

---

<div class="post-metadata">

**Author:** ![aantonio.tech](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/aantonio.tech/32/9326_2.png) [@aantonio.tech](https://forum.mautic.org/u/aantonio.tech)\
**Post date:** [September 20, 2023, 4:35pm UTC](https://forum.mautic.org/t/variable-in-href-in-an-email-button/27907/13 "2023-09-20T16:35:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jichodalen](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/jichodalen/32/11649_2.png) [@jichodalen](https://forum.mautic.org/u/jichodalen)\
**Post date:** [March 20, 2024, 12:56am UTC](https://forum.mautic.org/t/variable-in-href-in-an-email-button/27907/14 "2024-03-20T00:56:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![joeyk](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/joeyk/32/11164_2.png) [@joeyk](https://forum.mautic.org/u/joeyk)\
**Post date:** [March 20, 2024, 7:45am UTC](https://forum.mautic.org/t/variable-in-href-in-an-email-button/27907/15 "2024-03-20T07:45:10Z")

</div>

Are you editing the code directly?

---

<div class="post-metadata">

**Author:** ![jichodalen](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/jichodalen/32/11649_2.png) [@jichodalen](https://forum.mautic.org/u/jichodalen)\
**Post date:** [March 20, 2024, 3:46pm UTC](https://forum.mautic.org/t/variable-in-href-in-an-email-button/27907/16 "2024-03-20T15:46:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![thmclellan](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/thmclellan/32/10282_2.png) [@thmclellan](https://forum.mautic.org/u/thmclellan)\
**Post date:** [March 20, 2024, 4:19pm UTC](https://forum.mautic.org/t/variable-in-href-in-an-email-button/27907/17 "2024-03-20T16:19:56Z")

</div>

@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 [https://domain.com/unsubscribe?email=firstlast%2B715%40gmail.com](https://domain.com/unsubscribe?email=firstlast%2B715%40gmail.com). 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!

---

<div class="post-metadata">

**Author:** ![jichodalen](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/jichodalen/32/11649_2.png) [@jichodalen](https://forum.mautic.org/u/jichodalen)\
**Post date:** [March 20, 2024, 4:59pm UTC](https://forum.mautic.org/t/variable-in-href-in-an-email-button/27907/18 "2024-03-20T16:59:35Z")

</div>

This Helps a lot thank you so much

---

<div class="post-metadata">

**Author:** ![thmclellan](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/thmclellan/32/10282_2.png) [@thmclellan](https://forum.mautic.org/u/thmclellan)\
**Post date:** [March 20, 2024, 6:27pm UTC](https://forum.mautic.org/t/variable-in-href-in-an-email-button/27907/19 "2024-03-20T18:27:42Z")

</div>

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(“[firstlast@gmail.com](mailto:firstlast@gmail.com)”) results in firstlast%[40gmail.com](http://40gmail.com).

---

<div class="post-metadata">

**Author:** ![jichodalen](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/jichodalen/32/11649_2.png) [@jichodalen](https://forum.mautic.org/u/jichodalen)\
**Post date:** [March 20, 2024, 9:05pm UTC](https://forum.mautic.org/t/variable-in-href-in-an-email-button/27907/20 "2024-03-20T21:05:35Z")

</div>

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

|URL|

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.

[Next page](https://forum.mautic.org/t/variable-in-href-in-an-email-button/27907.md?page=2)
