1

I have little issue about my contact form. it doesn't send mail the server admin.

<?php
session_start();
// get the data from the form
$admin_email = 'server@admin.com';
$email = isset($_POST['email']) ? $_POST['email'] : '';
$subject = 'Contact Form';
$name = isset($_POST['name']) ? $_POST['name'] : '';
$message = isset($_POST['message']) ? $_POST['message'] : '';
$captcha = isset($_POST['captcha']) ? $_POST['captcha'] : '';
$img_session = isset($_SESSION['img_session']) ? $_SESSION['img_session'] : '';
$website = $_SERVER['www.mywebsite.com'];

// check if the fields are empty
if(empty($email) or empty($name) or empty($email) or empty($message)){
    $output = "All fields are required!";
}else{
    if(md5($captcha) == $img_session){
$header = "From: $email"."\r\n"
.'Content-Type: text/plain; charset=utf-8'."\r\n";

$message = "
New entry from $subject!

Name: $name
E-Mail: $email
Message:
$message

This message was sent from http://$website";

        if(mail($admin_email, '=?utf-8?B?'.base64_encode($subject).'?=', $message, $header)){
            $output = "Your message was sent!<br />Thank you!";
        }
    }else{
        $output = "Wrong captcha code!";
    }
}
echo $output;
?>

UPDATED

the wrong part is if $admin_email is not match the contact form email address wrong captcha error pops up? I don't know why. I mean if I don't write contact form email line, same address with admin_email I can't send mail??? What I am trying to do here: send mail to the guest (your mail sent), and also $admin_email: server@admin.com (You have email) $messege.

Form

<form action="" id="contact_form" method="POST">
  <p>Name:</p>
  <input type="text" name="name" placeholder="Enter name" required=""/>
  <p>Email:</p>
  <input type="email" name="email" placeholder="Enter email" required=""/>
  <p>Message:</p>
  <textarea name="message" rows="10" cols="30" required=""></textarea>
  <p>Captcha:</p>
  <img src="captcha.php" id="captcha"/>
  <input type="text" name="captcha" placeholder="Enter code" required=""/>
  <input type="submit" value="Submit" />
</form>

1 Answers1

0

If you want to send both email to user and admin, try to call the mail function twice, for example


 if(mail($user_email_address, '=?utf-8?B?'.base64_encode($subject).'?=', $message, $header)){
            $output = "Your message was sent!<br />Thank you!";
        }

 if(mail($admin_email_address, '=?utf-8?B?'.base64_encode($subject).'?=', $message, $header)){
            $output = "You've got a new message from @user_email_address <br /> ";
        }
Duc Vu
  • 95
  • 1
  • 1
  • 8
  • I updated the question. the problem part is the wrong part is if $admin_email is not match the contact form email address wrong captcha error pops up? I don't know why. I mean if I don't write contact form email line, same address with admin_email I can't send mail??? –  Apr 01 '19 at 03:11
  • Can you update the detail inputs and the error? If you got the wrong captcha error that means the condition if(md5($captcha) == $img_session) return false. – Duc Vu Apr 01 '19 at 03:25
  • update the form as well. –  Apr 01 '19 at 03:28