How do I trigger Gmail's compose window using a mailto function?

I want to open Gmail’s compose window by clicking a mailto-style link, but I end up launching a different email client, such as Outlook Express. I am looking for a method that reliably opens Gmail’s new message popup. I’ve experimented with various approaches, but none seem to force Gmail to appear. For example, check out the code snippet below:

<a href="#" onclick="startEmailCompose(event)">Initiate Email</a>
<script>
  function startEmailCompose(evt) {
    evt.preventDefault();
    var emailAddress = '[email protected]';
    var emailSubject = 'Query';
    var gmailUrl = 'YOUR_GMAIL_COMPOSE_URL_HERE';
    window.open(gmailUrl + '?to=' + emailAddress + '&subject=' + emailSubject, '_blank');
  }
</script>

Any advice or tips to ensure Gmail is used for composing the email would be greatly appreciated.

I encountered a similar issue and eventually managed to overcome it by building the compose URL dynamically, ensuring the user is directed to Gmail directly rather than falling back to mailto. I set up a function that verifies the user is logged in to Gmail, and then constructs the URL like ‘https://mail.google.com/mail/?view=cm&fs=1&[email protected]&su=Query’, making sure to URL encode any necessary parameters. At one point, browser settings forced the mailto protocol to take precedence. Overriding the click behavior with JavaScript has proven effective, though it remains crucial that browsers are configured to support this redirection. This solution worked consistently in my experience.

During my work on similar issues, I found that bypassing the mailto protocol completely is the most reliable method. Instead of relying on mailto and the system’s default mail client, I built a solution that uses a direct redirection to Gmail’s compose window. I generate a URL resembling ‘https://mail.google.com/mail/?view=cm&fs=1&[email protected]&su=Query’, ensuring proper URL encoding for all parameters. Additionally, I implemented a check to confirm the user is logged into Gmail before redirection, which has resulted in consistent behavior across different browsers in my tests.

hey, i solved it by using gmail’s own compose url with /u/0/#compose instead of mailto. works fine if the user is logged in. sometimes browser settings can be a pain tho, so check those too. hope it sorts it out for ypu

In my experience the key is to bypass the mailto protocol entirely and use Gmail’s own compose URL directly. Instead of letting the browser decide the default email client, I use something like ‘https://mail.google.com/mail/?view=cm&fs=1&[email protected]&su=Query’, properly URL-encoded when needed. This forces Gmail to handle the composition, given that the user is logged into their Google account. I’ve found that handling the click event to prevent default actions is necessary to override any unwanted system defaults.