$_POST is returning empty array - php

I've gone through a lot of forums today to figure out exactly why this is happening. I've set up PHPMailer and everything works fine. However, at the top of the code you can see that I attempt to define variables which are initialized with $_POST, however for example if I run var_dump($_POST['name']); it will return NULL for the value. The form data simply is not being sent over. It might be a simple mistake but insight would be appreciated.
PHP Code:
<?php
require 'PHPMailerAutoload.php';
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
print_r($_POST['name']);
// if(isset($_POST['name'])) {
// $name = htmlspecialchars($_POST['name']);
// }
// if(isset($_POST['email'])) {
// $email = htmlspecialchars($_POST['email']);
// }
// if(isset($_POST['message'])) {
// $message = htmlspecialchars($_POST['message']);
// }
$mail = new PHPMailer();
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'xxxxxxxxxxxx'; // SMTP username
$mail->Password = 'xxxxxxxxxx'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->From = $email;
$mail->FromName = $name;
$mail->addAddress('xxxxxxxx'); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = "Contact Request Form From $name";
$mail->Body = "
<table width='450px'>
<tr>
<td><strong>Name: </strong></td>
<td>$name</td>
</tr>
<tr>
<td><strong>Email: </strong></td>
<td>$email</td>
</tr>
<tr>
<td><strong>Message: </strong></td>
<td>$message</td>
</tr>
</table>
";
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
Here is my HTML form code:
<div class="contact-form">
<h3>Contact Me</h3>
<p>Please use the form below to get in contact with me</p>
<form action="process.php" method="post">
<div class="form-group">
<label for="name-1">Name</label>
<input type="text" name="name" id="name-1" placeholder="Enter Name" class="style">
</div>
<div class="form-group">
<label for="email-1">Email</label>
<input type="email" name="email" id="email-1" placeholder="Enter Email" class="style">
</div>
<div class="form-group">
<label for="message-1">Message</label>
<textarea name="message" id="message-1" placeholder="Enter Message"></textarea>
</div>
<div>
<input type="submit" value="Submit" class="btn confirm">
</div>
</form>
</div>
JavaScript Code:
// If submit button is clicked on contact form
document.querySelector('.confirm').onclick = function() {
window.location.href = 'confirmed.html';
}

Related

Php form instead send mail download on computer

<div class="contact_form clearfix" id="Contact">
<h2>Hello... You can send me message to my universe here.</h2>
<img src="img/planeta1.png" alt="">
<form class="clearfix spaceForm" action="contactform.php" metod="post" >
<label for="name">Your name:</label>
<input type="text" name="name" id="name" placeholder="Jon Doe" required>
<label for="email">Your email:</label>
<input type="text" name="email" id="email" placeholder="something#mama.com" required>
<label for="subject">Subject</label>
<input type="text" name="subject" id="subject" required>
<label for="message">Your message:</label>
<textarea name="message" id="message" required></textarea>
<button type="submit" name="submit">Send mail</button>
</div>
and php code here...
<?php
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$mailFrom = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$mailTo = "pisitenam#sammostalnisindikatstark.org.rs";
$headers = "From: ".$mailFrom;
$txt = "You have received an e-mail from " .$name.".\n\n".$message;
mail($mailTo, $subject, $txt, $headers);
header("Location: index.html");
}
?>
My contact form instead to send message download on computer as php file. I uploaded my site to netfly but stil doesnt work.
Can anybody help and give me a hint where is problem?
On XAMPP im getting blank page and mail is not sent. When I uploaded site on netfly site works fine but contact from when click submit start download php file where is code writen for controling contact form.5 day im trying to find solution for this problem and im geting tired :D So if anybody can help...
you are having some spell mistake in your form tag, first of all correct the method spell in your code as it is not correct so it can't redirect and post your data to contact form.
mail library contains various function.
for example:
<?php require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'your_smtp_domain.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
$mail->From = 'from#example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('john#example.net', 'John doe'); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
this can works for you if you use library.

SMTP Error: Could not authenticate. while sending email in php

Hi i am using PHPMailer for my website to send an email but getting error as SMTP Error: Could not authenticate.
This was error which i was getting.I have given the password correct and turned Allow less secure apps to "ON as well but still getting the same error.
020-01-25 09:11:24 SMTP ERROR: Password command failed: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbv534-5.7.14 oCoMr_x13f0BsQ-UMqe0zgPQOcVX9A7SY4fBk-WoLrRIJfT6uwspq7QpzlUjE8srm9esG534-5.7.14 73T38rlSCuGdkDRsjhkB3YQxKz3V8njuaclMAfqHskMAU3eX_1LYPH80oO9ZLkAl>534-5.7.14 Please log in via your web browser and then try again.534-5.7.14 Learn more at534 5.7.14 https://support.google.com/mail/answer/78754 i81sm528818ywe.82 - gsmtp
SMTP Error: Could not authenticate.
2020-01-25 09:11:24 CLIENT -> SERVER: QUIT
Here is the code which i have used for sending an email
<?php ob_start();
if(isset($_POST['submit_contact']))
{
require 'PHPMailer/PHPMailerAutoload.php';
$address = 'testing1#gmail.com';
$name=$_POST['name'];
$email =$_POST['email'];
$subject = $_POST['subject'];
$textMessage = $_POST['message'];
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug =1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->CharSet = "UTF-8";
$mail->IsHTML(true);
$mail->Username = "testing#gmail.com";
$mail->Password = "PASSword1#3";
$message = array();
$message[] = 'Name : '.trim($name).' ';
$message[] = 'Email : '.trim($email).' ';
$message[] = 'Comment : '.trim($textMessage).' ';
$message = implode(',', $message);
$mail->SetFrom($email);
$mail->Subject = $subject;
$mail->Body = $message;
$mail->AddAddress($address);
if (!$mail->Send()) {
$msg = "Error while sending email";
$msgclass = 'bg-danger';
} else {
$msg = 'Thank you for contacting us.Will get back to you soon.....';
$msgclass = 'bg-success';
header("Location: contact.php");
}
}
?>
Here is the HTML Code which i have written
<form class="form-horizontal" action="contactus" method="post" role="form" enctype="multipart/form-data">
<?php if(isset( $msg)) {?>
<div class="<?php echo $msgclass; ?>" style="padding:5px;"><?php echo $msg; ?></div>
<?php } ?>
<div class="form-group">
<input type="text" name="name" id="name" class="form-control input-field" placeholder="Name" required="">
<input type="email" name="email" id="email" class="form-control input-field" placeholder="Email ID" required="">
<input type="text" name="subject" id="subject" class="form-control input-field" placeholder="Subject" required="">
</div>
<textarea name="message" id="message" class="textarea-field form-control" rows="4" placeholder="Enter your message" required=""></textarea>
<button type="submit" id="btnSend" value="Submit" class="btn center-block" name="submit_contact" >Send message</button>
<!-- Contact results -->
</form>
Always look at the information that’s right in front of you.
Read the troubleshooting guide linked from the error message - it tells you exactly how to deal with this precise problem — you most likely need to perform the “display captcha unlock” step.
It’s not just the “less secure apps” setting (which you can avoid by implementing XOAUTH2), it’s also that gmail imposes additional auth steps when logging in through a new mechanism (I.e. your script), which is briefly mentioned in the link they provide in the responses.

PHP script with form not sending the email

I am having some problems with php. Well, the code, when uploaded to the server and run in browser is working and the email is arriving, using this code below:
<?php
date_default_timezone_set('Etc/UTC');
require 'PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->SMTPDebug = 3;
$mail->IsSMTP();
//$mail->IsSMTP();
$mail->Debugoutput = 'html';
$mail->Host = 'relay-hosting.secureserver.net';
$mail->Port = 25;
$mail->SMTPSecure = false;
$mail->SMTPAuth = false;
$mail->Username = "exemple#gmail.com";
$mail->Password = "password";
$name = "catarina";
$email = "myEmail#hotmail.com";
$message = "test";
$mail->setFrom($email, $name);
$mail->addAddress('me#exemple.io');
$mail->Subject = 'Contact Site';
$mail->Body = $message;
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
The problem is, when I change this code a little to send with a form, the email never arrives:
js:
function submitForm() {
$.ajax({type:'POST', url:'email-action.php', data:$('#contact-form').serialize(), success: function(result){
$('.submit').html('send');
$('.send').removeClass('no-show');
$('.send').removeClass('no-show-mobile');
}});
};
html:
<form action="/" onsubmit="return submitForm();" method="post" name="contactform" class="contact-form wow zoomIn" data-wow-delay="0.6s" id="contact-form">
<div class="col-md-6">
<div class="row">
<div class="col-md-12">
<input placeholder="Name" class="input-field" name="name" required="" type="text">
</div>
<div class="col-md-12">
<input placeholder="E-mail" class="input-field" name="email" required="" type="email">
</div>
</div>
</div>
<div class="col-md-6">
<textarea placeholder="Message" class="input-field" name="message"></textarea>
<input value="Send Message" class="input-send submit" type="submit" name="submit">
</div>
<div class="col-md-12 send no-show hidden-xs"><button class=" botao btn btn-sucess"><h4 class="">Message sent sucessfully! We will get in touch shortly.</h4></button></div>
<div class="col-md-12 send no-show-mobile visible-xs"><button class=" botao btn btn-sucess"><h4 class="">Message sent sucessfully!</h4></button></div>
</form>
php:
date_default_timezone_set('Etc/UTC');
require 'PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->SMTPDebug = 3;
$mail->IsSMTP();
//$mail->IsSMTP();
$mail->Debugoutput = 'html';
$mail->Host = 'relay-hosting.secureserver.net';
$mail->Port = 25;
$mail->SMTPSecure = false;
$mail->SMTPAuth = false;
$mail->Username = "exemple#gmail.com";
$mail->Password = "password";
///this is the thing that change for one script to another
$name = strip_tags($_POST['name']);
$email = strip_tags($_POST['email']);
$message = strip_tags($_POST['message']);
/////
$mail->setFrom($email, $name);
$mail->addAddress('me#exemple.io');
$mail->Subject = 'Contact Site';
$mail->Body = $message;
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}

PHPMailer sending to blank page

I am trying to get PHPMailer to work to send mail from form data(obviously).
ISSUE:
When submitting form, I am getting sent to a blank page. No errors are being reported. I have followed as many stackoverflow threads surrounding this issue as I could find, and no solutions in those threads resolved the issue for myself.
Thanks
ERRORS:
Fixed all errors aside from: 2016-03-26 22:10:43 SMTP ERROR: Failed to connect to server: Connection refused (111) 2016-03-26 22:10:43 SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
PHP:
<?php
$message=
'First Name: '.$_POST['first'].'<br />
Last Name: '.$_POST['last'].'<br />
Phone: '.$_POST['phone'].'<br />
Email: '.$_POST['email'].'<br />
Message: '.$_POST['message'].'
';
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->IsSMTP(); // Sets up a SMTP connection
$mail->CharSet = 'UTF-8';
$mail->SMTPAuth = true; // Connection with the SMTP does require authorization
$mail->SMTPSecure = "tls"; // Connect using a TLS connection
$mail->Host = "smtp.gmail.com"; //Gmail SMTP server address
$mail->Port = 587;
// Authentication
$mail->Username = "xxxx#gmail.com"; // Your full Gmail address
$mail->Password = "xxxx"; // Your Gmail password
// Compose
$mail->SetFrom($_POST['email'], $_POST['firstname'] . ' ' . $_POST['lastname']);
$mail->AddReplyTo($_POST['email'], $_POST['firstname'] . ' ' . $_POST['lastname']);
$mail->Subject = "New Contact Form Enquiry"; // Subject (which isn't required)
$mail->MsgHTML($message);
// Send To
$mail->AddAddress("xxxx#gmail.com", "xxxx"); // Where to send it - Recipient
$result = $mail->Send(); // Send!
$message = $result ? 'Successfully Sent!' : 'Sending Failed!';
unset($mail);
}
?>
HTML:
<form class="form-inline" name="contactform" action="mailer.php" enctype="multipart/form-data" method="POST">
<div class="col-sm-6 form-group form-sty">
<label class="sr-only" for="first" required>First Name</label>
<input type="text" class="form-control-sty" name="first" placeholder="First Name">
</div>
<div class="col-sm-6 form-group form-sty">
<label class="sr-only" for="last" required>Last Name</label>
<input type="text" class="form-control-sty" name="last" placeholder="Last Name">
</div>
<div class="col-sm-6 form-group form-sty">
<label class="sr-only" for="phone" required>Phone</label>
<input type="text" class="form-control-sty" name="phone" placeholder="Phone">
</div>
<div class="col-sm-6 form-group form-sty">
<label class="sr-only" for="email" required>Email</label>
<input type="email" class="form-control-sty" name="mailfrom" placeholder="Email">
</div>
<div class="col-sm-12 form-sty">
<label class="sr-only" for="comment">Comment</label>
<textarea class="form-control-sty" name="message" id="message" rows="7" placeholder="What's on your mind?" required></textarea>
</div>
<div class="col-sm-12 form-sty form-sty-btn">
<button type="submit" value="Submit" class="btn btn-default col-sm-12">Send</button>
</div>
<p><?php if(!empty($message)) echo $message; ?></p>
</form>
You need to redirect the header back to the form (I assume that is where you want to send them) Replace the location inside the header with your URL
<?php
$message=
'First Name: '.$_POST['first'].'<br />
Last Name: '.$_POST['last'].'<br />
Phone: '.$_POST['phone'].'<br />
Email: '.$_POST['email'].'<br />
Message: '.$_POST['message'].'
';
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer();
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->IsSMTP(); // Sets up a SMTP connection
$mail->CharSet = 'UTF-8';
$mail->SMTPAuth = true; // Connection with the SMTP does require authorization
$mail->SMTPSecure = "tls"; // Connect using a TLS connection
$mail->Host = "smtp.gmail.com"; //Gmail SMTP server address
$mail->Port = 587;
// Authentication
$mail->Username = "xxxx#gmail.com"; // Your full Gmail address
$mail->Password = "xxxx"; // Your Gmail password
// Compose
$mail->SetFrom($_POST['email'], $_POST['firstname'] . ' ' . $_POST['lastname']);
$mail->AddReplyTo($_POST['email'], $_POST['firstname'] . ' ' . $_POST['lastname']);
$mail->Subject = "New Contact Form Enquiry"; // Subject (which isn't required)
$mail->MsgHTML($message);
// Send To
$mail->AddAddress("xxxx#gmail.com", "xxxx"); // Where to send it - Recipient
$result = $mail->Send(); // Send!
if ($result)
{
header("Location: : http://www.example.com/contactform.php");
}
else {
echo "sending mail failed: " . $mail->ErrorInfo;
}
unset($mail);
}
?>
You should make sure there are no errors. Try placing this code at the top of your file to make sure all errors/warnings are reported and run the script again.
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(E_ALL);

Mail send function not getting called from .php Customer Form

I am new to PHP coding. I have created a Customer form, followed by send e-mail function. When a Customer submits data (including E-Mail and password) he should receive a confirmation e-mail with User ID (same as entered E-Mail Id) and entered Password.
At this moment, with my current code, when a Customer submits the form, database gets updated, but no e-mail is send to the user. Can anyone point out what needs to be changed to ensure that send e-mail function works correctly.
<!DOCTYPE html>
<html>
<head>
<title>Registration Desk</title>
</head>
<body>
<div class="form">
<form id="contactform" action="reg_submit.php" method="post">
<p class="contact"><label for="name">Name</label></p>
<input id="name" name="name" placeholder="First and last name" required="" tabindex="1" type="text">
<p class="contact"><label for="add">Address</label></p>
<textarea id="add" name="add" style="width:85%; height:60%; margin-top:1%" required=""></textarea>
<fieldset>
<label>Birthday</label>
<input type="date" name="dob" value="yyyy-mm-dd" required="">
</fieldset>
<label>I am</label> <br><br>
<input type="radio" name="sex" value="male" >Male
<input type="radio" name="sex" value="female">Female
<p class="contact"><label for="email">Email</label></p>
<input id="email" name="email" class="field" placeholder="Please enter a valid emailid" required="" type="email">
<p class="contact"><label for="password">Create a password</label></p>
<input type="password" id="password" name="password" required="">
<p class="contact"><label for="repassword">Confirm your password</label></p>
<input type="password" id="repassword" name="repassword" required="">
<br><br>
<p class="contact"><label for="phone">Mobile phone</label></p>
<input id="phone" name="phone" placeholder="phone number" required="" type="text"> <br>
<input class="buttom" name="submit" id="submit" tabindex="5" value="Sign me up!" type="submit">
</form>
</div>
</div>
</body>
</html>
<?php
error_reporting( 0);
$connect=mysqli_connect("localhost","wbtecqoj_saltee","webuildtec1","wbtecqoj_salteegroup");
if(mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if( $_POST["password"]!= $_POST["repassword"])
{
?>
<script type="text/javascript">
alert("password miss matched!!!.");
history.back();
</script>
<?php
}
if($_POST["name"] && $_POST["add"] && $_POST["phone"] && $_POST["email"] && $_POST["password"] && $_POST["dob"] )
{
$insert="INSERT INTO `db`.`new_user` (`id`, `name`, `add`, `contact`, `email`, `pass`, `dob`, `gender`,`code`) VALUES (NULL, '$_POST[name]', '$_POST[add]', '$_POST[phone]', '$_POST[email]', '$_POST[password]', '$_POST[dob]', '$_POST[sex]', 'SAL1000000')";
if(!mysqli_query($connect,$insert))
{
echo die('Error: ' . mysqli_error($connect));
}
else
{
$to=$_POST['email'];
$from= "admin#wbtec.in";
$subject="Welcome to xyz Group";
$message="Welcome " . $_POST['name'] . "Dear Customer,
Your Card Number is " .$Ccode.$cookieId. " User id -" . $_POST['email'] . "Your Password -" . $_POST['password'].
"Thanking You,
Customer Care,";
$header = "From" .$from;
if(isset($_POST['btnSend']))
{
$res = mail($to,$subject,$message,$header);
if($res)
{
echo 'Message send';
}
else
{
echo 'msg not send';
}
}
}}
mysqli_close($connect);
?>
Remove the
if(isset($_POST['btnSend']))
{
}
You are checking for
if(isset($_POST['btnSend']))
But your form doesn't contain an input named btnSend, you should be able to remove that if statement altogether
I suggest you to use PHPMailer from
http://sourceforge.net/projects/phpmailer/
With the PHPMailer class you can specify the outgoing SMTP server and authenticate with it properly. Here is an example how I used it:
<?php
require("class.phpmailer.php");
class Mailer
{
function send($email, $subject, $body, $attachment1)
{
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host = "mail.xyz.com"; // your SMTP servers
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "authname"; // SMTP username
$mail->Password = "authpassword"; // SMTP password
$mail->From = "info#xyz.com";
$mail->FromName = "info#xyz.com";
$mail->AddAddress($email);
$mail->WordWrap = 50; // set word wrap
if ($attachment1 != "") {
$mail->AddAttachment($attachment1); // attachment1
}
$mail->IsHTML(false); // send as HTML
$mail->Subject = $subject;
$mail->Body = $body;
if(!$mail->Send())
{
return -1;
}
else
{
return 0;
}
}
}
?>
Hope that helps!
u have to download php mailer library which have to files
PHP mailer
class.phpmailer.php and other class.smtp.php then add this file at same directory which have php code which is shown below..
<?PHP
require_once('class.phpmailer.php');
if('POST' == $_SERVER['REQUEST_METHOD']) {
// process the form here
$subject = "This is subject testing1";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->IsHTML(true); // send as HTML
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port
//$mail->Username = "gmailusername"; // GMAIL username
//$mail->Password = "password"; // GMAIL password
$mail->From = 'email id';
$mail->FromName = "FrontName";
$mail->Subject = $subject;
$mail->Body = "this is html body"; //HTML Body
$emailId='email id';
$emails = explode(",",$emailId);
foreach($emails as $email)
$mail->AddAddress($email);
if($mail->Send()){
echo "Message sent successfully";
}else{
echo "Mailer Error: " . $mail->ErrorInfo;
}
}
?>
<form action="" method="post">
<input type="submit" name='button1'value="sent mail" />
</form>

Categories