简体   繁体   English

在 HTML 上发送电子邮件

[英]Send Email on HTML

I am making a website completely from scratch and I am facing a problem when I want to receive an email when the client presses the submit button.我正在完全从头开始制作网站,当我想在客户按下提交按钮时收到电子邮件时,我遇到了问题。 Here is my code on the form tag.这是我在表单标签上的代码。

What am I doing wrong?我究竟做错了什么?

<form id="contactform" method="post" action="MAILTO:myemail@hotmail.com?    subject=subject&message=message">
  <p class="contact1"><label for="name">Name</label></p>
  <input id="name" name="name" placeholder="First and last name" required="" tabindex="1" type="text">

  <p class="contact"><label for="email">Email</label></p>
  <input id="email" name="email" placeholder="example@hotmail.com" required="" tabindex="2" type="text">

  <p class="contact"><label for="Subject">Subject</label></p>
  <input id="subject" name="subject" placeholder="Subject" required="" tabindex="2" type="text">

  <p class="contact"><label for="comment">Your Message</label></p>
  <textarea name="comment" id="comment" tabindex="4"></textarea> <br>
  <input name="submit" id="submit" tabindex="5" value="Send Message" type="submit">
</form>

The solution:解决方案:

It is impossible to send emails with HTML, JavaScript, CSS or any client side language.不可能使用 HTML、JavaScript、CSS 或任何客户端语言发送电子邮件。 You have to go server side.你必须去服务器端。 The most common choice for emails is PHP.电子邮件最常见的选择是 PHP。 So your opening form tag should look like this:所以你的打开表单标签应该是这样的:

<form method="POST" action="mailer.php">

And then the code in mailer.php would be:然后 mailer.php 中的代码将是:

$name = $_POST["name"];
$message = $_POST["message"];
mail("your_email@site.com", "Subject", "Message from your site. \n Name: ".$name." \n Message: ".$message);

Bear in mind that this is a very basic email script and does not protect against script injection and XSS.请记住,这是一个非常基本的电子邮件脚本,不能防止脚本注入和 XSS。

In order to pull this off you would need some level scripting (server-side I think... not sure if client side can do this...) to hold your values in a placeholder variable, and then repaint the screen with the and then inject them into the <a> tag.为了实现这一点,您需要一些级别的脚本(我认为服务器端...不确定客户端是否可以这样做...)将您的值保存在占位符变量中,然后使用和重新绘制屏幕然后将它们注入<a>标签。

If you want to simply pre-populate a standard link with a SUbject line, even body text, and use the individuals default mail client, you can dress up the link as such:如果您想简单地使用主题行,甚至正文文本预先填充标准链接,并使用个人默认邮件客户端,您可以将链接装扮成这样:

<a href="mailto:someone@yoursite.com?subject=This is an example SL
&body=Your body copy can go here \n ... and new lines can be made like that.
&cc=justincaseyouwantit@yoursite.com&bcc=final-protection@yoursite.com">
Click here to send an email</a>

if you wanted to use a form to build those links you would have to capture the form's input as variables, and then build the link with those variables defined, and then echo it to the screen.如果您想使用表单来构建这些链接,您必须将表单的输入捕获为变量,然后使用定义的这些变量构建链接,然后将其回显到屏幕上。

DEMO演示

You're doing it totally wrong.你这样做完全错了。

When the user would press submit button.当用户按下提交按钮时。 The form would be redirected to the new page, with the link that is provided in the action attribute.表单将使用action属性中提供的链接重定向到新页面。

Remember, most of the users have some default Email Clients installed on their System.请记住,大多数用户在他们的系统上安装了一些默认的电子邮件客户端。 I have Google Mail set as Default, some folk out there might have Outlook, some of us would love to use Thunderbird (by Mozilla).我将 Google Mail 设置为默认值,有些人可能有 Outlook,我们中的一些人可能喜欢使用 Thunderbird(由 Mozilla 提供)。

These programs would have their own Algorithm to send emails.这些程序将有自己的算法来发送电子邮件。 So the code that you're using would never succeed.所以你使用的代码永远不会成功。 If does, then it would send you an email as如果是,那么它会向您发送一封电子邮件

Sender: client_email发件人:client_email
Subject: Subject主题: 主题
Body:身体:

No data in the body tag, since you're not including that method at all. body 标签中没有数据,因为您根本不包含该方法。

The above would be the email recieved to you.以上将是您收到的电子邮件。 Also, if you do so, the Form Data would be lost and the software would try to ask the user for data as a new cycle.此外,如果您这样做,表单数据将丢失,软件将尝试向用户询问数据作为新周期。

I think you need to do something like the following.我认为您需要执行以下操作。

<form id="contactform" method="post" action="send_email.html"> 
    <p class="contact1"><label for="name">Name</label></p> 
    <input id="name" name="name" placeholder="First and last name" required="" tabindex="1" type="text">     

    <p class="contact"><label for="email">Email</label></p> 
    <input id="email" name="email" placeholder="example@hotmail.com" required="" tabindex="2" type="text">       

    <p class="contact"><label for="Subject">Subject</label></p> 
    <input id="subject" name="subject" placeholder="Subject" required="" tabindex="2" type="text">   

    <p class="contact"><label for="comment">Your Message</label></p> 
    <textarea name="comment" id="comment" tabindex="4"></textarea> <br>
    <input name="submit" id="submit" tabindex="5" value="Send Message" type="submit">
</form>

And then, handle the Email sending request on the Server-side, and send it yourself using the Server-side Language you're using.然后,在服务器端处理电子邮件发送请求,并使用您正在使用的服务器端语言自行发送。

In that way, you'll also get the email, and the content that User wrote and tried to send you.这样,您还将收到电子邮件以及用户编写并尝试发送给您的内容。

I have to say: I am not a hundred percent sure.我不得不说:我不是百分百确定。 But it is not possible to send E-Mails via HTML.但是不可能通过 HTML 发送电子邮件。 You need some kind of Server.你需要某种服务器。 Maybe try the Google AppEngine it is free(until some limit), easy to lern and has an extra E-Mail API.也许试试 Google AppEngine,它是免费的(直到某些限制),易于学习并且有一个额外的电子邮件 API。

It may not always work.它可能并不总是有效。 Look at http://webdesign.about.com/cs/forms/a/aamailtobroke.htm看看http://webdesign.about.com/cs/forms/a/aamailtobroke.htm

Personally I never used such solution.我个人从未使用过这样的解决方案。 I use always PHP to send email我总是使用 PHP 来发送电子邮件

When I use your solution my e-mail client opens correctly but mail content doesn't look nice.当我使用您的解决方案时,我的电子邮件客户端可以正确打开,但邮件内容看起来不太好。

But when I use:但是当我使用:

<form action="mailto:admin@example.com" enctype="text/plain" method="post">
<p>Name: <input name="Name" type="text" id="Name" size="40"></p>
<p>E-mail address: <input name="E-mail" type="text" id="E-mail" size="40"></p>
<p>Comment:</p>
<p><textarea name="Comment" cols="55" rows="5"
id="Comment"></textarea></p>
<p><input type="submit" name="Submit" value="Submit"></p>
</form>

it works really fine它工作得很好

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

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