verify if mail valid and phone - php

I have a problem. It's that when in the contact form we want to contact and we write an invalid email or a phone number there is the bootstrap alert which appears alert-success when I haven't even received the email
contact.php
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';
if(isset($_POST["send"])){
$body = $_POST['message'];
$name = $_POST['name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '73.auto.73#gmail.com';
$mail->Password = 'rlylecrtuvztqosz';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; //Enable implicit TLS encryption
$mail->Port = 465;
$mail->setFrom('73.auto.73#gmail.com');
$mail->addAddress($_POST["email"]);
$mail->isHTML(true);
$mail->Subject = 'Projet web';
$mail->Body = "Message:" . $body . "<br>Phone number: " . $phone . "<br>Name: " . $name . "<br>Mail: " . $email;
$mail->send();
echo "success";
die;
}
index.php
<div class="col-md-4 mb-3 mb-md-0" data-aos="fade-left">
<form id="contactForm" action="contact.php" method="POST">
<input type="hidden" name="send">
<div class="alert alert-success" id="success-message" role="alert">
A simple success alert—check it out!
</div>
<div class="alert alert-danger" id="error-message" role="alert">
A simple success alert—check it out!
</div>
<div class="input-group mb-3">
<span class="input-group-text" id="basic-addon1"><i class="bi bi-person-fill"></i></span>
<input type="text" id="name" name="name" class="form-control" placeholder="Nom Prénom"
aria-label="Nom Prénom" aria-describedby="basic-addon2" required>
</div>
<!-- Email address input -->
<div class="input-group mb-3">
<span class="input-group-text" id="basic-addon1">#</span>
<input type="text" id="email" name="email" class="form-control" placeholder="Email"
aria-label="email" aria-describedby="basic-addon1" required>
</div>
<div class="input-group mb-3">
<span class="input-group-text" id="basic-addon1"><i class="bi bi-telephone-fill"></i></span>
<input type="text" id="phone" name="phone" class="form-control" placeholder="Téléphone"
aria-describedby="basic-addon2" required>
</div>
<!-- Message input -->
<div class="mb-3">
<textarea class="form-control" id="exampleFormControlTextarea1" rows="3"
placeholder="Decrivez le plus possible votre projet" name="message" required></textarea>
</div>
<!-- Form submit button -->
<div class="d-grid">
<button class="btn btn-primary btn-lg" name="send" type="submit" id="send-form">Submit</button>
</div>
</form>
</div>
<script>
AOS.init();
$(document).ready(function(){
$('#contactForm').submit(function(e){
e.preventDefault();
const $this = $(this);
// disable submit button
const $button = $(this).find('button[type="submit"]').text('Submit...').attr('disabled', 'disabled');
// send message
$.ajax({
type: 'POST',
url: 'contact.php',
data: $this.serialize(),
success: function(data){
$('#success-message').css('display', 'block');
$this[0].reset(); // reset form
},
error: function(jqXHR, textStatus, errorThrown){
$('#error-message').css('display', 'block');
},
complete: function(jqXHR, textStatus){
// enable submit button
$button.text('Submit').removeAttr('disabled');
}
})
})
})
</script>
Above you can see the code there is jquery ajax, php. I would like to know how I can do so that it verifies the email if it is a written email (that is to say in this format: mymail#mail.com
I receive the email when I am contacted and if the email is not valid (#mail.com) there is the bootstrap alert which appears write email not sent because the email is invalid I would like to do this for the email and the phone so that only numbers can be entered. how can i do that?

First of all always use type="email" for email,
Second thing is never validate at client site only,
You have to validate on both end i.e: HTML and PHP, On PHP end validate this as:
$email_val = $_POST['email'];
if(filter($email_val, FILTER_VALIDATE_EMAIL)){
// enter code here
}
And same you can validate for phone number by applying regex.

if ($mail->send()) {
echo "success";
} else {
echo "unnable to sent mail id error!!!";
}

Related

PHPMailer error: Undefined property: PHPMailer\PHPMailer\Exception::$getMessage

Need some help with PHPMailer code. I made a contact form with SMTP, and when i submit the vaules, i get this error:
Undefined property: PHPMailer\PHPMailer\Exception::$getMessage in C:\xampp\htdocs\contact\mail.php on line 32
What is the wrong?
Here is the php code:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require_once 'PHPMailer/src/Exception.php';
require_once 'PHPMailer/src/PHPMailer.php';
require_once 'PHPMailer/src/SMTP.php';
$mail = new PHPMailer (true);
$alert = '';
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
try{
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'myEmail#gmail.com';
$mail ->Password = 'appPassword';
$mail->SMTPSecure = "tls";
$mail->Port = '587';
$mail->setFrom( 'myEmail#gmail.com');
$mail->addAddress('myEmail#gmail.com');
$mail->isHTML (true);
$mail->Subject = 'Message Received from Contact: ' . $name;
$mail->Body = "Name: $name <br>Email: $email<br>Subject: $subject<br>Message: $message";
$mail->send();
$alert= "<div class='alert-success'><span>Message Sent! Thanks for contact us.</span></div>";
} catch (Exception $e) {
$alert = "<div class='alert-error'><span>' . $e->getMessage().'</span></div>";
}
}
?>
Here's the form code so you have it:
<form name="form1" method="post">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input type="text" class="form-control input-lg" name="name" id="name" placeholder="Enter name" required="required" />
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<input type="email" class="form-control input-lg" name="email" id="email" placeholder="Enter email" required="required" />
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<input type="text" class="form-control input-lg" name="subject" id="subject" placeholder="Subject" required="required" />
</div>
</div>
</div>
<div class="col-md-12"><?php echo $alert; ?></div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<textarea name="message" id="message" class="form-control" rows="4" cols="25" required="required" placeholder="Message" style="height: 170px;"></textarea>
</div>
<button type="submit" class="btn btn-skin btn-block" name="submit" id="submitcontact">Submit</button>
</div>
</div>
</form>
</div>
</div>
</div>
You have a semantics error. You started a string with a single quote and you closed it with a double quote. Be sure to start and end each string with the same type of quote.
change this
$alert = "<div class='alert-error'><span>' . $e->getMessage().'</span></div>";
to this
$alert = "<div class='alert-error'><span>" . $e->getMessage()."</span></div>";
You got your quotes in your alert message wrong. Change this line:
$alert = "<div class='alert-error'><span>' . $e->getMessage().'</span></div>";
to this:
$alert = '<div class="alert-error"><span>' . $e->getMessage(). '</span></div>';

PHPMailer used on contact form and hosted on Heroku. Post requests gives 200 but the mail is not sent

I am writing a contact form to collect data.
My app is on Heroku. It is a PHP application. SSL configured as well.
The PHPMailer is configured correctly as far as I am aware of. Following is the php code which does that. (contact.php)
<?php require 'PHPMailer-master/PHPMailerAutoload.php';
$fromEmail = 'myemail#gmail.com';
$fromName = 'Application Form';
$sendToEmail = 'myotheremmail#gmail.com';
$sendToName = 'Applied';
$subject = 'New message from contact form';
// smtp credentials and server
$smtpHost = 'smtp.gmail.com';
$smtpUsername = 'myemail#gmail.com';
$smtpPassword = 'passwordformyemail';
$fields = array('name' => 'Name', 'linkedin' => 'LinkedIn URL', 'phone' => 'Phone Number', 'email' => 'Email', 'message' => 'Cover Letter', 'notice' => 'Notice Period', 'salary' => 'Salary Expectation');
// message that will be displayed when everything is OK
$okMessage = 'Contact form successfully submitted. Thank you, I will get back to you soon!';
// If something goes wrong, we will display this message.
$errorMessage = 'There was an error while submitting the form. Please try again later';
error_reporting(E_ALL & ~E_NOTICE);
try {
if (count($_POST) == 0) {
throw new \Exception('Form is empty');
}
$emailTextHtml = "<h1>You have a new message from your contact form</h1><hr>";
$emailTextHtml .= "<table>";
foreach ($_POST as $key => $value) {
// If the field exists in the $fields array, include it in the email
if (isset($fields[$key])) {
$emailTextHtml .= "<tr><th>$fields[$key]</th><td>$value</td></tr>";
}
}
$emailTextHtml .= "</table><hr>";
$emailTextHtml .= "<p>Have a nice day,<br>Best,<br>Me</p>";
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = $smtpUsername;
//Password to use for SMTP authentication
$mail->Password = $smtpPassword;
$mail->setFrom($fromEmail, $fromName);
$mail->addAddress($sendToEmail, $sendToName);
$mail->addReplyTo($fromEmail, $fromName);
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $emailTextHtml;
$mail->msgHTML($emailTextHtml);
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
if (!$mail->send()) {
throw new \Exception('I could not send the email.' . $mail->ErrorInfo);
}
$responseArray = array('type' => 'success', 'message' => $okMessage);
} catch (\Exception $e) {
// $responseArray = array('type' => 'danger', 'message' => $errorMessage);
$responseArray = array('type' => 'danger', 'message' => $e->getMessage());
}
// if requested by AJAX request return JSON response
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
// else just display the message
else {
echo $responseArray['message'];
}
Here is the contact form code (index.php),
<form id="contact-form" method="post" action="contact.php" role="form">
<div class="messages"></div>
<div class="controls">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="form_name">Name *</label>
<input id="form_name" type="text" name="name" class="form-control" placeholder="Please enter your Name *" required="required" data-error="Name is required.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="form_linkedin">LinkedIn URL</label>
<input id="form_linkedin" type="text" name="linkedin" class="form-control" placeholder="Please enter your LinkedIn profile URL" data-error="LinkedIn URL is required.">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="form_email">Email *</label>
<input id="form_email" type="email" name="email" class="form-control" placeholder="Please enter your email *" required="required" data-error="Valid email is required.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="form_phone">Phone *</label>
<input id="form_phone" type="tel" name="phone" required="required" class="form-control" placeholder="Please enter your phone number" data-error="Valid Phone number is required.">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="form_email">Required Notice Period *</label>
<input id="form_email" type="text" name="notice" class="form-control" placeholder="Please enter the required period of notice to previous work place *" required="required" data-error="Notice Period detail is required.">
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="form_salary">Salary Expectation *</label>
<input id="form_salary" type="number" name="salary" class="form-control" placeholder="Please enter your expected salary in Sri Lankan Rupees *" required="required" data-error="Salary expectation is required.">
<div class="help-block with-errors"></div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group">
<label for="form_message">Cover Letter *</label>
<textarea id="form_message" name="message" class="form-control" placeholder="Please write Cover Letter *" rows="4" required="required" data-error="Cover Letter is required"></textarea>
<div class="help-block with-errors"></div>
</div>
</div>
<div class="col-md-12">
<input type="submit" class="btn btn-success btn-send" value="Submit Application">
</div>
</div>
<div class="row">
<div class="col-md-12">
<p class="text-muted">
<strong>*</strong> These fields are required.</p>
</div>
</div>
</div>
</form>
Then I have the JS code (index.js) This is added as an external script in index.php,
$(function () {
$('#contact-form').validator();
// when the form is submitted
$('#contact-form').on('submit', function (e) {
// if the validator does not prevent form submit
if (!e.isDefaultPrevented()) {
var url = "contact.php";
// POST values in the background the the script URL
$.ajax({
type: "POST",
url: url,
data: $(this).serialize(),
success: function (data)
{
var messageAlert = 'alert-' + data.type;
var messageText = data.message;
var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>' + messageText + '</div>';
// If we have messageAlert and messageText
if (messageAlert && messageText) {
// inject the alert to .messages div in our form
$('#contact-form').find('.messages').html(alertBox);
// empty the form
$('#contact-form')[0].reset();
}
}
});
return false;
}
})
});
The Heroku application log gives the following when the form is filled and submitted,
2021-07-18T10:47:27.738111+00:00 app[web.1]: 10.181.143.206 - - [18/Jul/2021:10:47:27 +0000] "POST /applications/contact.php HTTP/1.1" 200 2177 "https://www.example.com/applications/" "Mozilla/5.0 (Linux; Android 11; GM1910) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.210 Mobile Safari/537.36
As I am using gmail SMTP,
I have switched off two factor authentication
I have switched on less secure access
Nothing on the error logs as well.
I have added the following files for PHPMailer to work,
class.phpmailer.php
class.smtp.php
PHPMailerAutoload.php
I tried following the tutorials available on the web and checking the issue but had no luck.
Is there any silly mistake that I am doing here or have I missed an important point regarding PHPMailer and Heroku which makes this not to work?
Any help is greatly appreciated!

Form POST wont transfer variables to PHP script

I have this problem that I haven't been able to fix for days now and I am going crazy...
I have a HTML form and I am trying to pass those variables from the form over to my PHP script but for some reason they are not passing over.
HTML Form:
<div class="col-md-4 col-sm-12">
<a name="contactus"></a>
<div class="contact-form bottom">
<h2>Send a message</h2>
<form name="contact form" method="POST" action="sendemail_1.php" id="main-contact-form">
<div class="form-group">
<input type="text" name="name" id="names" class="form-control" required="required" placeholder="Name">
</div>
<div class="form-group">
<input type="email" name="email" id="emails" class="form-control" required="required" placeholder="Email">
</div>
<div class="form-group">
<input type="text" name="number" id="numbers" class="form-control" required="required" placeholder="Number">
</div>
<div class="form-group">
<textarea name="message" id="messages" required="required" class="form-control" rows="8" placeholder="Your text here"></textarea>
</div>
<div class="form-group">
<input type="submit" name="submit" class="btn btn-submit" value="Submit">
</div>
</form>
</div>
</div>
Full PHP SCRIPT:
<?php
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '*****#gmail.com';
$mail->Password = '****';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('*****#gmail.com', 'Test');
$mail->addReplyTo('****#gmail.com', 'Test');
$mail->addAddress("****#hotmail.com");
$mail->isHTML(false); // Set email format to HTML
$bodyContent = "test";
$name = filter_input(INPUT_POST, 'name');
$number = filter_input(INPUT_POST, 'number');
$email = filter_input(INPUT_POST, 'email');
$message = filter_input(INPUT_POST, 'message');
$mail->Subject = 'Test';
$mail->Body = $bodyContent;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
error_reporting(E_ALL);
?>
The JS used
// Contact form
var form = $('#main-contact-form');
form.submit(function(event){
event.preventDefault();
var form_status = $('<div class="form_status"></div>');
$.ajax({
url: $(this).attr('action'),
beforeSend: function(){
form.prepend( form_status.html('<p><i class="fa fa-spinner fa-spin"></i> Email is sending...</p>').fadeIn() );
}
}).done(function(data){
form_status.html('<p class="text-success">Thank you for contact us. As early as possible we will contact you</p>').delay(3000).fadeOut();
});
});
I have also tried it with the $_POST('name') but it also does not work.
Any help is really appreciated! Thanks
Commenting is starting to grow too large and this is too large for a comment.
To test, use what follows instead of what you have now.
Change from:
$name = filter_input(INPUT_POST, 'name');
$number = filter_input(INPUT_POST, 'number');
$email = filter_input(INPUT_POST, 'email');
$message = filter_input(INPUT_POST, 'message');
To:
if(
!empty($_POST['name'])
&&
!empty($_POST['number'])
&&
!empty($_POST['email'])
&&
!empty($_POST['message'])
) {
$name = $_POST['name'];
$number = $_POST['number'];
$email = $_POST['email'];
$message = $_POST['message'];
}
else{
echo "No empty fields please.";
exit; // This will STOP any further execution.
}
// the rest of your code below
I can't see this failing.
Note:
I see ID's everywhere and this suggests that you may be using additional Javascript to work with this.
Should this be the case, then you need to make sure that the syntax is correct.
This wasn't answered in my comment to you earlier.
okay found the issue - you need to pass submit button name as a submit not submited.
Try below code:
Your html file :
<form name="contact form" method="POST" action="sendemail_1.php" id="main-contact-form">
<div class="form-group">
<input type="text" name="name" id="names" class="form-control" required="required" placeholder="Name">
</div>
<div class="form-group">
<input type="email" name="email" id="emails" class="form-control" required="required" placeholder="Email">
</div>
<div class="form-group">
<input type="text" name="number" id="numbers" class="form-control" required="required" placeholder="Number">
</div>
<div class="form-group">
<textarea name="message" id="messages" required="required" class="form-control" rows="8" placeholder="Your text here"></textarea>
</div>
<div class="form-group">
<input type="submit" name="submit" class="btn btn-submit" value="Submit">
</div>
</form>
Your Php File
if(isset($_POST['submit']))
{
print_r($_POST);
}

Mailer with php not working [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
This is code of my form.
<form method="post" action="mailer.php" id="contactfrm">
<div class="col-sm-4">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" name="name" id="name" placeholder="Enter name" title="Please enter your name (at least 2 characters)">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" class="form-control" name="email" id="email" placeholder="Enter email" title="Please enter a valid email address">
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<label for="comments">Message</label>
<textarea name="message" class="form-control" id="comments" cols="3" rows="5" placeholder="Enter your message…" title="Please enter your message (at least 10 characters)"></textarea>
</div>
<button name="submit" type="submit" class="btn btn-lg btn-primary" id="submit">Submit</button>
<div class="result"></div>
</div>
</form>
Here My mailer.php
<?php
$replyemail="my email";
$name = $_POST["name"];
$email = $_POST["email"];
$thesubject = "Project With Me Query";
$themessage = $_POST["message"];
$success_sent_msg='<p align="center"><strong> </strong></p>
<p align="center"><strong>Your message has been successfully sent to My Email<br>
</strong> and I will reply as soon as possible.</p>
<p align="center">A copy of your query has been sent to you.</p>
<p align="center">Thank you for contacting Me.</p>';
$replymessage = "Hi $name
Thank you for your email.
We will endeavour to reply to you shortly.
Please DO NOT reply to this email.
Below is a copy of the message you submitted:
--------------------------------------------------
Subject: $thesubject
Query:
$themessage
--------------------------------------------------
Thank you";
$themessage = "name: $name \nQuery: $themessage";
mail("$replyemail",
"$thesubject",
"$themessage",
"From: $email\nReply-To: $email");
mail("$email",
"Receipt: $thesubject",
"$replymessage",
"From: $replyemail\nReply-To: $replyemail");
echo $success_sent_msg;
echo '<script>setTimeout(function(){location.href="index.php"} , 5000); </script>';
?>
I am unable to figure out what wrong I've done.
whenever i fill out information in for a Success Message displayed. but i didn't get any email of that information.
can someone fix this existing code or provide me a new mailer code?
Your form
<form method="post" action="1.php" id="contactfrm">
<div class="col-sm-4">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" name="name" id="name" placeholder="Enter name" title="Please enter your name (at least 2 characters)">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" class="form-control" name="email" id="email" placeholder="Enter email" title="Please enter a valid email address">
</div>
</div>
<div class="col-sm-4">
<div class="form-group">
<label for="comments">Message</label>
<textarea name="message" class="form-control" id="comments" cols="3" rows="5" placeholder="Enter your message…" title="Please enter your message (at least 10 characters)"></textarea>
</div>
<button name="submit" type="submit" class="btn btn-lg btn-primary" id="submit">Submit</button>
<div class="result"></div>
</div>
</form>
Your php code with smtp
<?php
if(isset($_POST["submit"])){
$replyemail="my email";
$name = $_POST["name"];
$email = $_POST["email"];
$thesubject = "Project With Me Query";
$themessage = $_POST["message"];
$success_sent_msg='<p align="center"><strong> </strong></p>
<p align="center"><strong>Your message has been successfully sent to My Email<br>
</strong> and I will reply as soon as possible.</p>
<p align="center">A copy of your query has been sent to you.</p>
<p align="center">Thank you for contacting Me.</p>';
$replymessage = "Hi $name
Thank you for your email.
We will endeavour to reply to you shortly.
Please DO NOT reply to this email.
Below is a copy of the message you submitted:
--------------------------------------------------
Subject: $thesubject
Query:
$themessage
--------------------------------------------------
Thank you";
$themessage = "name: $name \nQuery: $themessage";
include "PHPMailer_5.2.4/class.phpmailer.php";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
$mail->IsHTML(true);
$mail->Username = "yourusername#gmail.com";
$mail->Password = "yourgmailpassword";
$mail->AddReplyTo($replymessage, "Reply name");
$mail->AddAddress($email,'ashu');
$mail->Subject = "SMTP Receivced";
$mail->Body = "<b>Succesfully SMTP Receivced</b>";
$mail->MsgHTML($success_sent_msg);
$text = 'Text version of email';
$html = '<html><body>HTML version of email</body></html>';
$file = 'index.php';
$crlf = "\n";
$hdrs = array(
'From' => 'you#yourdomain.com',
'Subject' => 'Test mime message'
);
if($mail->send($hdrs))
{
echo "<script> alert('Successfully Mailed');window.location = '';</script>";
}
else{
echo "Mailed Error: " . $mail->ErrorInfo;
}
}
//echo '<script>setTimeout(function(){location.href="pra-2.php"} , 5000); </script>';
?>

Issue with PHPMailer, email won't send

I am trying to get my contact form to work. I have uploaded the website on the live server which uses PHP 5.4. I fill out the contact form and it says "Your Message has been sent" but I don't receive any emails for some reason.
This is HTML Form.
<form role="form" id="contactForm">
<div class="form-group control-group">
<div class="controls">
<p class="help-block"></p>
<input type="text" class="form-control"
placeholder="First Name" id="firstname"
data-validation-required-message="Please enter your first name."
aria-invalid="false">
</div>
</div>
<div class="form-group control-group">
<div class="controls">
<p class="help-block"></p>
<input type="text" class="form-control"
placeholder="Last Name" id="lastname"
data-validation-required-message="Please enter your last name."
aria-invalid="false">
</div>
</div>
<div class="form-group control-group">
<div class="controls">
<p class="help-block"></p>
<input type="email" class="form-control"
placeholder="Email" id="email"
data-validation-required-message="Please enter your email address."
aria-invalid="false">
</div>
</div>
<div class="form-group control-group">
<div class="controls">
<p class="help-block"></p>
<textarea class="form-control" placeholder="Message"
id="message" data-validation-required-message="Please enter some message."
aria-invalid="false"></textarea>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Send message</button>
</div>
</form>
This is where it goes wrong potentially. I have the url: "send.php" file in my directory...
<script>
$(function () {
$("input,textarea").jqBootstrapValidation({
preventSubmit: true,
submitSuccess: function($form, event) {
event.preventDefault(); // prevent default submit behaviour
// get values from FORM
var firstname = $("input#firstname").val();
var lastname = $("input#lastname").val();
var email = $("input#email").val();
var message = $("textarea#message").val();
$.ajax({
url: "send.php",
type: "POST",
data: {
firstname: firstname,
lastname: lastname,
email: email,
message: message
},
cache: false,
success: function() {
// Success message
$('.contact-alert').html("<div class='alert alert-success'>");
$('.contact-alert > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'><i class='fa fa-times'></i></button><strong>Your message has been sent.</strong></div>");
//clear all fields
$('#contactForm').trigger("reset");
},
error: function() {
// Fail message
$('.contact-alert').html("<div class='alert alert-danger'>");
$('.contact-alert > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'><i class='fa fa-times'></i></button><strong>Sorry, it seems that my mail server is not responding. Please try again later!</strong></div>");
//clear all fields
$('#contactForm').trigger("reset");
},
})
}
});
});
</script>
The send.php file.. I also have downloaded class.phpmailer.php and it's in the same location as the send.php
if(isset($_POST['firstname']) && isset($_POST['lastname']) && isset($_POST['email']) && isset($_POST['message'])) {
require("class.phpmailer.php"); //you have to download this plugin from github.com - link is in documentation
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$message = $_POST['message'];
$mail = new PHPMailer();
$mail->From = $email;
$mail->CharSet = "UTF-8";
$mail->FromName = $email;
$mail->addAddress("adomas2006#gmail.com");
$mail->IsHTML(true);
$mail->Subject = $name." sent you an email";
$mail->Body = $message."<br><br>".$name."<br>".$email."<br>".$phone."<br>";
$mail->send();

Categories