2016-07-31 11 views
0

私はそれを持っているようにしようとしています。ユーザーがフォームを送信するときに、「[Name of user]からのお問い合わせ」のようなユーザーの名前が表示されます。どのようにセットアップされているのかは、「サイトからの連絡フォーム」という件名で私に届きます。どのようにコードを修正してそのようにすることができますか?連絡先フォームの名前文字列

<?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

件名にユーザーの名前が表示されますか?同じような主題は 'Rahulからのメール'です。 –

答えて

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それがあなたを助けたら私の答えを受け入れてください。ようこそ :) –

関連する問題