0

I would like to write Simple Contact Form. I use code as below:

    <?php
if(isset($_POST['email'])) {

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "memail58895@wp.pl";
    $email_subject = "Your email subject line";

    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }


    // validation expected data exists
    if(!isset($_POST['firstname']) ||
        !isset($_POST['email']) ||
        !isset($_POST['lsubject'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }



    $first_name = $_POST['firstname']; // required
    $email_from = $_POST['email']; // required
    $subject = $_POST['lsubject']; // required
    $messege = $_POST['mess'];

    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }

    $string_exp = "/^[A-Za-z .'-]+$/";

  if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }

  if(strlen($error_message) > 0) {
    died($error_message);
  }

    $email_message = "Form details below.\n\n";


    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }



    $email_message .= "First Name: ".clean_string($first_name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";

// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>

<!-- include your own success html here -->

Thank you for contacting us. We will be in touch with you very soon.

<?php

}
?>

I see "Thanks you for contacting us....." but I don't get email in mailbox. What is the problem? Should I also include information about server or roles, password? Does the server send a message from PHP?

Please, tell what I should change in this code.

EDIT:

I use PHPMailer, code like as :

<?php

// $email and $message are the data that is being
// posted to this page from our html contact form
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;

// When we unzipped PHPMailer, it unzipped to
// public_html/PHPMailer_5.2.0
require('C:\xampp\php\lib\PHPMailer\PHPMailerAutoload.php');

$mail = new PHPMailer();

// set mailer to use SMTP
$mail->IsSMTP();

// As this email.php script lives on the same server as our email server
// we are setting the HOST to localhost
$mail->Host = "smtp.wp.pl";  // specify main and backup server

$mail->SMTPAuth = true;     // turn on SMTP authentication

// When sending email using PHPMailer, you need to send from a valid email address
// In this case, we setup a test email account with the following credentials:
// email: send_from_PHPMailer@bradm.inmotiontesting.com
// pass: password
$mail->Username = "test@wp.pl";  // SMTP username
$mail->Password = "test"; // SMTP password

// $email is the user's email address the specified
// on our contact us page. We set this variable at
// the top of this page with:
// $email = $_REQUEST['email'] ;
$mail->From = $email;

// below we want to set the email address we will be sending our email to.
$mail->AddAddress("test@wp.pl", "Test na WP");

// set word wrap to 50 characters
$mail->WordWrap = 50;
// set email format to HTML
$mail->IsHTML(true);

$mail->Subject = "You have received feedback from your website!";

// $message is the user's message they typed in
// on our contact us page. We set this variable at
// the top of this page with:
// $message = $_REQUEST['message'] ;
$mail->Body    = $message;
$mail->AltBody = $message;

if(!$mail->Send())
{
   echo "Message could not be sent.
";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}

echo "Message has been sent";
?>

But I get error:

Fatal error: Uncaught Error: Class 'PHPMailer' not found in C:\xampp\htdocs\contact.php:12 Stack trace: #0 {main} thrown in C:\xampp\htdocs\contact.php on line 12
Bartosz20188
  • 27
  • 2
  • 8
  • well, you probably aren't sending anything because `isset($_POST['email'])` is returning `false`. You need to update your question with the form as well. – Cemal Aug 14 '18 at 12:49
  • @Cemal Could you explain me? I update the message, I have an HTML file – Bartosz20188 Aug 14 '18 at 12:56
  • please ignore my previous comment, I was unable to see where the `if` clause ended. As proposed by @JayBlanchard you may find some answers in the tagged question. As a personal side note, şnstead of dealing with mail function which generally ends up with more hassle than its worth, you can find php classes that is way easier to configure, and agile. such as PhpMailer, etc. – Cemal Aug 14 '18 at 13:00
  • @Cemal I add PHPMailer code in post. – Bartosz20188 Aug 15 '18 at 13:09
  • Please ask another question. The title is not related to what you are asking hence not helpful to those who may come to this for solution – Cemal Aug 15 '18 at 13:10
  • @Cemal https://stackoverflow.com/questions/51859468/php-mailer-contact-form-issue – Bartosz20188 Aug 15 '18 at 13:15

0 Answers0