HTML form not sending all data - php

I have a website form where someone can send me a message. They have to enter their name, email and a message.
The form works to a point in that I get the email and the senders name and email they entered is shown in the email, but not the message they enter.
Can anyone see where the problem lies please?
Here is the HTML form
<form method="post" action="php/mail.php" name="cform" id="cform">
<input name="name" id="name" type="text" class="col-xs-12 col-sm-6 col-md-6 col-lg-6" placeholder="Your name..." >
<input name="email" id="email" type="email" class=" col-xs-12 col-sm-6 col-md-6 col-lg-6 noMarr" placeholder="Your email..." >
<textarea name="message" id="message" cols="" rows="" class="col-xs-12 col-sm-12 col-md-12 col-lg-12" placeholder="Your message..."></textarea>
<input type="submit" id="submit" name="send" class="submitBnt" value="Send message">
</form>
And here is the php/mail.com that is uses (connection details changed to anonymous details)
<?
require("class.phpmailer.php");
//form validation vars
$formok = true;
$errors = array();
//sumbission data
$ipaddress = $_SERVER['REMOTE_ADDR'];
$date = date('d/m/Y');
$time = date('H:i:s');
//form data
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP - mail or smtp.domain.com
$mail->Host = "myhost.myprovider.com"; // SMTP servers
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "info#myaddress.com"; // SMTP username
$mail->Password = "mypassword"; // SMTP password
$mail->From = "info#myaddress.com"; // SMTP username
$mail->AddAddress("info#myaddress.com"); // Your Adress
$mail->Subject = "New contact request from ME !";
$mail->IsHTML(true);
$mail->CharSet = 'UTF-8';
$mail->Body = "<p>You have recieved a new message from the enquiries form on your website.</p>
<p><strong>Name: </strong> {$name} </p>
<p><strong>Email Address: </strong> {$email} </p>
<p><strong>Subject: </strong> {$subject} </p>
<p><strong>Message: </strong> {$message} </p>
<p>This message was sent from the IP Address: {$ipaddress} on {$date} at {$time}</p>";
if(!$mail->Send())
{
echo "Mail Not Sent <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}

There is no reason for, as you say, name and email to be shown and message to be not.

I think you need enctype for textareas ... but im not completely sure. Try it, it wont hurt you:
<form enctype="multipart/form-data" method="post" action="php/mail.php" name="cform" id="cform">

Might want to take a look at this similar question
Someone had the same issue and all the had to do was change the id and name to something different
Issue using $_POST with a textarea
As to the issue with the subject being shown I saw you commented about, you never pass a subject to $_POST unless you are in some other code that is not posted
EDIT 1:
If you are manually setting the subject then why try to get it from $_POST as well as trying to do <p><strong>Subject: </strong> {$subject} </p> will not work because $subject has no value from $_POST

Related

How to make a functional PHP contact form that sends email to #outlook.com account on a website that is going to be published on ftp server?

I am building a website that has a php contact form that will send an email on #outlook.com address. Website will be published on ftp server, but till then I published it on this free one https://www.000webhost.com/ so I can test contact form but it is not working. I've read somewhere that php mail() function does not work with smtp so I tried using PHPMailer as I found in some suggestions here (Trying to send email through PHP using an email address that is configured on Microsoft's SMTP servers (#live.com, #outlook.com, etc.)) but it's also not working or maybe I'm not doing it correctly since I don't quite understand it. The code is written without using PHPMailer:
PHP:
<?php
if ($_POST["submit"]) {
if (!$_POST['name']) {
$error="<br />Please enter your name";
}
if (!$_POST['email']) {
$error.="<br />Please enter your email address";
}
if (!$_POST['message']) {
$error.="<br />Please enter a message";
}
if ($_POST['email']!="" AND !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)) {
$error.="<br />Please enter a valid email address";
}
if ($error) {
$result='<div class="alert alert-danger"><strong>There were error(s)in your form:</strong>'.$error.'</div>';
} else {
if (mail("mymail#outlook.com", "message from website!", "Name: ".$_POST['name']."Email: ".$_POST['email']."message: ".$_POST['message'])) {
$result='<div class="alert alert-success"><strong>Thank you!</strong> I\'ll be in touch.</div>';
} else {
$result='<div class="alert alert-danger">Sorry, there was an error sending your message. Please try again later.</div>';
}
}
}
?>
HTML:
<div>
<?php echo $result; ?>
<form method="POST" enctype="multipart/form-data ">
<div class="form-group ">
<label><h3>Your name:</h3></label>
<input name="name" type="text " id="exampleInputName" class="form-control" placeholder="Name" value="<?php echo $_POST['name']; ?>">
</div>
<div class="form-group ">
<label><h3>Your e-mail:</h3></label>
<input name="email" type="email " id="exampleInputEmail" class="form-control" placeholder="name#email.com" value="<?php echo $_POST['email']; ?>">
</div>
<div class="form-group ">
<label><h3>Message:</h3></label>
<textarea name="message" class="form-control " rows="5" placeholder="Your message... " value="<?php echo $_POST['message']; ?>"></textarea>
</div>
<div class="col text-right">
<div class="form-group ">
<button class="button" type= "submit" name="submit" value="Submit">send</button>
</div>
</div>
</form>
</div>
Another separate code where I tried using PHPMailer looks like this, which is basically copied from this link getting custom php contact form to send email to exchange 365 account
:
<?php
if ($_POST["submit"]) {
include("PHPMailer-master/class.PHPMailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = "localhost";
$mail->SMTPAuth = false;
$mail->Username = "mymail#outlook.com"; #or do i put my account for webhostapp?
$mail->Password = "my_password_for_outlook";
$mail->From = 'https://webpage.000webhostapp.com/';
$mail->FromName = $_POST['name'];
$mail->AddAddress('mymail#outlook.com');
$mail->IsHTML(true);
$mail->Subject = 'Subject line goes here';
$mail->Body = $_POST['message'];
$mail->AltBody = $_POST['message'];
$mail->Send();
}?>
result after submitting form

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.

PHPMailer sending e-mail without form data

I'm trying to send a e-mail with a contact form but it`s not showing the data.
I've tried some answers here but can't get it to work.
My code:
<form class="form form-container method="post" action="assets/send_form_email.php">
<div class="left">
<input name="f_name" type="text" id="f_name" placeholder="Nome*" required />
<input name="f_email" type="email" id="f_email" placeholder="Email*" required />
<input name="f_phone" type="text" id="f_phone" required="required" maxlength="15" placeholder="Telefone*" />
<textarea name="f_msg" id="f_msg" placeholder="Mensagem" rows="6"></textarea>
</div>
<div class="right">
<input name="f_cnpj" type="text" id="f_cnpj" placeholder="CNPJ" style="font-weight: 700;" required />
<input name="f_sector" type="text" id="f_sector" placeholder="Setor da empresa" style="font-weight: 700;" required />
<input name="f_faturamento" type="text" id="f_faturamento" placeholder="Faturamento médio" style="font-weight: 700;" required />
<input name="f_valorMedio" type="text" id="f_valorMedio" placeholder="Valor médio de duplicatas" style="font-weight: 700;" required />
<button type="submit" class="botaoContato">Enviar</button>
</div>
</form>
Here is the PHP:
<?php
$nome = $_POST['f_name'];
$email = $_POST['f_email'];
$telefone = $_POST['f_phone'];
$cnpj = $_POST['f_cnpj'];
$setor = $_POST["f_sector"];
$faturamento = $_POST['f_faturamento'];
$valorMedio = $_POST['f_valorMedio'];
$msg = $_POST["f_msg"];
$mensagem = "Nome: '.$nome.' <br>
Email: '$email.' <br>
Telefone: '.$telefone.' <br><br>
CNPJ: '.$cnpj.' <br>
Setor: '.$setor.' <br>
Faturamento: '.$faturamento.' <br>
Valor Medio: '.$valorMedio.' <br>
Mensagem: '.$msg.' <br>";
require_once("class.phpmailer.php");
$mail = new PHPMailer();
$mail->SMTPDebug = 2;
$mail->IsSMTP();
$mail->Host = "smtp.****.com.br";
$mail->SMTPAuth = true;
$mail->Username = 'contato#****.com.br';
$mail->Password = '********';
$mail->From = "contato#*****.com.br";
$mail->Sender = "contato#****.com.br";
$mail->FromName = "****";
$mail->AddAddress('contato#*****.com.br');
$mail->IsHTML(true);
$mail->CharSet = 'UTF-8';
$mail->Subject = "Contato Site ";
$mail->Body = $mensagem;
$mail->AltBody = $mensagem;
$enviado = $mail->Send();
$mail->ClearAllRecipients();
$mail->ClearAttachments();
if ($enviado) {
echo "E-mail enviado com sucesso!";
} else {
echo "Não foi possível enviar o e-mail.
";
echo "Informações do erro:
" . $mail->ErrorInfo;
}
I've tried removing the variables and adding it directly to the $mail->Body but it does not work. I don't know much PHP so I'm relying on posts here or some tutorials but I can't find the error. :(
The most important thing when you ask a question like this is to describe exactly how it's not working - we can't guess.
First of all, you're using a very old version of PHPMailer, so get the latest, and base your code on the contact form example provided with PHPMailer.
You're sending only plain text content, so do this:
$mail->isHTML(false);
$mail->Body = $mensagem;
You don't need to set AltBody when you're doing this.
If your message is arriving, but doesn't contain what you expect, do this just before calling send() to confirm what parameters the script received, and what you asked it to send:
var_dump($_POST, $mail->Body);
You have SMTPDebug = 2, so you should be seeing debug output. If you get a false return value from send(), any error should be in $mail->ErrorInfo - you'll see how that can be used in the example code I linked to.
If you're seeing no SMTP debug output, it means it's having trouble connecting at all, in which case you should read the troubleshooting guide which tells you how to diagnose connection problems. The usual reasons are that your hosting provider may block outbound SMTP, or that you have outdated CA certificates.

Contact form rejected by server

I am pretty new at this so I apologize up front. Sorry for asking this question for the millionth time but I can't get my contact form to mail. I receive server error 500. I tried to find out what that meant without success. I have looked though all of the other posts here without success. Thanks for helping.
Here's my html:
Contact Form -->
<form id="contact-form" action="sendEmail.php" method="post">
<p>
<span>
<input placeholder="Name" type="text" name="name" required>
</span>
</p>
<p>
<span>
<input placeholder="Email" type="email" name="email" required>
</span>
</p>
<p>
<span>
<textarea placeholder="Message" name="message" cols="40" rows="10"></textarea>
</span>
</p>
<p>
<input type="submit" value="Send Message">
</p>
</form>
Here's the php:
<?php
// Form Variables
$sender_name = trim($_POST['name']);
$sender_email = $_POST['email'];
$sender_message = $_POST['message'];
// Configuration Vaiables
$receiving_email = 'name#email.com'; // Replace this with your own email address
$receiver_name = 'name'; // replace with your name
$email_subject = 'Email Sent from Contact Form'; // replace with any default title
// Require swiftMailer Library
require_once('lib/classes/swiftMailer/swift_required.php');
// Create the transport
$transport = Swift_SmtpTransport::newInstance('smtp.rickstrode.com') // enter SMTP address
->setPort(587) // enter SMTP port
->setEncryption('ssl') // enter encryption mode
->setUsername('name#email.com') // enter SMTP username
->setPassword('abcd1234') // enter SMTP password
;
// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
// Create a Message
$message = Swift_Message::newInstance($email_subject)
->setFrom(array($sender_email => $sender_name))
->setTo(array($receiving_email, $receiving_email => $receiver_name))
->setBody($sender_message)
;
// Send the Message
$result = $mailer->send($message);
// Success and Failure Message
if ($result) {
echo "Congratulations, We've received your email. We'll be in touch as soon as we possibly can!";
} else {
echo "Unfortunately, Something went wrong while sending the message, Please try again!";
}
?>
Your SMTP server name is incorrect
$transport = Swift_SmtpTransport::newInstance('smtp.rickstrode.com')
should be
$transport = Swift_SmtpTransport::newInstance('mail.rickstrode.com')

PHPMailer not sending e-mail through Gmail, returns blank page

I am trying to set up a contact form at the bottom of my page. I am using PHPMailer and trying to receive emails at my gmail account. Every time I submit the form, my url changes to url/email.php and I get a blank page. What am I doing wrong? Can anyone see the glaring error that I am obviously missing?
HTML:
<div class="container-fluid">
<form action="email.php" id="contact-me" method="post" name="contact-me">
<div class="row-fluid">
<div class="form-group">
<div class="col-md-3 col-md-offset-3">
<input class="input" name="name" id="name" placeholder="Name" type="text">
</div>
<div class="col-md-3">
<input class="input" id="email" name="email"
placeholder="Email" type="text">
</div>
</div>
</div>
<div class="form-group">
<textarea class="input input-textarea" id="message" name="message" placeholder="Type your message here" rows=
"5"></textarea>
</div>
<input name="send" class="send" type="submit" value="Get In Touch">
</form>
</div>
</section>
PHP:
<?php
if (isset($_POST['submit']))
{
require('PHPMailer-master/PHPMailerAutoload.php');
require_once('PHPMailer-master/class.smtp.php');
include_once('PHPMailer-master/class.phpmail.php');
$name = strip_tags($_Post['name']);
$email = strip_tags($_POST['email']);
$msg = strip_tags($_POST['message']);
$subject = "Message from Portfolio";
$mail = new PHPMailer();
$mail->isSMTP();
$mail->CharSet = 'UTF-8';
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->Username = 'me#gmail.com';
$mail->Password = '***********';
$mail->From = $email;
$mail->FromName = $name;
$mail->AddAddress("me.com", "Katia Sittmann");
$mail->AddReplyTo($email, $name);
$mail->isHTML(true);
$mail->WordWrap = 50;
$mail->Subject =$subject;
$mail->Body = $msg;
$mail->AltBody ="No message entered";
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}else{
header("Location: thank-you.html");
}
}
?>
You've got some very confused code here. You should start out with an up-to-date example, not an old one, and make sure you're using the latest PHPMailer (at least 5.2.10).
Don't do this:
require('PHPMailer-master/PHPMailerAutoload.php');
require_once('PHPMailer-master/class.smtp.php');
include_once('PHPMailer-master/class.phpmail.php');
All you need is this, which will auto-load the other classes if and when they are needed:
require 'PHPMailer-master/PHPMailerAutoload.php';
This won't work:
$name = strip_tags($_Post['name']);
PHP variables are case-sensitive, so do this:
$name = strip_tags($_POST['name']);
This will cause problems:
$mail->From = $email;
When you do this, you're forging the From address, and it will get rejected by SPF checks. Put your own address in here, and let the reply-to carry the submitter's address, as you're already doing.
Submit may not be included in the submission if the form is not submitted via that control, e.g. if it's submitted by hitting return in a text input. Check for a field that you know will always be in a submission:
if (array_key_exists('email', $_POST)) {...
Adding a try/catch will not achieve anything - PHPMailer does not throw exceptions unless you ask it to, and you are not.
You are applying strip_tags on the body, but then calling $mail->isHTML(true); - do you want HTML or not?
All that said, none of those things will cause a blank page. It's fairly likely you are running into gmail auth problems which are popular lately, and to see that you need to enable debug output:
$mail->SMTPDebug = 2;
and then read the troubleshooting guide on what you see.

Categories