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.
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 want to send email from the contact form on my site that has multiple form fields. But I can't seem to get that right.
Here is my form HTML code:
<form class="form-horizontal" action="mail.php" method="post">
<h2>Quick Contact</h2>
<div class="form-group">
<label class="control-label col-sm-2" for="email">Full Name:</label>
<div class="col-sm-6">
<input type="text" class="form-control" id="fname" placeholder="Enter Full Name" name="fname">
</div>
</div><br>
<div class="form-group">
<label class="control-label col-sm-2" for="num">ID Number:</label>
<div class="col-sm-6">
<input type="text" class="form-control" id="num" placeholder="Enter ID Number" name="id">
</div>
</div><br>
<div class="form-group">
<label class="control-label col-sm-2" for="email">Email:</label>
<div class="col-sm-6">
<input type="email" class="form-control" id="email" placeholder="Enter Email Address" name="email">
</div>
</div><br>
<div class="form-group">
<label class="control-label col-sm-2" for="sal">Monthly Salary:</label>
<div class="col-sm-6">
<input type="number" class="form-control" id="sal" placeholder="Enter Salary" name="salary">
<div class="label label-warning" >Numbers only. Any other characters are not permitted.</div>
</div>
</div><br>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-danger">Submit</button>
</div>
</div>
</form>
This is my PHP code, I am using PHP mail() to send the email:
<?php
$to = 'mail#gmail.com';
$subject = 'Loan Request';
$from = $_POST['email']; // required
$name = $_POST['fname']; // required
$id = $_POST['id']; // required
$telephone = $_POST['mobile']; // not required
$salary = $_POST['salary']; // required
$message = $name." ".$telephone." ".$salary;
$headers = 'From: '.$from."\r\n".
'Reply-To: '.$from."\r\n" ;
// Sending email
if(mail($to, $subject, $from, $message)){
header('Location: http://example.com');
} else{
echo 'Unable to send email. Please try again.';
}
?>
The mail delivers quite alright, but does not display the entire form details when it enters my email. I will like to know how I can capture all the form fields cos I intend to add more form fields. It's a really long form.
If you are using PHP's mail function then the parameters should be in this order. In your example the $headers parameter was not passed at all.
if (mail($to, $subject, $message, $headers)) {
header('Location: http://example.com');
} else {
echo 'Unable to send email. Please try again.';
}
first ensure ur localhost/server is configured to send email
use the following function to send email (works for me)
public function email($to,$cc,$from,$subject,$message){
$config['protocol']='mail';
$config['smtp_host']='ssl://smtp.gmail.com';
$config['smtp_port']='465';
$config['smtp_timeout']='30';
$config['smtp_user']='urEmail#gmail.com';
$config['smtp_pass']='urPassword';
$config['charset']='utf-8';
$config['newline']="\r\n";
$config['wordwrap'] = TRUE;
$config['mailtype'] = 'html';
$this->load->library('email',$config);
// $this->email->initialize($config);
$this->email->from($from, 'Limecar.in');
$this->email->to($to);
$this->email->cc($cc);
$this->email->subject($subject);
$this->email->message($message);
if ($this->email->send()) {return true;}
else {return false;}
}
I am a novice at PHP. I have a simple contact form. I got the code online, copy/pasted/tweaked. It will send the email fine, but 2 of the fields (email and state) are blank in the email. All the other fields are filled in. I have looked and I don't see any reason why. What am I missing? Thanks in advance for any help. Here is my code:
HTML
<form id="contactusForm" method="post" action="connect.php">
<div><blockquote>Please fill out this short form and we will respond as soon as possible. Nothing is required except your reason for contact and your message. Thank you for your time.</blockquote>
<br><br><br>
</div>
<div class="row">
<div class="col-25" id="help-with">
<label for="reason">Reason</label>
</div><div class="col-75">
<select name="reason" id="reason" type="select" tabindex="1">
<option value="Prayer">Request For Prayer</option>
<option value="Information">More Information</option>
</select></div> </div> <br>
<br>
<div class="row">
<div class="col-25">
<label for="name">Name</label>
</div>
<div class="col-75">
<input name="name" type="text" id="name" placeholder="Your name.." tabindex="2" title="Name" size="50" maxlength="50">
</div>
</div>
<div class="row">
<div class="col-25">
<label for="phone">Phone</label>
</div>
<div class="col-75">
<input name="phone" type="tel" id="phone" placeholder="Your phone number.." tabindex="3" title="Phone" size="30" maxlength="30">
</div>
</div>
<div class="row">
<div class="col-25">
<label for="email">Email</label>
</div>
<div class="col-75">
<input name="email" type="email" id="email" placeholder="Your email..." tabindex="4" title="Email" size="50" maxlength="50" >
</div>
</div>
<div class="row">
<div class="col-25">
<label for="state">State</label>
</div>
<div class="col-75">
<select name="state" id="state" type="select" tabindex="5">
<option value="pick-a-state">Select your state...</option>
<option value="NA">N/A</option>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
</select>
</div>
</div>
<div class="row">
<div class="col-25">
<label for="country">Country</label>
</div>
<div class="col-75">
<select name="country" id="country" type="select" tabindex="6">
<option value="">Select your country...</option>
<option value="United States">United States</option>
<option value="United Kingdom">United Kingdom</option>
<option value="Afghanistan">Afghanistan</option>
</select>
</div>
</div>
<div class="row">
<div class="col-25">
<label for="message">Your Message:</label>
</div>
<div class="col-75">
<textarea name="message" type="text" maxlength="800" required="required" id="message" style="height:180px" placeholder="Type your message here..." tabindex="7"></textarea>
</div>
</div><br>
<br>
<input type="checkbox" name="contact_me_by_fax_only" value="1" style="display:none !important" tabindex="-1" autocomplete="off">
<div class="row">
<input type="submit" tabindex="10" value="Submit">
</div>
</form>
PHP
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHP/PHPMailer/vendor/autoload.php';
$reason = $_POST['reason'];
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$phone = filter_var($_POST['phone'], FILTER_SANITIZE_STRING);
$mailFrom = $_POST['email'];
$state = $_POST['state'];
$country = $_POST['country'];
$message = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
//validation
if(empty($message)){
header("location: connect.html?nomessage");
}
$honeypot = FALSE;
if (!empty($_REQUEST['contact_me_by_fax_only']) && (bool) $_REQUEST['contact_me_by_fax_only'] == TRUE) {
$honeypot = TRUE;
log_spambot($_REQUEST);
# treat as spambot
} else {
# process as normal
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 0; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp.dotster.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'test#missionlighthouse.org'; // SMTP username
$mail->Password = '1234567'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('test#missionlighthouse.org', 'MLC Website');
$mail->addAddress('myemail80#myhost.net', 'John Doe'); // Add a recipient
//Body Content
$body = "<p><strong>Hello</strong>, you have received a message submitted from the MLC Website.<br><br>
<br><strong>Message Topic:</strong><br>" . $reason.
"<br><br><strong>Name:</strong><br>" . $name.
"<br><br><strong>Phone:</strong><br>" . $phone.
"<br><br><strong>Email:</strong><br>" . $email.
"<br><br><strong>State:</strong><br>" . $state.
"<br><br><strong>Country:</strong><br>" . $country.
"<br><br><strong>Message:</strong><br>". $message."</p>";
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Email from MLC Website';
$mail->Body = $body;
$mail->AltBody = strip_tags ($body);
$mail->send();
header("Location: thankyou.html?sent");
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
}
If you change the variable $mailFrom to $email, I believe your email field will show up as you expect. As for the state field, I'm not sure... Try changing both the locations of the variable $state to some other name such as $stateField
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 am trying to send mail from one to other using gmail smtp.
the mail sent successfully but without attachment. how can i solve this?
html form:
<form id="main-contact-form" class="contact-form" name="contact-form" method="post" action="sendresume.PHP" enctype="multipart/form-data">
<div class="row">
<div class="col-sm-5">
<div class="form-group">
<input type="text" name="name" class="form-control" required="required" placeholder="Name">
</div>
<div class="form-group">
<input type="text" name="mobile" class="form-control" required="required" placeholder="Mobile number">
</div>
<div class="form-group">
<input type="email" name="email" class="form-control" required="required" placeholder="Email address">
</div>
<p class="lead" style="margin-bottom: 10px;">Resume :</p>
<div class="form-group">
<input type="file" name="file" class="form-control" required="required" >
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-lg">Send Message</button>
</div>
</div>
<div class="col-sm-7">
<textarea name="messege" id="message" class="form-control" rows="8" placeholder="Note"></textarea>
</div>
</div>
</form>
and php code is:
<?php
include "classes/class.phpmailer.php"; // include the class name
$email = $_POST["email"];
$messege=$_POST['messege'];
$name=$_POST['name'];
$mobile=$_POST['mobile'];
$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->Username = "abc#gmail.com";
$mail->Password = "mypassword";
$mail->SetFrom("feedback#abc.com");
$mail->Subject = "this is subject";
$mail->Body = "this is body <br/><br/><br/><br/>
name:$name <br/><br/>
email:$email<br/><br/>
mobile:$mobile<br/><br/>
messege:$messege</b>";
$mail -> Addattachment('here is problem');
$mail->AddAddress("mymail#gmail.com");
if(!$mail->Send()){
echo "Mailer Error: " . $mail->ErrorInfo;
}
else{
echo "mail sent";
}
?>
this mail is sent successfully but I want it with attachment. file store in directory is not important. hope this problem solve immediately. thanks in advance because I never learned an used php before.
Here is an example to send attachment using PHPMailer class:
http://tournasdimitrios1.wordpress.com/2011/11/08/a-simple-example-sending-email-with-attachment-using-phpmailer/