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);
}
Related
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>';
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!!!";
}
Hi I'm new to php and swift mailer and I face this problem when I want to send the data from a bootstrap form. When I use a bootstrap form I get an empty mail, but when I use a simple html form I get a .txt file with the information from the form in the mail. Here's my index.php code:
<form id="main-contact-form" name="contact-form" method="post" action="contact_email.php" role="form">
<div class="row wow fadeInUp" data-wow-duration="1000ms" data-wow-delay="300ms">
<div class="col-sm-6">
<div class="form-group">
<input type="text" name="name" class="form-control" placeholder="Name" required="required">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<input type="email" name="email" class="form-control" placeholder="Email Address" required="required">
</div>
</div>
</div>
<div class="form-group">
<input type="text" name="subject" class="form-control" placeholder="Subject" required="required">
</div>
<div class="form-group">
<textarea name="message" id="message" class="form-control" rows="4" placeholder="Enter your message" required="required"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn-submit">Send</button>
</div>
</form>
and here's my contact_email.php code
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
set_error_handler("var_dump");
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
$subject = filter_var($_POST['subject'], FILTER_SANITIZE_STRING);
$mesage = filter_var($_POST['messsage'], FILTER_SANITIZE_STRING);
$data = 'Name: '.$name.'<br />
Subject: '.$subject.'<br />
Email: '.$email.'<br />
Message: '.$mesage;
require_once 'swift/lib/swift_required.php';
$transport = Swift_SmtpTransport::newInstance('ssl://smtp.gmail.com', 465)
->setUsername('myemail#gmail.com')
->setPassword('********');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('Email From My Website')
->setFrom(array('myemail#gmail.com' => 'website'))
->setTo(array('myemail#gmail.com'))
->setBody($data, text/html);
if ($mailer->send($message)) {
echo 'Mail sent successfully.';
} else {
echo 'I am sure, your configuration are not correct. :(';
}
?>
</body>
</html>
please tell me why I'm getting an empty mail and how to fix it and why I'm getting the information in a .txt file. thank you in advance :)
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>';
?>
I've been looking around everywhere and cannot seem to find how to make this work - it is simply not sending an email to my address. Here is my HTML form:
<form id="contactMe" name="contact" method="post" novalidate="novalidate">
<fieldset>
<label for="name" id="name">Name<span class="required">*</span></label>
<input type="text" name="name" id="name" size="30" value="" required="">
<label for="email" id="email">Email<span class="required">*</span></label>
<input type="text" name="email" id="email" size="30" value="" required="">
<label for="phone" id="phone">Phone</label>
<input type="text" name="phone" id="phone" size="30" value="">
<label for="Message" id="message">Message<span class="required">*</span></label>
<textarea name="message" id="message" required=""></textarea>
<label for="Answer" id="answer">Name the house pet that says “<i>woof</i>“<span class="required">*</span></label>
<input type="text" name="answer" value="" required=""></br>
<input id="submit" type="submit" name="submit" value="Send">
</fieldset>
<div id="success">
<span class="green textcenter">
<p>Your message was sent successfully! I will be in touch as soon as I can.</p>
</span>
</div> <!-- closes success div -->
<div id="error">
<span><p>Something went wrong, try refreshing and submitting the form again.</p></span>
</div> <!--close error div-->
</form>
and here is my PHP saved as mailer.php:
<?php
$to = "someone#gmail.com";
$from = $_REQUEST['email'];
$name = $_REQUEST['name'];
$headers = "From: $from";
$subject = "You have a message from your.";
$fields = array();
$fields{"name"} = "name";
$fields{"email"} = "email";
$fields{"phone"} = "phone";
$fields{"message"} = "message";
$body = "Here is what was sent:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }
mail("maytee.kneitz#gmail.com",$subject,$message,"From: $from\n");
echo 'Mail sent';
?>
This is my first shot at working on a mailer / contact form, so sorry if it's a blatant problem. I just can't seem to find it. Any guidance would be appreciated.
I do have validation in my scripts (not posted here).
You don't have a form action defined. Try this:
<form id="contactMe" name="contact" method="post" action="mailer.php" novalidate="novalidate">
By default, the form will be submitted to its current location unless otherwise specified. In your case, point it to wherever your mail script is located