2017-04-04 1 views
0

私のウェブサイトにはアンケートフォームがあり、訪問者からの回答がメールで届きます。質問の1つは、ユーザーに最良の連絡時間(朝、午後、夕方)を要求します。その選択クラスの名前は連絡時間です。電子メールの本文にあるフィールドの名前を置き換える方法

現在、電子メールの本文には、html形式で名前を付けたように連絡先時刻として表示されます。コンタクトタイムとして表示したい。

電子メールの本文を作成してコンタクトタイムをコンタクト時間として表示し、それでもコンタクトタイムという選択クラスから値をフェッチできるようにするにはどうすればよいですか?私はこれに関するすべての質問を見てきましたが、関連する情報は見つかりませんでした。

メールを構築し、その後

<?php 
    function constructEmailBody() { 
    $fields = array("name" => true, "email" => true, "address" => true, "phone" => true, "contact-time" => true); 
    $message_body = ""; 
    foreach ($fields as $name => $required) { 
     $postedValue = $_POST[$name]; 
     if ($required && empty($postedValue)) { 
     errorResponse("$name is empty."); 
     } else { 
     $message_body .= ucfirst($name) . ": " . $postedValue . "\n"; 
     } 
    } 
    return $message_body; 
    } 

    //attempt to send email 
    $emailBody = constructEmailBody(); 
    require './vender/php_mailer/PHPMailerAutoload.php'; 
    $email = new PHPMailer; 
    $email->CharSet = 'UTF-8'; 
    $email->isSMTP(); 
    $email->Host = 'smtp.gmail.com'; 
    $email->SMTPAuth = true; 
    $email->Username = '[email protected]'; 
    $email->Password = 'MyPassword'; 

    $email->SMTPSecure = 'tls'; 
    $email->Port = 587; 

    $email->setFrom($_POST['email'], $_POST['name']); 
    $email->addAddress('[email protected]'); 
    $email->Subject = 'Estimate Request Answers'; 
    $email->Body = $emailBody; 
    //try to send the message 
    if($email->send()) { 
    echo json_encode(array('message' => 'Thank you! Your message was successfully submitted.')); 
    } else { 
    errorResponse('An unexpected error occured while attempting to send the email: ' . $email->ErrorInfo); 
    } 
?> 

HTMLフォーム

<form role="form" id="estimateForm" method="POST"> 
    <div class="col-xs-12 contact-information"> 
     <div class="form-group"> 
      <fieldset> 
       <legend>Contact Information</legend> 
       <div class="col-xs-12 col-sm-12 col-md-6 col-lg-6"> 
        <label class="control-label" for="name">Name</label> 
        <input type="text" class="form-control" id="name" name="name" placeholder="Enter Name"> 
        <label class="control-label" for="email">Email</label> 
        <input type="email" class="form-control" id="email" name="email" placeholder="Enter Email"> 
        <label class="control-label" for="address">Address</label> 
        <input type="text" class="form-control" id="address" name="address" placeholder="Enter Address"> 
       </div> 
       <div class="col-xs-12 col-sm-12 col-md-6 col-lg-6"> 
        <label class="control-label" for="phone">Phone Number</label> 
        <input type="tel" class="form-control" id="phone" name="phone" placeholder="Enter Phone Number"> 
        <label class="control-label" for="contact-time">Preferred Contact Time</label> 
        <select class="selectpicker contact-time" id="contact-time" name="contact-time" title="Preferred Contact Time"> 
         <option value="Morning">Morning (9-11am)</option> 
         <option value="Afternoon">Afternoon (11-2pm)</option> 
         <option value="Evening">Evening (2-5pm)</option> 
        </select> 
        <label class="control-label" for="contact-method">Preferred Contact Method</label> 
        <select class="selectpicker contact-method" id="contact-method" name="contact-method" title="Preferred Contact Method"> 
         <option>By Phone</option> 
         <option>By Email</option> 
        </select> 
       </div> 
      </fieldset> 
     </div> 
    </div> 
</form> 

それを送信するPHPコードは、事前にお時間をいただき、ありがとうございます、私は任意の助けに感謝します。

答えて

1

あなたは$name

または

if($name =='whatever'){ 
$name='something else'; 
} 

または約10他のオプション

に簡単な str_replace()を使用することができ、メールのコードとは何の関係
関連する問題