简体   繁体   English

发送电子邮件的html按钮

[英]html button to send email

How do I send an email with specified initial values for the headers subject and message from a button in html, such as this如何从 html 中的按钮发送带有指定初始值的电子邮件,例如标题subjectmessage

<form method="post" action="mailto:email.com?subject=subject&message=message">

where subject and message are values fetched from a form ?其中subjectmessage是从form获取的值?

You can use mailto , here is the HTML code:您可以使用mailto ,这是 HTML 代码:

<a href="mailto:EMAILADDRESS">

Replace EMAILADDRESS with your email.EMAILADDRESS替换为您的电子邮件。

This method doesn't seem to work in my browser, and looking around indicates that the whole subject of specifying headers to a mailto link/action is sparsely supported, but maybe this can help...这种方法似乎在我的浏览器中不起作用,环顾四周表明很少支持将标题指定到mailto链接/操作的整个主题,但也许这可以帮助...

HTML: HTML:

<form id="fr1">
    <input type="text" id="tb1" />
    <input type="text" id="tb2" />
    <input type="button" id="bt1" value="click" />
</form>

JavaScript (with jQuery): JavaScript(使用 jQuery):

$(document).ready(function() {
    $('#bt1').click(function() {
        $('#fr1').attr('action',
                       'mailto:test@test.com?subject=' +
                       $('#tb1').val() + '&body=' + $('#tb2').val());
        $('#fr1').submit();
    });
});

Notice what I'm doing here.注意我在这里做什么。 The form itself has no action associated with it.表单本身没有与之关联的操作。 And the submit button isn't really a submit type, it's just a button type.而且提交按钮并不是真正的submit类型,它只是一种button类型。 Using JavaScript, I'm binding to that button's click event, setting the form's action attribute, and then submitting the form.使用 JavaScript,我绑定到该按钮的点击事件,设置表单的 action 属性,然后提交表单。

It's working in so much as it submits the form to a mailto action (my default mail program pops up and opens a new message to the specified address), but for me (Safari, Mail.app) it's not actually specifying the Subject or Body in the resulting message.它的工作原理是将表单提交给mailto操作(我的默认邮件程序会弹出并打开指定地址的新邮件),但对我(Safari、Mail.app)而言,它实际上并没有指定主题或正文在结果消息中。

HTML isn't really a very good medium for doing this, as I'm sure others are pointing out while I type this. HTML 并不是一个很好的媒介,因为我确信其他人在我输入时指出了这一点。 It's possible that this may work in some browsers and/or some mail clients.这可能适用于某些浏览器和/或某些邮件客户端。 However, it's really not even a safe assumption anymore that users will have a fat mail client these days.然而,现在用户将拥有胖邮件客户端,这甚至不再是一个安全的假设。 I can't remember the last time I opened mine.我不记得上次打开我的是什么时候了。 HTML's mailto is a bit of legacy functionality and, these days, it's really just as well that you perform the mail action on the server-side if possible. HTML 的mailto是一些遗留功能,现在,如果可能的话,您在服务器端执行邮件操作真的一样好。

As David notes , his suggestion does not actually fulfill the OP's request, which was an email with subject and message .正如大卫所指出的,他的建议实际上并没有满足 OP 的要求,即一封带有主题和消息的电子邮件。 It doesn't work because most, maybe all, combinations of browsers plus e-mail clients do not accept the subject and body attributes of the mailto: URI when supplied as a <form> 's action .它不起作用,因为当作为<form>action提供时,大多数(也许是所有)浏览器和电子邮件客户端的组合不接受mailto: URI 的subjectbody属性。

But here's a working example:但这是一个工作示例:

HTML (with Bootstrap styles): HTML(使用Bootstrap样式):

<p><input id="subject" type="text" placeholder="type your subject here" 
    class="form-control"></p>
<p><input id="message" type="text" placeholder="type your message here" 
    class="form-control"></p>
<p><a id="mail-link" class="btn btn-primary">Create email</a></p>

JavaScript (with jQuery ): JavaScript(使用jQuery ):

<script type="text/javascript">
    function loadEvents() {
        var mailString;
        function updateMailString() {
            mailString = '?subject=' + encodeURIComponent($('#subject').val())
                + '&body=' + encodeURIComponent($('#message').val());
            $('#mail-link').attr('href',  'mailto:person@email.com' + mailString);
        }
        $( "#subject" ).focusout(function() { updateMailString(); });
        $( "#message" ).focusout(function() { updateMailString(); });
        updateMailString();
    }
</script>

Notes:笔记:

  • The <form> element with associated action attribute is not used.不使用带有关联action属性的<form>元素。
  • The <input> element of type button is also not used. button类型的<input>元素也未使用。
    • <a> styled as a button (here using Bootstrap) replaces <input type="button"> <a>样式为按钮(此处使用 Bootstrap)替换<input type="button">
    • focusout() with updateMailString() is necessary because the <a> tag's href attribute does not automatically update when the input fields' values change. focusout()updateMailString()是必要的,因为<a>标记的href属性不会在输入字段的值更改时自动更新。
    • updateMailString() is also called when document is loaded in case the input fields are prepopulated. updateMailString()也会在加载文档时调用,以防输入字段已预先填充。
  • Also encodeURIComponent() is used to get characters such as the quotation mark (") across to Outlook. encodeURIComponent()还用于将引号 (") 等字符传递到 Outlook。

In this approach, the mailto: URI is supplied (with subject and body attributes) in an a element's href tag.在这种方法中, mailto: URI 是在a元素的href标签中提供的(带有subjectbody属性)。 This works in all combinations of browsers and e-mail clients I have tested, which are recent (2015) versions of:这适用于我测试过的浏览器和电子邮件客户端的所有组合,它们是最近的(2015)版本:

  • Browsers: Firefox/Win&OSX, Chrome/Win&OSX, IE/Win, Safari/OSX&iOS, Opera/OSX浏览器: Firefox/Win&OSX、Chrome/Win&OSX、IE/Win、Safari/OSX&iOS、Opera/OSX
  • E-mail clients: Outlook/Win, Mail.app/OSX&iOS, Sparrow/OSX电子邮件客户端: Outlook/Win、Mail.app/OSX&iOS、Sparrow/OSX

Bonus tip: In my use cases, I add some contextual text to the e-mail body .额外提示:在我的用例中,我在电子邮件body添加了一些上下文文本。 More often than not, I want that text to contain line breaks.通常,我希望该文本包含换行符。 %0D%0A (carriage return and linefeed) works in my tests. %0D%0A (回车和换行)在我的测试中有效。

I couldn't ever find an answer that really satisfied the original question, so I put together a simple free service ( PostMail ) that allows you to make a standard HTTP POST request to send an email.我找不到真正满足原始问题的答案,所以我组合了一个简单的免费服务 ( PostMail ),它允许您发出标准的 HTTP POST 请求来发送电子邮件。 When you sign up, it provides you with code that you can copy & paste into your website.当您注册时,它会为您提供代码,您可以将这些代码复制并粘贴到您的网站中。 In this case, you can simply use a form post:在这种情况下,您可以简单地使用表单帖子:

HTML: HTML:

<form action="https://postmail.invotes.com/send"
    method="post" id="email_form">

    <input type="text" name="subject" placeholder="Subject" />
    <textarea name="text" placeholder="Message"></textarea>
    <!-- replace value with your access token -->
    <input type="hidden" name="access_token" value="{your access token}" />

    <input type="hidden" name="success_url"
      value=".?message=Email+Successfully+Sent%21&isError=0" />
    <input type="hidden" name="error_url"
      value=".?message=Email+could+not+be+sent.&isError=1" />

    <input id="submit_form" type="submit" value="Send" />
</form>

Again, in full disclosure, I created this service because I could not find a suitable answer.再次,完全公开,我创建此服务是因为我找不到合适的答案。

@user544079 @用户544079

Even though it is very old and irrelevant now, I am replying to help people like me!尽管它现在很旧且无关紧要,但我正在回复以帮助像我这样的人! it should be like this:它应该是这样的:

<form method="post" action="mailto:$emailID?subject=$MySubject &message= $MyMessageText">

Here $emailID, $MySubject, $MyMessageText are variables which you assign from a FORM or a DATABASE Table or just you can assign values in your code itself.这里 $emailID、$MySubject、$MyMessageText 是您从 FORM 或 DATABASE 表分配的变量,或者您可以在代码本身中分配值。 Alternatively you can put the code like this (normally it is not used):或者,您可以像这样放置代码(通常不使用):

<form method="post" action="mailto:admin@website.com?subject=New Registration Alert &message= New Registration requires your approval">

You can use an anchor to attempt to open the user's default mail client, prepopulated, with mailto: , but you cannot send the actual email.您可以使用锚点来尝试打开用户的默认邮件客户端,预填充的mailto: ,但您无法发送实际的电子邮件。 *Apparently it is possible to do this with a form action as well, but browser support is varied and unreliable, so I do not suggest it. *显然也可以通过表单操作来做到这一点,但浏览器支持多种多样且不可靠,所以我不建议这样做。

HTML cannot send mail, you need to use a server side language like php, which is another topic. HTML不能发送邮件,需要使用php这样的服务器端语言,这是另外一个话题。 There are plently of good resources on how to do this here on SO or elsewhere on the internet.有很多关于如何在 SO 上或互联网上的其他地方执行此操作的良好资源。

If you are using php, I see SwiftMailer suggested quite a bit.如果您使用的是 php,我看到SwiftMailer建议了很多。

You can not directly send an email with a HTML form.您不能直接发送带有 HTML 表单的电子邮件。 You can however send the form to your web server and then generate the email with a server side program written in eg PHP.但是,您可以将表单发送到您的 Web 服务器,然后使用用 PHP 等编写的服务器端程序生成电子邮件。

The other solution is to create a link as you did with the "mailto:".另一种解决方案是像使用“mailto:”一样创建一个链接。 This will open the local email program from the user.这将从用户打开本地电子邮件程序。 And he/she can then send the pre-populated email.然后他/她可以发送预先填充的电子邮件。

When you decided how you wanted to do it you can ask another (more specific) question on this site.当你决定了你想怎么做时,你可以在这个网站上问另一个(更具体的)问题。 (Or you can search for a solution somewhere on the internet.) (或者您可以在互联网上的某处搜索解决方案。)

 <form action="mailto:someone@example.com" method="post"               enctype="text/plain">
 Name:<br>
<input type="text" name="name"><br>
 E-mail:<br>
<input type="text" name="mail"><br>
Comment:<br>
<input type="text" name="comment" size="50"><br><br>
<input type="submit" value="Send">
<input type="reset" value="Reset">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM