Hello, I have problem with PHPMailer email sending, there are 5 input fields –
1. Name
2. Email
3. Phone
4. Website
5. Message
— Where I want Name, email & message as required other two – Phone & Website as not mandatory.
Here`s my code below, please someone help me –
This is “form-process.php” file —
[CODE]<?php
require(“class.phpmailer.php”);
$errorMSG = “”;
// NAME
if (empty($_POST[“name”])) {
$errorMSG = “Name is required “;
} else {
$name = $_POST[“name”];
}
// EMAIL
if (empty($_POST[“email”])) {
$errorMSG .= “Email is required “;
} else {
$email = $_POST[“email”];
}
// MESSAGE
if (empty($_POST[“message”])) {
$errorMSG .= “Message is required “;
} else {
$message = $_POST[“message”];
}
// prepare email body text
$MsgContent = “”;
$MsgContent .= “Name: “;
$MsgContent .= $name;
$MsgContent .= “<br> n”;
$MsgContent .= “Email: “;
$MsgContent .= $email;
$MsgContent .= “<br> n”;
$MsgContent .= “Message: “;
$MsgContent .= $message;
$MsgContent .= “<br> n”;
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = ‘ssl’;
$mail->Host = “smtp.gmail.com”;
$mail->Port = 465;
$mail->SetLanguage(“tr”, “phpmailer/language”);
$mail->CharSet =”utf-8″;
$mail->Username = “[email protected]”;
$mail->Password = “password***”;
$mail->SetFrom(“[email protected]”, “Email Server”);
$mail->AddAddress(“[email protected]”);
$mail->Subject = “Contact us email from $name”;
$mail->Body = $MsgContent ;
$mail->IsHTML(true);
$success = $mail->Send();
if ($success && $errorMSG == “”){
echo “success”;
}else{
if($errorMSG == “”){
echo “Something went wrong :(“;
} else {
echo $errorMSG . “<br> Mailer Error: ” . $mail->ErrorInfo;
}
}
?>
And this is the javascript file for ajax loading with validation –
[CODE]$(“#contactForm”).validator().on(“submit”, function (event) {
if (event.isDefaultPrevented()) {
// handle the invalid form…
formError();
submitMSG(false, “Did you fill in the form properly?”);
} else {
// everything looks good!
event.preventDefault();
submitForm();
}
});
function submitForm(){
// Initiate Variables With Form Content
var name = $(“#name”).val();
var email = $(“#email”).val();
var message = $(“#message”).val();
$.ajax({
type: “POST”,
url: “php/form-process.php”,
data: “name=” + name + “&email=” + email + “&message=” + message,
success : function(text){
if (text == “success”){
formSuccess();
} else {
formError();
submitMSG(false,text);
}
}
});
}
function formSuccess(){
$(“#contactForm”)[0].reset();
submitMSG(true, ” $name Message Submitted!”)
}
function formError(){
$(“#contactForm”).removeClass().addClass(‘shake animated’).one(‘webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend’, function(){
$(this).removeClass();
});
}
function submitMSG(valid, msg){
if(valid){
var msgClasses = “h3 text-center tada animated text-success”;
} else {
var msgClasses = “h3 text-center text-danger”;
}
$(“#msgSubmit”).removeClass().addClass(msgClasses).text(msg);
}
Please help me, someone.
Thanks