Skip to content
Advertisement

How do you dynamically set a recipient in a mailto link?

I’m trying to dynamically set the recipient of my mailto link in javascript. I thought that I could just put a javascript variable in the recipient place of the link, but I get errors when trying to do so. Anyone have any suggestions of why this might not be working?

This is what I currently have, which is throwing errors inside my code.

var customerEmail = "someone@email.com";

<a href='mailto:' + customerEmail + '?subject=Quote&body=I%20would%20like%20to%20accept%20this%20quote' ><button>Click To Accept</button></a>

Advertisement

Answer

either pull out the concatenation out of the href or use bacticks to fill the href.

<a href={`mailto:${customerEmail}?subject=Quote&body=I%20would%20like%20to%20accept%20this%20quote`} >
   <button>Click To Accept</button>
</a>
Advertisement