2017-07-06 80 views
0

當我點擊我的提交按鈕時,它會拉起我的form-to-email.php源代碼。這是我第一次使用任何PHP。我如何獲得它發送電子郵件?它可能只是不正確的鏈接或格式不正確。用PHP發送html格式的郵件

<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 = "[email protected]"; 
$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._%-][email protected][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 

} 
?> 
+0

https://stackoverflow.com/questions/18379238/send-email -with-PHP-從-HTML表單上提交 - 用最相同的腳本 –

回答

0

添加名字變成像這樣的按鈕,然後再試

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