联系表格PHP-从特定电子邮件发送

时间:2018-07-19 20:39:23

标签: php forms email contact

我在我的网站上有一个联系表,工作正常。问题发生在联系表单发送到我的电子邮件时,它进入了我的垃圾邮件文件夹,因为发送表单的电子邮件是一个随机的电子邮件地址。

是否可以使用受信任的电子邮件地址将表单发送到我的电子邮件?

 <?php
    // Define some constants
    define( "RECIPIENT_NAME", "Greg Collins" );
    define( "RECIPIENT_EMAIL", "maxacollins@gmail.com" );


// Read the form values
$success = false;
$sendername = isset( $_POST['username'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['username'] ) : "";
$senderEmail = isset( $_POST['email'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['email'] ) : "";
$senderSubject = isset( $_POST['subject'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['subject'] ) : "";
$message = isset( $_POST['message'] ) ? preg_replace( "/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_POST['message'] ) : "";

// If all values exist, send the email
if ( $sendername && $senderEmail && $senderSubject && $message) {
  $recipient = RECIPIENT_NAME . " <" . RECIPIENT_EMAIL . ">";
  $headers = " G.C. Environmental.com - Contact Form Message ";
  $msgBody = " 
  Name: " . $sendername . "
  From: " . $senderEmail . " <" . $senderSubject . ">
  Message: " . $message . "  
  ";
  $success = mail( $recipient, $headers, $msgBody );

  //Set Location After Successsfull Submission
  header('Location: contact.html?message=Successfull');
}

else{
    //Set Location After Unsuccesssfull Submission
    header('Location: index.html?message=Failed');  
}

?>

0 个答案:

没有答案