How to Create a Mailto Share Button that Opens a Window in which Ones Can Enter Email Address to Send to

雨燕双飞 提交于 2019-12-02 04:11:29

I recommend never using that mailto: feature as it behaves unpredictably, different on different devices and sometimes fails alltogether. You could create a page just to take the email string then do whatever you want with it server side. Make the popup/popover small like:

<a href="/my-email-collector-page"
   onclick="window.open(this.href,'targetWindow',
                                   'toolbar=no,
                                    location=no,
                                    status=no,
                                    menubar=no,
                                    scrollbars=yes,
                                    resizable=yes,
                                    width=200px,
                                    height=120px');
 return false;">email</a>

You can use window.location.href like this:

window.location.href = 'mailto:email@domain.com'

You can use navigator.registerProtocolHandler() to set specific domains to handle specific protocols as demonstrated by @PaulIrish at Getting Gmail to handle all mailto: links with registerProtocolHandler.

.registerProtocolHandler() must be called at the origin where the protocol is to be handled by the web application, else an error occurs.

User agents must throw a "SecurityError" DOMException if the resulting URL record has an origin that differs from the origin specified by the relevant settings object of this NavigatorContentUtils object.

For example, you can navigate to "https ://mail.google.com/mail/#inbox", then at console

navigator.registerProtocolHandler("mailto",
                                  "https://mail.google.com/mail/?extsrc=mailto&url=%s",
                                  "Gmail");

var email = `<a href="mailto:yourbestfriend@example.com?subject=registerProtocolHandler()%20FTW!&amp;body=Check%20out%20what%20I%20learned%20at%20http%3A%2F%2Fupdates.html5rocks.com%2F2012%2F02%2FGetting-Gmail-to-handle-all-mailto-links-with-registerProtocolHandler%0A%0APlus%2C%20flawless%20handling%20of%20the%20subject%20and%20body%20parameters.%20Bonus%20from%20RFC%202368!" target="_blank">this mailto: link</a>`;

document.body.insertAdjacentHTML("beforeend", email);

document.body.querySelector("a[href^=mailto]:nth-last-of-type(1)").click();

which should launch a window having title "Compose Mail", with Subject and Body populated.

To include "cc" you can place

cc=email@domain.com&amp; 

within the string following last

&amp;

on the

?subject=

line, see mailto with multiple cc addresses.

Or write the service for your own online email application to achieve the same handling of requests for specific protocols.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!