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.
Related
I've finally built my first website and gotten the hosting figured out. As stated in the title I'm using hostinger and phpmailer, I have never used PHP so I went through their short tutorial and got the PHP file set up how I feel is the correct way.
The issue I'm having is that when I submit the form, I get an error page. Nothing in the network tab on chrome suggests that it is trying to send any data, although it does show it opening the PHP file, I think, again this is my first website and first time using PHP.
PHP:
<?php
use PHPMailer\PHPMailer\PHPMailer;
require 'vendor/autoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.hostinger.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = 'hostinger-email#domain.com';
$mail->Password = 'password for hostinger email';
$mail->setFrom('same-as-username??', 'Admin');
$mail->addAddress('destination#gmail.com', 'Savannah');
if ($mail->addReplyTo($_POST['email'], $_POST['name'])) {
$mail->Subject = 'PHPMailer contact form';
$mail->isHTML(false);
$mail->Body = <<<EOT
Email: {$_POST['email']}
Name: {$_POST['name']}
Message: {$_POST['message']}
EOT;
if (!$mail->send()) {
$msg = 'Sorry, something went wrong. Please try again later.';
} else {
$msg = 'Message sent! Thanks for contacting us.';
}
} else {
$msg = 'Share it with us!';
}
?>
HTML FORM:
<header class="form_header">Contact Me</header>
<form id="form" class="topBefore" action="/domains/piecesofblonde.com/public_html/formscript.php" method="post">
<input id="name" type="text" placeholder="NAME">
<input id="email" type="text" placeholder="E-MAIL">
<textarea id="message" type="text" placeholder="MESSAGE"></textarea>
<input id="submit" type="submit" value="GO!">
</form>
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.
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 4 years ago.
I'm running locally from my mac, when the info is entered and I click send the email is not showing up in the outgoing email account in sent mail, and not showing up in the incoming email either; spam folder included. There is not an error echo. The echo states the message is sent without error. I'm at a loss here. By the way I am a newbie at coding so please be gentle and explain in depth. I appreciate any help that anyone might be able to provide. Here is my code.
<form method="post" enctype="multipart/form-data">
<p>
E-Mail:
</p>
<p>
<input type="text" name="receiver" />
</p>
<p>
Subject:
</p>
<p>
<input type="text" name"subject" />
</p>
<p>
Note:
</p>
<p>
<textarea name="message"></textarea>
</p>
<p>
Select Photo:
<input type="file" name="file" />
</p>
<input type="submit" name="submit" value="Send Email" />
</form>
<?php
if (isset($_POST['submit'])) {
require "php-mailer-master/PHPMailerAutoload.php";
$mail = new PHPMailer;
try {
$sender = "????????????#gmail.com";
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '$sender';
$mail->Password = '????????????';
$mail->SMTPSecure = 'tls';
$mail->Port = '587';
$mail->isHTML();
$mail->setFrom($sender, 'Mailer');
$mail->addAddress($_POST["receiver"]);
$mail->addAttachment ($file_name);
$mail->isHTML(true);
$mail->Subject = $_POST["subject"];
$mail->Body = $_POST["message"];
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
}
?>
The code below will send a message for me. I replaced the ??? with my username and password (also had to set my gmail account to allow access).
Changes I made to your code:
I changed the require for my composer locally.
You used single quotes around the $sender - needs to be double quotes.
Added titles to the inputs (probably not needed).
Also used the namespaced values for instantiating $mail PHPMAILER();
Commented out the attachment feature.
I would also recommend checking into sanitizing your user input.
Something like:
$name = trim(filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING));
$email = trim(filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL));
Code:
<form method="post" enctype="multipart/form-data">
<p>
E-Mail:
</p>
<p>
<input type="text" name="receiver" title="receiver1"/>
</p>
<p>
Subject:
</p>
<p>
<input type="text" name="subject" title="subj1"/>
</p>
<p>
Note:
</p>
<p>
<textarea name="message" title="message"></textarea>
</p>
<p>
Select Photo:
<input type="file" name="file"/>
</p>
<input type="submit" name="submit" value="Send Email"/>
</form>
<?php
require '../vendor/autoload.php';
if (isset($_POST['submit'])) {
$mail = new \PHPMailer\PHPMailer\PHPMailer();
try {
$sender = "?????????#gmail.com";
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = "$sender";
$mail->Password = '?????????????';
$mail->SMTPSecure = 'tls';
$mail->Port = '587';
$mail->isHTML();
$mail->setFrom($sender, 'Mailer');
$mail->addAddress($_POST["receiver"]);
// $mail->addAttachment ($file_name);
$mail->isHTML(true);
$mail->Subject = $_POST["subject"];
$mail->Body = $_POST["message"];
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail-
>ErrorInfo;
}
}
?>
Here is my website: www.accurateaccountsinc.tk
The problem is that the website just loads and doesn't do anything.
Everything works properly in localhost/wamp but it doesn't work on the actual site when running the task.
here's the code for the form
<form method="POST" action="index.php">
<input type="text" required name="name" id="name" placeholder="Enter Your Name" />
<input type="text" required name="email" id="email" placeholder="Enter Your Email" />
<input type="text" required name="phone" id="phone" placeholder="Phone Number" />
<textarea name="message" required id="message" placeholder="Enter Your Message"></textarea>
<input type="submit" name="mailed" value="Submit" />
</form>
Here's the php:
<?php
include 'dbconn.php';
if(isset($_POST['mailed'])){
$name = mysqli_real_escape_string($con,$_POST['name']);
$emailadd = mysqli_real_escape_string($con,$_POST['email']);
$contact = mysqli_real_escape_string($con,$_POST['phone']);
$entrymessage = mysqli_real_escape_string($con,$_POST['message']);
include "class.phpmailer.php";
include "class.smtp.php";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMPTDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->IsHTML(true);
$mail->Username = "email here";
$mail->Password = "password here";
$mail->SetFrom("email here", 'Website Entry');
$subject = "Client Entry";
$message = "<br>Client Name: " . $name;
$message .= "<br>Email Address: " . $emailadd;
$message .= "<br>Contact Number: " . $contact;
$message .= "<br>Message: " . $entrymessage;
$mail->Subject = $subject;
$mail->Body = $message;
$mail->AddAddress("email here", $name);
if($mail->Send()){
unset($_POST['mailed']);
echo "<script>window.open('index.php','_self')</script>";
}
}
?>
if there's any more info you'd like to know please do ask..
also note that I only used the class.phpmailer.php and class.smtp.php to minimize the work needed if that's related to the problem..
Typo: $mail->SMPTDebug should be $mail->SMTPDebug, and you should set it to 2 for useful feedback.
You're also not displaying any errors that it may have caused, so echo $mail->ErrorInfo; if sending fails.
You can't send through gmail on port 25, only to gmail. Stick to tls on Port 587, like all the examples and docs say. I don't know where you got your code, but it looks like you used an old example - you should base it on the examples provided with PHPMailer, particularly the ones for gmail.
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.