简体   繁体   English

如何从HTML5表单发送电子邮件

[英]How to send an email from an HTML5 form

I am having issues trying to submit a form and have it email the data submitted. 我在尝试提交表单并将其通过电子邮件发送提交的数据时遇到问题。 I understand this is usually done but I seem to be having problems figuring out what's wrong here. 我知道通常会这样做,但是我似乎在找出问题所在时遇到了问题。 So the current code is. 所以当前的代码是。

 <div class="contact" id="contact">
        <div class="wrap">
            <h3>CONTACT US</h3>
            <p class="a">Free Estimates</p>
            <div class="form">
                 <div class="form-text">
                 <form action="../contact.php">
                    <input type="text" class="textbox" value="Your Name*" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Name';}">
                    <input type="text" class="textbox" value="Your E-mail*" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'E-mail';}">
                    <input name="PNumber" type="text" class="Subject" value="Phone Number" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Website';}">
                </div>
                     <textarea placeholder="Your Message*"></textarea>
                     <div class="clear"> </div>
                     <input type="submit" value="SEND MESSAGE">                  
             </div>
     </div>
     </div>

I have a file called mailfrom.php but I think this is where I'm having problems 我有一个名为mailfrom.php的文件,但我认为这是我遇到问题的地方

<?php
$to      = 'contact@qixty.com';
$subject = 'New Request from Website';
$message = $_POST['mytextarea'];
$headers = 'From: webmaster@qixty.com' . "\r\n" .
'Reply-To: contact@qixty.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?>

After I hit submit on the main page, I am brought to a new blank page. 在主页上单击“提交”后,将带到新的空白页。

Changes the URL from http://qixty.com to http://qixty.com/mailfrom.php?PNumber=Phone+Number 将网址从http://qixty.com更改为http://qixty.com/mailfrom.php?PNumber=Phone+Number

Any help would be great. 任何帮助都会很棒。 I know I could just insert a link to mailto like this 我知道我可以像这样插入mailto的链接

<a href="mailto:contact@qixty.com?cc=faulkerson@gmail.com" class="btn" data-type="submit">Send</a>

But that doesn't post the form data. 但这不会发布表单数据。 I would like to have it send an email to me with the form data in the body of the message. 我希望它向我发送一封电子邮件,在邮件正文中包含表单数据。

You need to: 你需要:

  • Add method="POST" to the <form> method="POST"添加到<form>
  • Add name attributes (which match the keys you are trying to use in $_POST ) to your form controls (eg <textarea name="mytextarea"></textarea> ). name属性(与您尝试在$_POST使用的键匹配)添加到表单控件中(例如<textarea name="mytextarea"></textarea> )。

If you don't want a blank page to be displayed to the browser, then output some content from the PHP script as well as just calling mail() . 如果您不希望在浏览器中显示空白页,则可以从PHP脚本输出一些内容,而不仅仅是调用mail()

You should also stop abusing the value attribute as a <label> . 您还应该停止滥用value属性作为<label> Use a proper <label> element and leave the value attribute for providing a default value. 使用适当的<label>元素,并保留value属性以提供默认值。

if your action file name is mailform.php then this 如果您的操作文件名为mailform.php则此

<form action="../contact.php">

should be 应该

<form action="../mailform.php">

if both files are in same directory then remove ../ from action attribute. 如果两个文件都在同一目录中,则从动作属性中删除../

UPDATE 更新


form 形成


<div class="contact" id="contact">
        <div class="wrap">
            <h3>CONTACT US</h3>
            <p class="a">Free Estimates</p>
            <div class="form">
                 <div class="form-text">
                 <form method='post' action="../mailform.php">
                    <input type="text" class="textbox" value="Your Name*" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Name';}">
                    <input type="text" class="textbox" value="Your E-mail*" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'E-mail';}">
                    <input name="PNumber" type="text" class="Subject" value="Phone Number" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Website';}">
                </div>
                     <textarea name='mytextarea' placeholder="Your Message*"></textarea>
                     <div class="clear"> </div>
                     <input type="submit" value="SEND MESSAGE">                  
             </div>
     </div>
     </div>

mailform.php mailform.php


$to      = 'contact@qixty.com';
$subject = 'New Request from Website';
$message = $_POST['mytextarea'];
$headers = 'From: webmaster@qixty.com' . "\r\n" .
'Reply-To: contact@qixty.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();

$send = mail($to, $subject, $message, $headers);

// this will help you to get the status mail sent or not
if($send) :
   echo "Email sent";
else :
    echo "Email sending failed";
endif;

This question has been answered. 这个问题已经回答。 I had html issues php was fine. 我有html问题,php很好。

<div class="form">
                 <div class="form-text">
                 <form action="../mailfrom.php" method="post">
                    <input type="text" class="textbox" value="Your Name*" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Name';}">
                    <input type="text" class="textbox" value="Your E-mail*" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'E-mail';}">
                    <input name="PNumber" type="text" class="Subject" value="Phone Number" onfocus="this.value = '';" onblur="if (this.value == '') {this.value = 'Website';}">
                </div>
                     <textarea name="mytextarea" placeholder="Your Message*"></textarea>
                     <div class="clear"> </div>
                     <input type="submit" Label="SEND MESSAGE">                  
             </div>

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

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