Can't set the sender name, his email and subject in phpmailer - php

My phpmailer sends messages without sender name, sender email and subject of message.
I need receive message like this:
Name: sender name
Email: sender#gmail.com
Subject: Business
Message: text of message .................... ............. ............
Please help me to fix it to receive sender name, email and subject. Thank you.
Here is my php file:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Language" content="EN" />
<title> Contact Form</title>
</head>
<body>
<?php
//receive and save all informations
$name=$_POST['Name'];
$from=$_POST['E-mail'];
$subject=$_POST['Subject'];
$message=$_POST['Message'];
// reading PHPmailer
require("phpmailer/class.phpmailer.php");
include("phpmailer/class.smtp.php");
$mail = new phpmailer(true);
$mail->IsSMTP();
try {
$mail->IsSMTP();
$mail->CharSet="UTF-8";
$mail->SMTPSecure = 'tls';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->Username = 'eric21#gmail.com';
$mail->Password = 'ter3221';
$mail->SMTPAuth = true;
$mail->SetFrom("$from",'website');
$mail->AddAddress('eric#gmail.com','message has been sent. contact us');
$mail->Subject='Website message';
$mail->CharSet='UTF-8';
$mail->ContentType='text/html';
$mail->MsgHTML("$message");
if (!$mail->send()) {
echo 'your message was not sent. Tap "Try Again" to send the message.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo '<div class="alert alert-success container text-center" style="text-align:center" role="alert">
<big><b><br><br><br>Message has been sent!<br></big></b>
</div>
<div class="alert alert-success container text-center" style="text-align:center" role="alert">
<big><b><button>Back </big></b></button>
</div>';
}
} catch(phpmailerException $e) {
echo "<p class='message alert alert-danger'>" . $e->errorMessage() . "</p>";
} catch(Exception $e) {
echo "<p class='message alert alert-danger'>" . $e->getMessage() . "</p>";
}
?>
</body>
</html>
here is my form
<form class="form" id="form-main" method="Post" action="1.php">
<p class="name">
<input name="Name" type="text" class="validate[required,custom[onlyLetter],length[0,100]] feedback-input" placeholder="Name" id="name" />
</p>
<p class="e-mail">
<input name="E-mail" type="text" class="validate[required,custom[email]] feedback-input" id="e-mail" placeholder="E-mail" />
</p>
<p class="subject">
<input name="subject" type="text" class="validate[required,custom[onlyLetter],length[0,100]] feedback-input" placeholder="subject" id="subject" />
</p>
<p class="text">
<textarea name="message" class="validate[required,length[6,300]] feedback-input" id="message" placeholder="Message"></textarea>
</p>
<div class="submit">
<input type="submit" value="SEND" id="button-blue"/>
<div class="ease"></div>
</div>
</form>

You've based your code on an obsolete example and are probably using an old version - get the latest. If you want those submitted fields to appear in the message, you need to append them to $message, or to $mail->Body after calling msgHTML(). This will make you a plain-text body with that content in:
$mail->isHTML(false);
$mail->Body = "Name: $name
Email: $email
Subject: $subject
Message: $message";

Related

Why is my form no longer showing up?

I created a contact form in a PHP project and I have been trying to get the mail to send and in the process I have been altering some code such as removing my contact form. Now I re-added the form, but it does not display on the browser:
This is the actual file called contact.php:
<?php
define("TITLE", "Contact Us | MicroUrb");
include('includes/header.php');
/**
* PHPMailer Autoloader
*/
require '../vendor/phpmailer/phpmailer/PHPMailerAutoload.php';
/**
* Configure PHPMailer with your SMTP server settings
*/
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = 'myemail#example.com';
$mail->Password = 'secret';
$mail->SMTPSecure = 'tls';
/**
* Enable SMTP debug messages
*/
$mail->SMTPDebug = 2;
/**
* Send an email
*/
$mail->setFrom('sender#gmail.com');
$mail->addAddress('recipient#gmail.com');
$mail->Subject = 'An email sent from PHP';
$mail->Body = 'This is a test message';
if ($mail->send()) {
echo 'Message sent!';
} else {
echo 'Mailer error: ' . $mail->ErrorInfo;
}
?>
<div id="contact">
<hr>
<h1 class="contact">Get in touch with us!</h1>
<form method="post" action="" id="contact-form">
<label for="name">Your name</label>
<input type="text" id="name" name="name">
<label for="email">Your email</label>
<input type="email" id="email" name="email">
<label for="message">and your message</label>
<textarea id="message" name="message"></textarea>
<input type="checkbox" id="subscribe" name="name" value="Subscribe">
<label for="">Subscribe to newsletter</label>
<input type="submit" class="button next" name="contact_submit" value="Send Message">
</form>
<hr>
</div>
<?php include('includes/footer.php'); ?>

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 sending mail without form data [duplicate]

This question already has answers here:
method="post" enctype="text/plain" are not compatible?
(2 answers)
Closed 8 years ago.
I have a css div html form for a contact page and would like the form to be emailed when sent. Being a newbie at PHP ive tried different examples and can get the mail to be sent but cant get the form data with in the email.
html code
<div class="maincontent">
<div class="content">
<article class="topcontent">
<content>
<div id="form-main">
<div id="form-div">
<form method="post" enctype="text/plain" action="mail.php" class="form" id="form1">
<p class="name">
<input name="name" type="text" class="validate[required,custom[onlyLetter],length[0,100]] feedback-input" placeholder="Name" id="name" />
</p>
<p class="email">
<input name="email" type="text" class="validate[required,custom[email]] feedback-input" id="email" placeholder="Email" />
</p>
<p class="text">
<textarea name="text" class="validate[required,length[6,300]] feedback-input" id="comment" placeholder="Comment"></textarea>
</p>
<div class="submit">
<input type="submit" value="SEND" id="button-blue"/>
<div class="ease"></div>
</div>
</form>
</div>
</content>
</article>
</div>
</div>
PHP Mail Script
<?php
require("class.phpmailer.php");
$formcontent="From: $name \n Message: $text \n Email: $email";
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "localhost;"; // specify main and backup server
$mail->SMTPAuth = false; // turn on SMTP authentication
$mail->Username = "jswan"; // SMTP username
$mail->Password = "secret"; // SMTP password
$name = $_POST['name']; // contain name of person
$email = $_POST['email']; // Email address of sender
$message = $_POST['text']; // Your message
$mail->AddAddress('warren#.com', 'recipient email');
$mail->From = "noreply#.com";
$mail->FromName = "Contact Us Information";
$mail->Subject = "Website Contact Us Information";
$mail->Body = "Name:{$name}\n\nEmail:{email}\n\nComments:{$body}";
$mail->WordWrap = 50; // set word wrap to 50 characters
$mail->IsHTML(true); // set email format to HTML
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
?>
Like I said, mail is sent but without the form information.
Thanks
Take out enctype="text/plain" from the form tag.
Using this you can not get the posted data.

Categories