2016-07-31 107 views
0

我想擁有它,因此當用戶發送表單時,主題行顯示用戶名稱,如「來自[用戶名稱]的查詢」。它現在如何設置,它的主題是「從站點聯繫表單」。我如何修改代碼來實現這一點?聯繫人表單名稱字符串

<?php 
// configure 
$from = '[email protected]'; 
$sendTo = '[email protected]'; 
$subject = 'Contact form at site'; 
$fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' => 'Phone', 'email' => 'Email', 'message' => 'Message'); // array variable name => Text to appear in email 
$okMessage = 'Your Message was successfully submitted. Thank you! We will get back to you soon.'; 
$errorMessage = 'There was an error while submitting the form. Please try again or call us at 970-201-9619'; 

// let's do the sending 

try 
{ 
    $emailText = "You have new message from your Website\n=============================\n"; 

    foreach ($_POST as $key => $value) { 

     if (isset($fields[$key])) { 
      $emailText .= "$fields[$key]: $value\n"; 
     } 
    } 

    mail($sendTo, $subject, $emailText, "From: " . $from); 

    $responseArray = array('type' => 'success', 'message' => $okMessage); 
} 
catch (\Exception $e) 
{ 
    $responseArray = array('type' => 'danger', 'message' => $errorMessage); 
} 

if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { 
    $encoded = json_encode($responseArray); 

    header('Content-Type: application/json'); 

    echo $encoded; 
} 
else { 
    echo $responseArray['message']; 
} 
+0

請提供有關表格(字段名稱)的更多信息。 – Tom

+0

「顯示用戶」如何? – McStuffins

+0

主題行是否顯示用戶名?像主題將'拉胡爾的郵件'。 –

回答

0

根據您的要求對主題進行了更改。我已將主題變量移到郵件發送代碼上方,並將用戶的姓名和姓氏與郵件的主題行連接起來。請檢查下面的代碼段,並讓我知道你是否有任何疑問。

<?php 
// configure 
$from = '[email protected]'; 
$sendTo = '[email protected]'; 
$fields = array('name' => 'Name', 'surname' => 'Surname', 'phone' => 'Phone', 'email' => 'Email', 'message' => 'Message'); // array variable name => Text to appear in email 
$okMessage = 'Your Message was successfully submitted. Thank you! We will get back to you soon.'; 
$errorMessage = 'There was an error while submitting the form. Please try again or call us at 970-201-9619'; 

// let's do the sending 

try 
{ 
    $emailText = "You have new message from your Website\n=============================\n"; 

    foreach ($_POST as $key => $value) { 

     if (isset($fields[$key])) { 
      $emailText .= "$fields[$key]: $value\n"; 
     } 
    } 
    $subject = 'Inquiry from '.$_POST['name']." ".$_POST['surname']; 
    mail($sendTo, $subject, $emailText, "From: " . $from); 

    $responseArray = array('type' => 'success', 'message' => $okMessage); 
} 
catch (\Exception $e) 
{ 
    $responseArray = array('type' => 'danger', 'message' => $errorMessage); 
} 

if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { 
    $encoded = json_encode($responseArray); 

    header('Content-Type: application/json'); 

    echo $encoded; 
} 
else { 
    echo $responseArray['message']; 
} 
?> 
+0

這樣做的伎倆。太感謝了!! – Dwayne

+0

@Dwayne請接受我的答案,如果它幫助你。歡迎:) –