0

I got to solve this problem..So I have a forms on my website(html/css/js) and I would like to receive an E-mail once a customer fill it up instead of the registation in a DB .I m a beginner in php and i hope u'll help me ..below is my PHP code.

<?php

// Define some constants
define( "RECIPIENT_NAME", "my name " );
define( "RECIPIENT_EMAIL", "******@***.com" );

// Read the form values
$success = false;
$userName = isset( $_POST['username'] ) ? preg_replace( "/[^\s\S\.\-\_\@a-zA-Z0-9]/", "", $_POST['username'] ) : "";
$lastName = isset( $_POST['lastname'] ) ? preg_replace( "/[^\s\S\.\-\_\@a-zA-Z0-9]/", "", $_POST['lastname'] ) : "";
$senderEmail = isset( $_POST['email'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['email'] ) : "";
$userPhone = isset( $_POST['phone'] ) ? preg_replace( "/[^\s\S\.\-\_\@a-zA-Z0-9]/", "", $_POST['phone'] ) : "";
$message = isset( $_POST['message'] ) ? preg_replace( "/(From:|To:|BCC:|CC:|Phone:|Content-Type:)/", "", $_POST['message'] ) : "";

// If all values exist, send the email
if ( $userName && $lastName && $senderEmail && $userPhone && $message) {
  $recipient = RECIPIENT_NAME . " <" . RECIPIENT_EMAIL . ">";
  $headers = "From: " . $userName . "";
  $msgBody = " Last Name: ". $lastName .  " Email: ". $senderEmail . " Phone: ". $userPhone . " 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: contact.html?message=Failed');    
}

?>

1 Answers1

-1

I recommend that you use the library PHPMailer for the sending of the E-Mails. An E-Mail needs to be configured and PHPMailer does that automatically.

Dharman
  • 30,962
  • 25
  • 85
  • 135
JVT038
  • 11
  • 1
  • 5