Solved

How to create and add a javascript for prepopulated email

  • 26 January 2024
  • 1 reply
  • 21 views

Badge

I’m looking for visitors to click a button that has a prepopulated email address, subject, and body copy. Currently when I use the email click action in a button I can only prepopulate the email address and subject. Has anyone found a javascript that would do this and knows how to attach the buttons ID to it? 

icon

Best answer by Hristian 29 January 2024, 09:10

View original

1 reply

Userlevel 7
Badge +3

Hi @amy.turner

Something like this might do the trick: 
 

<script>
  
    document.addEventListener('DOMContentLoaded', (event) => {
        var emailButton = document.querySelector('.emailButton');
        emailButton.addEventListener('click', function(e) {
            e.preventDefault();
            var mailtoLink = 'mailto:hello@hello.com?subject=Your Subject Here&body=Your pre-filled email content goes here.';
            window.location.href = mailtoLink;
        });
    });
  
</script>



Just a few notes: 

- Be careful with the body you pre-fill since different email clients handle these differently. Some email clients don’t like too long mailto links. 
- As always… make sure you test this out before deploying to your pages
- Last but not least… make sure you add a class to your buttons “emailButton”  

Reply