如何替换电子邮件正文中字段的名称

时间:2017-04-04 04:06:13

标签: php html forms phpmailer

我的网站上有一份问卷表格,通过电子邮件向我发送访问者提交的答案。其中一个问题要求用户提供最佳联系时间(上午,下午,晚上)。该选定班级的名称为联系时间。

目前在电子邮件正文中,它显示为联系时间,就像我在html表单中命名一样。我希望它显示为联系时间。

如何构建电子邮件正文,以便将联系时间显示为“联系时间”,同时仍然可以从名为“联系时间”的选定类中获取值?我已经查看了很多关于此的问题,但找不到任何相关信息。

构建电子邮件然后发送它的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@domain.com';
  $email->Password = 'MyPassword';

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

  $email->setFrom($_POST['email'], $_POST['name']);
  $email->addAddress('email@domain.com');
  $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 个答案:

答案 0 :(得分:1)

与您在str_replace()

上使用简单$name的邮件代码无关

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

或大约10个其他选项