2012-12-06 24 views
1

我的聯繫表單來自TEST--如下面的代碼所示,但我需要它來自完成表單的人員,以便當電子郵件被回覆時到輸入的地址,而不是測試。帶有Smtp身份驗證的Web窗體

我是PHP新手,可能會請求幫助實施回覆?我已經嘗試過,不斷失去一些東西。

這是我的原始代碼。謝謝!

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

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['contact_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.'); 
} 

$contact_name = $_POST['contact_name']; // required 
$email_from = $_POST['email']; // required 
$telephone = $_POST['telephone']; // required 
$comments = $_POST['comments']; // not 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 0-9.'-]+$/"; 
    if(!preg_match($string_exp,$contact_name)) { 
$error_message .= 'The Contact Name you entered does not appear to be valid.<br />'; 
    } 
    if(!preg_match($string_exp,$telephone)) { 
$error_message .= 'The Telephone # you entered does 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 .= "Contact Name: ".clean_string($contact_name)."\n"; 
$email_message .= "Email: ".clean_string($email_from)."\n"; 
$email_message .= "Telephone: ".clean_string($telephone)."\n"; 
$email_message .= "Comments: ".clean_string($comments)."\n"; 

ini_set('display_errors',1); 
error_reporting(E_ALL); 

    require_once "Mail.php"; 

$from = "TEST<[email protected]>"; 
$to = "<[email protected]>"; 
$subject = "Test message"; 
$body = "$email_message"; 

$host = "mail.xxxxxxxx.com"; 
$username = "[email protected]"; 
$password = "xxxxxxxxxx"; 

headers = array ('From' => $from, 
'To' => $to, 
'Subject' => $subject); 
$smtp = Mail::factory('smtp', 
    array ('host' => $host, 
'auth' => true, 
'username' => $username, 
'password' => $password)); 

$mail = $smtp->send($to, $headers, $body); 

if (PEAR::isError($mail)) { 
    echo("<p>" . $mail->getMessage() . "</p>"); 
} else { 
    echo("<p></p>"); 
} 
?> 


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

<?php 
} 
?> 

回答

0

嘗試:

$from = $_POST['contact_name']; 

或者,如果你希望自己的電子郵件顯示:

$from = $_POST['email']; 
+0

感謝。但是由於SMTP身份驗證,這個$必須來自測試地址。這就是爲什麼我必須設置Reply-To,但由於缺乏PHP知識,我無法使其正確編程。我正在應對並尋求實施答覆的幫助。 – user1881380