联系表格不发送电子邮件到我的具体地址

时间:2016-03-15 09:46:20

标签: forms contact

我的网站上有一个php联系表格。它在我的正常地址上发送电子邮件给我。但是当我在'to'部分输入所需的邮件收件人地址时。我没有收到任何电子邮件。可能是什么问题?

<?php
	if (isset($_POST["submit"])) {
		$name = $_POST['name'];
		$number = $_POST['number'];
		$email = $_POST['email'];
		$message = $_POST['message'];
		$from = ' Contact Form'; 
		$to = ''; 
		$subject = 'Message from Website contact page ';
		
		$body ="From: $name\n Number: $number\n E-Mail: $email\n Message:\n $message";
		// Check if name has been entered
		if (!$_POST['name']) {
			$errName = 'Please enter your name';
		}
		
		if (!$_POST['number']) {
			$errNumber = 'Please enter your mobile number';
		}
		
		// Check if email has been entered and is valid
		if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
			$errEmail = 'Please enter a valid email address';
		}
		
		//Check if message has been entered
		if (!$_POST['message']) {
			$errMessage = 'Please enter your message';
		}
		
// If there are no errors, send the email
if (!$errName && !$errNumber && !$errEmail && !$errMessage ) {
	if (mail ($to, $subject, $body, $from)) {
		$result='<div class="alert alert-success">Thank You! We will get in touch with you soon.</div>';
	} else {
		$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
	}
}
	}
?>

1 个答案:

答案 0 :(得分:0)

你可以检查一下:

$to = "desiredmailID@example.com, yourmailID@example.com";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= "From: noreply@example.com" . "\r\n";
  // Check if name has been entered
  $errName = '';
  if ($_POST['name'] == '') {
    $errName = 'Please enter your name';
  }
  //Other validation messages here...

  // If there are no errors, send the email
  if ($errName == '' && $errNumber == '' && $errEmail == '' && $errMessage == '' ) {
    if (mail ($to, $subject, $body, $headers)) {
      $result='<div class="alert alert-success">Thank You! We will get in touch with you soon.</div>';
    } else {
      $result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
    }
  }