2017-04-04 39 views
0

我在我的網站上發送了一份問卷調查表,通過電子郵件向我發送由訪問者提交的答案。其中一個問題要求用戶提供最佳聯繫時間(早上,下午,晚上)選擇課程的名稱是聯繫時間。如何替換電子郵件正文中的字段名稱

目前在電子郵件的正文中,它顯示爲聯繫時間,就像我在html表單中命名的那樣。我希望它顯示爲聯繫時間。

我該如何構建電子郵件的主體,以便將聯繫時間顯示爲聯繫時間,同時仍能夠從選擇的類名爲contact-time中獲取值?我已經瀏覽了幾乎所有關於此的問題,並且找不到任何相關信息。

是建立電子郵件然後

PHP代碼,並將其發送

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

謝謝你的時間提前,我感謝所有幫助。

回答

1

無關的郵件代碼,你可以用一個簡單的str_replace()$name

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

或約10其他選項

相關問題