Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP发送html表单电子邮件_Php_Email - Fatal编程技术网

使用PHP发送html表单电子邮件

使用PHP发送html表单电子邮件,php,email,Php,Email,当我单击提交按钮时,它会调出我的form-to-email.php源代码。这是我第一次使用PHP。我如何让它发送电子邮件?它可能只是链接不正确或格式不正确 <form method="post" name="myemailform" action="form-to-email.php"> <div> <label for="name">Name:</label><br> <input type="text" id="name

当我单击提交按钮时,它会调出我的form-to-email.php源代码。这是我第一次使用PHP。我如何让它发送电子邮件?它可能只是链接不正确或格式不正确

<form method="post" name="myemailform" action="form-to-email.php">
<div>
  <label for="name">Name:</label><br>
 <input type="text" id="name" name="user_name" size="45"><br>
</div>
<div>
  <label for="email">Email:</label><br>
  <input type="text" id="email" name="user_email" size="45"><br>
</div>
<div>
  <label for="message">Message:</label><br>
  <textarea type="text" id="message" name="user_message" size="600"></textarea><br>
</div>
<div>
  <input type = "submit" value = "Send Form">
  <input type = "reset" value = "reset">
</div>
</form>

名称:

电子邮件:

信息:

这是我的PHP代码

<?php
if(isset($_POST['email'])) {

// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "codymaheu@yahoo.com";
$email_subject = "Testing";

function died($error) {
    // your error code can go here
    echo "We are very sorry, but there were error(s) found with the form you submitted. ";
    echo "These errors appear below.<br /><br />";
    echo $error."<br /><br />";
    echo "Please go back and fix these errors.<br /><br />";
    die();
}


// validation expected data exists
if(!isset($_POST['user_name']) ||
    !isset($_POST['user_email']) ||
    !isset($_POST['user_message'])) {
    died('We are sorry, but there appears to be a problem with the form you submitted.');       
 }



$name = $_POST['user_name']; // required
$email_from = $_POST['user_email']; // required
$comments = $_POST['user_message']; // required


$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}

$string_exp = "/^[A-Za-z .'-]+$/";

if(!preg_match($string_exp,$name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
 }


if(strlen($comments) < 2) {
$error_message .= 'The Message you entered do not appear to be valid.<br />';
}

if(strlen($error_message) > 0) {
died($error_message);
}

$email_message = "Form details below.\n\n";


function clean_string($string) {
  $bad = array("content-type","bcc:","to:","cc:","href");
  return str_replace($bad,"",$string);
}



$email_message .= "First Name: ".clean_string($name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Message: ".clean_string($comments)."\n";

// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();

@mail($email_to, $email_subject, $email_message, $headers);  
?>

<!-- include your own success html here -->

Thank you for contacting us. We will be in touch with you very soon.

<?php

}
?>

感谢您联系我们。我们将很快与您联系。

像这样在按钮中添加名称,然后重试

<input type = "submit" value = "Send Form" name="email">