I've been trying this for a long but failed till now.
I've tried changing form names, or attributes names but didn't work.
Here is the code for my form:
<form action="contact_process.php" class="office_contact_form" id="contactForm" method="post" name="contactForm" novalidate="">
<div class="form-group col-md-12">
<input class="form-control" id="name" name="name" placeholder="Name" type="text">
</div>
<div class="form-group col-md-12">
<input class="form-control" id="email" name="email" placeholder="Email Address *" type="text">
</div>
<div class="form-group col-md-12">
<input class="form-control" id="subject" name="subject" placeholder="Subject" type="text">
</div>
<div class="form-group col-md-12">
<textarea class="form-control" id="message" name="message" placeholder="Your Message" rows="1"></textarea>
</div>
<div class="form-group col-md-12">
<button class="btn p_btn" type="submit" value="submit">Send Message</button>
</div>
</form>
Here is my PHP code:
<?php
$to = "hello1224@gmail.com";
$from = $_REQUEST['yourname'];
$name = $_REQUEST['youremail'];
$headers = "From: $from";
$subject = "You have a message from your attornyeproducts.com";
$fields = array();
$fields{"yourname"} = "name";
$fields{"youremail"} = "email";
$fields{"subject"} = "subject";
$fields{"phone"} = "phone";
$fields{"message"} = "message";
$body = "Here is what was sent:
"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s
",$b,$_REQUEST[$a]); }
$send = mail($to, $subject, $body, $headers);
?>
I'm trying to receive the data from the this contact form to the email's id.
Did you tried https://github.com/PHPMailer/PHPMailer ?
simple example: https://github.com/PHPMailer/PHPMailer
example to fit your case:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
// Load Composer's autoloader
require 'vendor/autoload.php';
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user@example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom("YOUREMAIL@gmail.com");
$mail->addAddress("hello1224@gmail.com"); // Add a recipient
$fields = array(
"yourname" => $_REQUEST['yourname'],
"youremail" => $_REQUEST['youremail'],
"subject" => $subject ,
"phone" => "phone",
"message" => "message",
);
$body = "Here is what was sent:
"; foreach($fields as $a => $b){
$body .= sprintf("%20s: %s
",$b,$_REQUEST[$a]); }
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject
$mail->Body = $body;
$mail->AltBody = $body;
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
I did not test your fields. You can test your field to ensure they have correct values and then pass those values to this function.
I suggest you use FILTER_VALIDATE_EMAIL to ensure you have a valid email.
Also you can check that the post keys that you are expecting actually exist: $message=(isset($_POST['message']))?$_POST['message']:'default message';
function send($subject,$msg,$email,$from,$replyto=null){
$replyto=(isset($replyto) && filter_var($replyto, FILTER_VALIDATE_EMAIL) )?$replyto:'contact@mydomain.com';
$params="-fcontact@mydomain.com";
$subject = $subject;
$message = "<div style='font-family: Arial, Helvetica, sans-serif;'>";
$message .= $msg;
$message .="</div>";
$headers = "From: =?utf-8?b?".base64_encode($from)."?= <contact@mydomain.com>
";
$headers .= "Content-type: text/html; charset=UTF-8
";
$headers .= 'Bcc: info@mydomain.com' . "
";
$headers .= 'Reply-To: '.$replyto . "
";
$headers .= 'X-Mailer: PHP/' . phpversion();
$to = $email;
if(isset($_SERVER['REMOTE_ADDR']) && in_array( $_SERVER['REMOTE_ADDR'], array( '127.0.0.1', '::1' ))) return true;
return mail($email, $subject, $message, $headers,$params);
}