联系表格不发送给客户

时间:2013-12-04 11:12:28

标签: php html forms smtp contact

我在客户网站上设置的联系表单存在问题,表单发布到我的电子邮件地址没有问题,但不是我的客户通过123 reg和电子邮件帐户注册的域名设置Gmail中。在联系我的网络主机时,他们建议使用SMTP,这是我以前从未使用过的东西,我用谷歌搜索了这个但是没有任何亮点。有人有什么想法吗?

谢谢, 梅丽莎

<?php
if (array_key_exists('submit', $_POST)) {
$to = 'hello@email.co.uk'; //change this to suit your admin email address
$subject = 'Website enquiry'; //change this to the mail subject you want the admin to receive

$expected = array('name', 'email', 'tel', 'message1'); //list all of the fields in the form
$required = array('name', 'email', 'message1'); //list the required fields in the form
$missing = array();

$suspect = false;
$pattern = '/Content-Type:|Bcc:|Cc:/i';

function isSuspect($val, $pattern, &$suspect) {
if (is_array($val)) {
  foreach ($val as $item) {
    isSuspect($item, $pattern, $suspect);
    }
  }
else {
  if (preg_match($pattern, $val)) {
    $suspect = true;
    }
  }
}

isSuspect($_POST, $pattern, $suspect);

if ($suspect) {
$mailSent = false;
unset($missing);
}
else {
foreach ($_POST as $key => $value) {
  $temp = is_array($value) ? $value : trim($value);
  if (empty($temp) && in_array($key, $required)) {
    array_push($missing, $key);
    }
  elseif (in_array($key, $expected)) {
    ${$key} = $temp;
    }
  }
}

if (!empty($email)) {
$checkEmail = '/^[^@]+@[^\s\r\n\'";,@%]+$/';
if (!preg_match($checkEmail, $email)) {
  array_push($missing, 'email');
  }
}


if (!$suspect && empty($missing)) { 

    $message .= "Name: $name\n\n";
    $message .= "Email address: $email\n\n";
    $message .= "Telephone number: $tel\n\n";
    $message .= "Message: $message1\n\n";

    $message = wordwrap($message, 70);

    $additionalHeaders = "From: $email";

    $complete = mail($to, $subject, $message, $additionalHeaders);
    if ($complete) {
      unset($missing);
    }
}
}
?>

以下显示表格:

<?php 
                if ($_POST && isset($missing)) {
                    echo'<p class="warning">Please complete the missing or incorrect fields.</p>';
                }
                if ($_POST && isset($complete)) {
                    echo '<p class="green"><strong>Thank you for contacting us. We will be in touch soon.</strong></p>';
                }
                else{?>


                <form method="post" action="" id="contact_form" class="validate">

                    <fieldset>

                        <legend>Your Details</legend>

                        <label for="name"><b>Full Name</b></label>
                        <input name="name" placeholder="John Smith" class="" <?php if (isset($missing)) {echo 'value="'.htmlentities($_POST['name']).'"';} ?> />
                        <br>
                        <label for="email"><b>Email Address</b></label>
                        <input type="email" name="email" placeholder="hello@shnuggle.com" class="" <?php if (isset($missing)) {echo 'value="'.htmlentities($_POST['email']).'"';} ?> />
                        <br>
                        <label for="phone"><b>Phone Number <span class="note">(optional)</span></b></label>
                        <input type="tel" name="tel" placeholder="+44 (0)28 9012 3456" class="" <?php if (isset($missing)) {echo 'value="'.htmlentities($_POST['tel']).'"';} ?> />
                        <br>
                        <legend>Your Message</legend>

                        <textarea name="message1" <?php if (isset($missing)) {echo 'value="'.htmlentities($_POST['message1']).'"';} ?> />Hello,</textarea>

                    </fieldset>

                    <!--<button type="submit" class="btn submit"><span class="icon">m</span>&nbsp;&nbsp;Send Message</button>-->
                    <input type="submit" class="btn" name="submit" value="Send Message" />
                    <button type="reset" class="btn reset">Clear form</button>

                </form>
                <?php }?> 

1 个答案:

答案 0 :(得分:0)

这是工作!!
如果在54行上有错误未定义变量 比转换

 $message .= "Name: $name\n\n";

 $message = "Name: $name\n\n";