js contact form email code example
Example 1: php contact form
#Contact form
NOTE: NEEDS An SMTP service on the website server.
<?php
$msg = '';
$msgClass = '';
if(filter_has_var(INPUT_POST,'submit')){
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$message = htmlspecialchars($_POST['message']);
if(!empty($email) && !empty($name) && !empty($message)){
if(filter_var($email, FILTER_VALIDATE_EMAIL) === false){
$msg = 'email format is incorrect';
$msgClass='alert-danger';
}else{
$toEmail = '[email protected]';
}
}else{
$msg = 'Please Fill in all fields completely';
$msgClass='alert-danger';
$subject = 'contact request from '.$name;
$body = "<h2>Contact Request</h2>
<h4>Name</h4><p>'.$name.'</p>
<h4>Email</h4><p>'.$email.'</p>
<h4>Message</h4><p>'.$message.'</p>";
$headers = "MIME-VERSION: 1.0" . "\r\n";
$headers .= "Content-Type:text/html;charset=UTF-8" . "/r/n";
$headers.= "From: ".$name."<" .$email. ">". "\r\n";
if(mail($toEmail, $subject, $body, $headers)){
$msg = 'Email sent';
$msgClass = 'alert-success';
}else{
$msg = 'Email has not been sent';
$msgClass = 'alert-danger';
}
}
?>
Example 2: how to connect contact form to email in php
<?php
if(!empty($_POST["send"])) {
$name = $_POST["userName"];
$email = $_POST["userEmail"];
$subject = $_POST["subject"];
$content = $_POST["content"];
$toEmail = "admin@phppot_samples.com";
$mailHeaders = "From: " . $name . "<". $email .">\r\n";
if(mail($toEmail, $subject, $content, $mailHeaders)) {
$message = "Your contact information is received successfully.";
$type = "success";
}
}
require_once "contact-view.php";
?>
Example 3: how to connect contact form to email in php
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
}