Mailer with php not working [duplicate] - php

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>';
?>

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>';

Email PHP form doesn't work [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I haven't written code in years, so I am going off old code from a project that is over 5 years old, so I am not surprised that it doesn't work; I would like some pointers on how to make it work, please.
Here is what I have in my HTML email form --
<form action="fydcontact.php" method="post">
<div class="form-group">
<!--<label for="contact_name">Name:</label>-->
<input type="text" id="contact_name" class="form-control" placeholder="Name" />
</div>
<div class="form-group">
<!--<label for="contact_email">Email:</label>-->
<input type="text" id="contact_email" class="form-control" placeholder="Email Address" />
</div>
<div class="form-group">
<!--<label for="contact_message">Message:</label>-->
<textarea id="contact_message" class="form-control" rows="9" placeholder="Write a message"></textarea>
</div>
<button type="submit" class="btn btn-primary">Send</button>
</form>
and here is what my PHP looks like --
<?php
if(isset($_POST['send'])) {
// Prepare the email
$to = ''foryourdayformals#gmail.com ';
$name = $_POST['contact_name'];
$mail_from = $_POST['contact_email'];
$subject = 'Message sent from website';
$message = $_POST['contact_message'];
$header = "From: $name <$mail_from>";
// Send it
$sent = mail($to, $subject, $message, $header);
if($sent) {
echo 'Your message has been sent successfully! Return to For Your Day, LLC Website';
} else {
echo 'Sorry, your message could not send. Please use direct email link on Contact Us page (below the map). Return to For Your Day, LLC Website';
}
}
?>
Any help is GREATLY appreciated! Thanks!
update
$to = ''foryourdayformals#gmail.com ';
to
$to = 'foryourdayformals#gmail.com';
and need to add name attribute for form . This update your form
<form action="fydcontact.php" method="post">
<div class="form-group">
<!--<label for="contact_name">Name:</label>-->
<input type="text" id="contact_name" name="contact_name" class="form-control" placeholder="Name" />
</div>
<div class="form-group">
<!--<label for="contact_email">Email:</label>-->
<input type="text" id="contact_email" name="contact_email" class="form-control" placeholder="Email Address" />
</div>
<div class="form-group">
<!--<label for="contact_message">Message:</label>-->
<textarea id="contact_message" name="contact_message" class="form-control" rows="9" placeholder="Write a message"></textarea>
</div>
<button type="submit" class="btn btn-primary" name ="send">Send</button>
</form>

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);
}

Message body empty in contact form (using PhPMailer)

I'm having a little problem with my contact form.
I'm using PhpMailer and Bootstrap contact form. When I run the code I get this message:
"Uncaught exception 'phpmailerException' with message 'Message body empty'"
This is my code:
$name = $_POST['InputName'];
$company = $_POST['InputFirma'];
$email = $_POST['InputEmail'];
$phone = $_POST['InputPhone'];
$message = $_POST['InputSubject'];
require '../../PHPMailer-master/PHPMailerAutoload.php';
require '../../PHPMailer-master/class.smtp.php';
$mail = new PHPMailer(true);
$mail->SMTPDebug = false; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'poczta.cgsa.com.pl'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'sample#sample.pl'; // SMTP username
$mail->Password = 'FU86m6BSp7'; // SMTP password
$mail->Port = 587;
$mail->setFrom('sample#sample.pl', 'Giełd');
$mail->addAddress('sample#sample.pl', 'Odbiorca'); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
if(!$mail->send()) {
echo 'Wiadomość nie mogła zostać wysłana';
echo "<br><br><br><hr><br>";
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Wiadomość została wysłana';
}
$Body = "Wiadomość od: $name\n E-Mail: $email\n Firma: $company\n";
$success = mail($name, $company, $phone, $message);
This is my HTML:
<form class="padding-top-40" role="form" id="contactForm" class="contact-form" data-toggle="validator" class="shake">
<div class="form-group">
<label for="InputName">Imię i nazwisko</label>
<input type="text" class="form-control" id="InputName" name="fullname" placeholder="Imię i nazwisko" required data-error="Proszę wpisać swoje imię i nazwisko">
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<label for="InputFirma">Firma</label>
<input type="text" class="form-control" id="InputFirma" name="subject" name="comments" placeholder="Firma" required data-error="Proszę wpisać nazwę firmy">
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<label for="InputEmail">E-mail</label>
<input type="email" class="form-control" id="InputEmail" name="emailid" placeholder="E-mail" required data-error="Proszę wpisać swój email">
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<label for="InputPhone">Telefon kontaktowy</label>
<input type="number" class="form-control" name="phone" id="InputPhone" placeholder="Numer telefonu" required data-error="Proszę wprowadzić numer telefonu">
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<label for="InputSubject">Temat</label>
<textarea type="text" class="form-control" name="subject" id="InputSubject" placeholder="Treść wiadomości" rows="4" required data-error="Proszę wpisać treść wiadomości"></textarea>
<div class="help-block with-errors"></div>
</div>
<div class="padding-top-20">
<button type="submit" value="send" class="btn btn-default" id="submit" >Wyślij</button>
<div id="msgSubmit" class="h3 text-center"></div>
</div>
</form>
Question
How can I address the error message?
You're just doing things in the wrong order. You need to set the Body property (and not just a variable called $Body) before you send the message, and you don't need to call mail() at all.
$mail->Body = "Wiadomość od: $name\n E-Mail: $email\n Firma: $company\n";
if(!$mail->send()) {
echo 'Wiadomość nie mogła zostać wysłana';
echo "<br><br><br><hr><br>";
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Wiadomość została wysłana';
}
You're using the auoloader, so you don't need to require the SMTP class separately, it will be loaded automatically.
You are enabling exceptions (by passing true in the constructor), but you are not wrapping your code in a try/catch block to deal with any that may happen.

PHP Mailer - Additional Fields

I have searched and researched high and low and I am still unable to find an answer.
I am using PHP Mailer that came with a theme we purchased (version 5.2.1). I am trying to figure out how to capture the phone number and send it along with the other data in the email that is sent.
The Form:
<form method="post" action="#" id="contactForm">
<div class="input-prepend" style="margin-left:0px;margin-right:0px">
<input class="input-xlarge required" id="name" type="text" placeholder="Name (Required)" />
</div>
<div class="input-prepend" style="margin-left:0px">
<input class="input-xlarge required" id="email" type="text" placeholder="Email (Required)" />
</div>
<div class="input-prepend" style="margin-left:0px">
<input class="input-xlarge required" id="phone" name="phone" type="text" placeholder="Phone (Required)" />
</div>
<div class="input-prepend" style="margin-left:0px">
<input class="input-xlarge required" id="subject" type="text" placeholder="Subject (Required)" />
</div>
<div class="input-prepend" style="margin-left:0px">
<input class="input-xlarge required" id="children-3-7" type="text" placeholder="Number of Children (Age 3 - 7) (Required)" />
</div>
<div class="input-prepend" style="margin-left:0px">
<input class="input-xlarge required" id="children-7-14" type="text" placeholder="Number of Children (Age 7 - 14) (Required)" />
</div>
<div class="input-prepend" style="margin-left:0px">
<input class="input-xlarge required" id="adults" type="text" placeholder="Number of Adults (Required)" />
</div>
<div class="input-prepend" style="margin-left:0px">
<textarea name="message-input" id="message" class="span12 required" cols="5" rows="5" placeholder="Message (Required)" style="margin-left:0px"></textarea>
</div>
<div class="clearfix" style="margin:10px 0px 0px 0px">
<span>Reset Form</span>
<span>Send Message</span>
<!-- Processing data trick -->
<div id="loadingForm">
<img src="images/normal/ajax-small.gif" alt="ajax-small" />
</div>
<!-- Processing data trick -->
</div>
<br />
<div class="clearfix">
<!-- contact notice -->
<div class="alert alert-block fade" id="contact-notice">
<span><strong>Warning!</strong> All fields above are required to send the message properly.</span>
</div>
<!-- contact notice -->
</div>
</form>
The PHP
<?php
require_once("class.phpmailer.php");
send();
function send(){
$mailer = new PHPMailer();
$mailer->IsSMTP();
$mailer->Host = 'host.com';
$mailer->SMTPAuth = true;
$mailer->SMTPSecure = "tls";
$mailer->Port = 587;
$mailer->CharSet="utf-8";
$mailer->IsHTML(true);
$mailer->Username = 'mail-bot#mail.com';
$mailer->Password = 'PASSWORD';
$mailer->FromName = $_POST['name'];
$mailer->From = $_POST['email'];
$mailer->AddAddress('ADDRESS','NB=AME');
$mailer->Subject = $_POST['subject'];
$mailer->MsgHTML = $_POST['message'];
$phone = $_POST['phone'];
/**$mailer->Age1 = $_POST['children-3-7'];
//$mailer->Age2 = $_POST['children-7-14'];
//$mailer->Age3 = $_POST['adults'];**/
$mailer->Body = "From : ".$_POST['name']."<br /> E-mail : ".$_POST['email']."<br /> Subject : ".$_POST['subject']."<br />Message : ".$_POST['message']."<br />Phone : $phone";
if(!$mailer->Send()){
echo "error^Message could not be sent.<br />";
echo "Mailer Error: " . $mailer->ErrorInfo;
exit;
}
else{
echo "ok^Message sent successfully!";
}
}
?>
As you can see I am attempting to use
$phone = $_POST['phone'];
to extract the phone number and then adding it to the message body by using
$mailer->Body = "From : ".$_POST['name']."<br /> E-mail : ".$_POST['email']."<br /> Subject : ".$_POST['subject']."<br />Message : ".$_POST['message']."<br />Phone : $phone";
To inject the phone number
I have also tried injecting in the following way
$mailer->Body = "From : ".$_POST['name']."<br /> E-mail : ".$_POST['email']."<br /> Subject : ".$_POST['subject']."<br />Message : ".$_POST['message']."<br />Phone: ".$_POST['phone'];
Any help will be greatly appreciated.
It appears as if there was a secondary validation sitting in our main .js file that was validating empty fields before posting them via ajax. Phone number needed to be added in there before it posted it and I can now successfully capture the data.
Thanks for your help!

Categories