PHPMailer error: Undefined property: PHPMailer\PHPMailer\Exception::$getMessage - php

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

Related

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.

Mailer with php not working [duplicate]

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

send mail with attachment using phpmailer and html form

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/

PHPmailer setting From email the same as to email

I'm using PHP mailer to build a contact form and bear in mind I'm new to PHP. I have the form sending nicely, however, in the email that I receive the From address does not match the email that the user inputs into the contact form and is instead displaying the same email as the recipient. I'm 100% sure I'm entering the correct email into the live contact form. I have replaced my sensitive information with $$$$$$$$$
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = trim($_POST["name"]);
$email = trim($_POST["email"]);
$message = trim($_POST["message"]);
if ($name == "" OR $email == "" OR $message == "") {
$error_message = "You must specify a value for name, email address, and message.";
}
if (!isset($error_message)) {
foreach( $_POST as $value ){
if( stripos($value,'Content-Type:') !== FALSE ){
$error_message = "There was a problem with the information you entered.";
}
}
}
if (!isset($error_message) && $_POST["address"] != "") {
$error_message = "Your form submission has an error.";
}
require_once("../inc/phpmailer/PHPMailerAutoload.php");
$mail = new PHPMailer();
if (!isset($error_message) && !$mail->ValidateAddress($email)){
$error_message = "You must specify a valid email address.";
}
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->Username = "$$$$$$$$$$$$$";
$mail->Password = "$$$$$$$$$$$$$";
$mail->SMTPSecure = 'tls';
if (!isset($error_message)) {
$mail->From = $email;
$mail->FromName = $name;
$mail->addAddress('$$$$$$$$$$$$$$', '$$$$$$$$$$l');
$mail->isHTML(true);
$mail->Subject = '$$$$$$$$$$| ' . $name;
$mail->Body = $message;
$mail->AltBody = $message;
if($mail->Send()) {
header("Location: ../contact/?status=thanks");
exit;
} else {
$error_message = "There was a problem sending the email: " . $mail->ErrorInfo;
}
}
}
require_once("../inc/header.php"); ?>
<div class="section page">
<div class="wrapper">
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="center-block">
<h1>Contact</h1>
</div>
<?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
<div class="alert alert-success">
<p>Thanks for the email! I’ll be in touch shortly!</p>
</div>
<?php } else { ?>
<?php
if (!isset($error_message)) {
echo '<p>I’d love to hear from you! Complete the form to send me an email.</p>';
} else {
?><div class="alert alert-warning"><?php
echo '<p>' . $error_message . '</p>';
?></div><?php
}
?>
</div>
</div>
<form class="form-horizontal" method="post" role="form">
<div class="form-group">
<label for="name" class="col-sm-4 control-label">Name</label>
<div class="col-sm-4">
<input type="text" class="form-control" id="name" name="name" placeholder="Name" value="<?php if (isset($name)) { echo htmlspecialchars($name); } ?>">
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-4 control-label">Email</label>
<div class="col-sm-4">
<input type="email" class="form-control" id="email" name="email" placeholder="Email" value="<?php if(isset($email)) { echo htmlspecialchars($email); } ?>">
</div>
</div>
<div class="form-group">
<label for="message" class="col-sm-4 control-label">Message</label>
<div class="col-sm-4">
<textarea class="form-control" id="message" name="message" rows="3"><?php if (isset($message)) { echo htmlspecialchars($message); } ?></textarea>
</div>
</div>
<div class="form-group" style="display:none;">
<label for="address" class="col-sm-4 control-label">Address</label>
<div class="col-sm-4">
<input type="text" class="form-control" id="address" name="address" placeholder="Name">
<p>Humans: please leave this field blank.</p>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-4 col-sm-">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
<?php } ?>
</div>
</div>
<?php require_once("../inc/footer.php"); ?>
change this
$mail->From = $email;
$mail->FromName = $name;
$mail->addAddress('$$$$$$$$$$$$$$', '$$$$$$$$$$l');
to
$mail->setFrom($email, $name);
$mail->addAddress($EMAIL THAT ITS GOING TO, $NAME THATS ITS GOING TO);
Also see this link on sending FROM other address with GMAIL
https://support.google.com/mail/answer/22370?hl=en
It looks like you forgot to call the Sender variable:
public $Sender = '';
/**
* The Return-Path of the message.
* If empty, it will be set to either From or Sender.
* #type string
*/
https://github.com/PHPMailer/PHPMailer/blob/master/class.phpmailer.php
Thanks for your replies guys. I did a bit of external research and it turns out that it is an issue with gmail itself. They don't allow you to use their smtp service with a from address different to your gmail account. I think it's because of spam.

Categories