2017-03-09 53 views
-1

我是PHP的新手和那些複雜的東西。我可以讀它,只是沒有寫得那麼好。 但是,這裏的問題是:我如何將填寫聯繫表單的人員發送給確認郵件? 我希望按鈕「提交」也發送郵件給他們。但是一個不同的信息。PHP發送電子郵件給該人作爲確認

<form name="contactform" method="post" action="send_form_email.php"> 

    <table width="450px"> 

    <tr> 

    <td valign="top"> 

    <label for="first_name">First Name *</label> 

    </td> 

     <td valign="top"> 

    <input type="text" name="first_name" maxlength="50" size="30"> 

    </td> 

    </tr> 

<tr> 

<td valign="top"> 

    <label for="last_name">Last Name *</label> 

</td> 

<td valign="top"> 

    <input type="text" name="last_name" maxlength="50" size="30"> 

    </td> 

</tr> 

<tr> 

<td valign="top"> 

<label for="email">Email Address *</label> 

</td> 

    <td valign="top"> 

    <input type="text" name="email" maxlength="80" size="30"> 

    </td> 

    </tr> 

    <tr> 

    <td valign="top"> 

    <label for="telephone">Telephone Number</label> 

    </td> 

    <td valign="top"> 

    <input type="text" name="telephone" maxlength="30" size="30"> 

    </td> 

    </tr> 

    <tr> 

    <td valign="top"> 

    <label for="comments">Comments *</label> 

    </td> 

    <td valign="top"> 

    <textarea name="comments" maxlength="1000" cols="25" rows="6"></textarea> 

    </td> 

    </tr> 

    <tr> 

    <td colspan="2" style="text-align:center"> 

    <input type="submit" value="Submit"> <a href="http://www.freecontactform.com/email_form.php">Email Form</a> 

    </td> 

    </tr> 

    </table> 

    </form> 

這是我的HTML, 的PHP/

<?php 

if(isset($_POST['email'])) { 



// EDIT THE 2 LINES BELOW AS REQUIRED 

$email_to = "[email protected]"; 

$email_subject = "Reservering"; 





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['first_name']) || 

    !isset($_POST['last_name']) || 

    !isset($_POST['email']) || 

    !isset($_POST['telephone']) || 

    !isset($_POST['comments'])) { 

    died('We are sorry, but there appears to be a problem with the form you  submitted.'); 

} 



$first_name = $_POST['first_name']; // required 

$last_name = $_POST['last_name']; // required 

$email_from = $_POST['email']; // required 

$telephone = $_POST['telephone']; // not required 

$comments = $_POST['comments']; // 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,$first_name)) { 

$error_message .= 'The First Name you entered does not appear to be valid. <br />'; 

} 

if(!preg_match($string_exp,$last_name)) { 

$error_message .= 'The Last Name you entered does not appear to be valid.<br />'; 

} 

if(strlen($comments) < 2) { 

$error_message .= 'The Comments 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($first_name)."\n"; 

$email_message .= "Last Name: ".clean_string($last_name)."\n"; 

$email_message .= "Email: ".clean_string($email_from)."\n"; 

$email_message .= "Telephone: ".clean_string($telephone)."\n"; 

$email_message .= "Comments: ".clean_string($comments)."\n"; 

這些都是獲取來自:http://www.freecontactform.com/email_form.php

一些片斷我在網上找到:

$headers = 'From: '.$email_from."\r\n". 

'Reply-To: '.$email_from."\r\n" . 

'X-Mailer: PHP/' . phpversion(); 

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

-

$to  = '$email_from'; 
$subject = 'the subject'; 
$message = 'hello'; 
$headers = 'From: [email protected]' . "\r\n" . 
'Reply-To: [email protected]' . "\r\n" . 
'X-Mailer: PHP/' . phpversion(); 

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

爲什麼要這樣做呢,對於一個學校項目,我需要爲一個虛構的主題欄創建一個網站。但我希望人們能夠「訂購」門票。只是簡單的「我想在這一天的這種氣氛」。我可以實現我的自我。這只是爲了確認用戶問題,爲什麼我發佈這個。 謝謝你和我一起思考! :)

+0

你可以使用一個名爲[PHPMailer]的庫(https://github.com/PHPMailer/PHPMailer)。有一個文件夾,裏面有如何使用它的例子 –

+0

如果你使用本地主機,那麼郵件功能將不起作用。你可以使用phpmailer –

+0

第二個你在網上找到的東西幾乎從PHP的文檔http://php.net/manual/en/function.mail.php中解脫出來,你可以使用內置的mail()如果它在服務器上,可以從PHP發送郵件,如'if(mail($ email_to,$ email_subject,$ email_message,$ email_from)){*任何你想做的事情,如果它很好*} else {*任何你想要的如果它沒有發送*}'。另外你發現的第一個例子使用@mail你不應該使用它,因爲它只是抑制錯誤 – PhilS

回答

0

首先要把THT $ ERROR_MESSAGE和$值陣列,使得以存儲通過$ _ POST所有的錯誤和封郵件表單數據,那麼你可以顯示他們提交

<?php 
function send_email($values) 
{ 
$to  = $_POST['name']; 
$subject = 'the subject'; 
$message = $values; 
$headers = 'From:'.$_POST['email']."\r\n" . 
'Reply-To: [email protected]' . "\r\n" . 
'X-Mailer: PHP/' . phpversion(); 

    mail($to, $subject, $message, $headers); 
    } 
    //now 

    if(isset($submit)) 
    { 
     if($error_message) 
     { 
     display_form($error); 
     } 
     if(!$error_message) 
     { 
     send_email(); 
     header("Location:index.php"); 
    } 
    ?> 
+0

由於某種原因,我不斷收到一條消息它發生了錯誤,即使是你發送的信息。但它現在有用,謝謝你的意見! :) – hmforall

+0

消息只是您的澄清@hmforall的一個例子 –