How to send auto-respond email with PHPMailer - php

I am trying to build an inquiry form and send the data to my email using PHPMailer. I am receiving the data which is submitted through the form but I am not able to send a confirmation to the customer who filled the form. So far this is my form:
<form class="myForm" action="" id="iForm" method="POST">
<input type="text" id="Name" placeholder="Name" required>
<input type="email" id="Email" placeholder="Email" required>
<input type="tel" id="Phone" placeholder="Phone Number" required>
<input type="text" id="Date" placeholder="Schedule a call" required>
<textarea id="Message" rows="5" placeholder="Your Message" required></textarea>
<div class="form-group">
<button type="submit" class="btn cbtn">SUBMIT</button>
</div>
</form>
Passing the data from the submitted form
$("#iForm").on('submit', function(e) {
e.preventDefault();
var data = {
name: $("#Name").val(),
email: $("#Email").val(),
phone: $("#Phone").val(),
date: $("#Date").val(),
message: $("#Message").val()
};
if ( isValidEmail(data['email']) && (data['name'].length > 1) && (data['date'].length > 1) && (data['message'].length > 1) && isValidPhoneNumber(data['phone']) ) {
$.ajax({
type: "POST",
url: "php/appointment.php",
data: data,
success: function() {
$('.success.df').delay(500).fadeIn(1000);
$('.failed.df').fadeOut(500);
}
});
} else {
$('.failed.df').delay(500).fadeIn(1000);
$('.success.df').fadeOut(500);
}
return false;
});
Checking for valid email address
function isValidEmail(emailAddress) {
var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(#((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(#\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
return pattern.test(emailAddress);
}
Checking for a valid phone number
function isValidPhoneNumber(phoneNumber) {
return phoneNumber.match(/[0-9-()+]{3,20}/);
}
This is the code which I am using from PHPMailer:
$_name = $_REQUEST['name'];
$_email = $_REQUEST['email'];
$_phone = $_REQUEST['phone'];
$_date = $_REQUEST['date'];
$_message = $_REQUEST['message'];
$mail = new PHPMailer(true);
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
$mail->SMTPDebug = 0;
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->Username = "office#myhost.co.uk";
$mail->Password = "********";
$mail->Host = "myhost.co.uk";
$mail->setFrom('office#myhost.co.uk', 'Alex');
$mail->addAddress('me#gmail.com', 'Alex');
$mail->isHTML(true);
$mail->Subject = 'New inquiry from CC';
$mail->Body = <<<EOD
<strong>Name:</strong> $_name <br>
<strong>Email:</strong> $_email <br> <br>
<strong>Phone:</strong> $_phone <br>
<strong>Booking Date:</strong> $_date <br>
<strong>Message:</strong> $_message <br>
I've tried using this, making another instance of PHPMailer and Send the email to the customer with the email they provided.
if($mail->Send()) {
$autoRespond = new PHPMailer();
$autoRespond->setFrom('office#myhost.co.uk', 'Alex');
$autoRespond->AddAddress($_email);
$autoRespond->Subject = "Autorepsonse: We received your submission";
$autoRespond->Body = "We received your submission. We will contact you";
$autoRespond->Send();
}
I've tried a few online "solutions" to this with no success. Any ideas ?

I finally found the solution of my issue. Basically everything above is correct the only thing which I missed was pass the SMTP parameters again to the new instance of PHPMailer. So now the last piece of code which caused the issue looks like that:
if($mail->Send()) {
$autoRespond = new PHPMailer();
$autoRespond->IsSMTP();
$autoRespond->CharSet = 'UTF-8';
$autoRespond->SMTPDebug = 0;
$autoRespond->SMTPAuth = TRUE;
$autoRespond->SMTPSecure = "tls";
$autoRespond->Port = 587;
$autoRespond->Username = "office#myhost.co.uk";
$autoRespond->Password = "********";
$autoRespond->Host = "myhost.co.uk";
$autoRespond->setFrom('office#myhost.co.uk', 'Alex');
$autoRespond->addAddress($_email);
$autoRespond->Subject = "Autorepsonse: We received your submission";
$autoRespond->Body = "We received your submission. We will contact you";
$autoRespond->Send();
}
Thank you for the help everyone.

Related

PHPmailer fuction only will receive from one email

I have searched for this answer for the past few days and have tried everything to make this work so please help.
I had made a contact form but I will only receive an email if I use the email that the form is submitting to. If I use any other email it will still said the email is sent but when I check my inbox there is nothing there. Same as my junk and sent folders. Although if I complete the form with my email I will get an email in my inbox. (So the form email and the email address submitting to is the same).
This is my index.php
<!DOCTYPE html>
<html>
<head>
<title>Send an Email</title>
</head>
<body>
<center>
<h4 class="sent-notification"></h4>
<form id="myForm">
<h2>Send an Email</h2>
<label>Name</label>
<input id="name" type="text" placeholder="Enter Name">
<br><br>
<label>Email</label>
<input id="email" type="text" placeholder="Enter Email">
<br><br>
<label>Subject</label>
<input id="subject" type="text" placeholder=" Enter Subject">
<br><br>
<p>Message</p>
<textarea id="body" rows="5" placeholder="Type Message"></textarea>
<br><br>
<button type="button" onclick="sendEmail()" value="Send An Email">Submit</button>
</form>
</center>
<script src="http://code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
function sendEmail() {
var name = $("#name");
var email = $("#email");
var subject = $("#subject");
var body = $("#body");
if (isNotEmpty(name) && isNotEmpty(email) && isNotEmpty(subject) && isNotEmpty(body)) {
$.ajax({
url: 'sendEmail.php',
method: 'POST',
dataType: 'json',
data: {
name: name.val(),
email: email.val(),
subject: subject.val(),
body: body.val()
}, success: function (response) {
$('#myForm')[0].reset();
$('.sent-notification').text("Message Sent Successfully.");
}
});
}
}
This is my sendEmail.php
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
if(isset($_POST['name']) && isset($_POST['email'])){
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$body = $_POST['body'];
require 'vendor/autoload.php';
$mail = new PHPMailer();
//Server settings
$mail->isSMTP(); //Send using SMTP
$mail->Host = 'smtp.mail.yahoo.com'; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = 'atouchof.beauty#yahoo.com'; //SMTP username
$mail->Password = 'aaa'; //SMTP password
$mail->SMTPSecure = 'tsl'; //Enable implicit TLS encryption
$mail->Port = 587; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
$mail->isHTML(true); //Set email format to HTML
$mail->setFrom($email, $name);
$mail->addAddress("atouchof.beauty#yahoo.com");
$mail->Subject = ("$email ($subject)");
$mail->Body = $body;
if($mail->send()){
$status = "success";
$response = "Email is sent!";
}
else
{
$status = "failed";
$response = "Something is wrong: <br>" . $mail->ErrorInfo;
}
exit(json_encode(array("status" => $status, "response" => $response)));
}
?>
This may sound confusing so please ask any questions if you don't understand.
Two problems. Firstly you're forging the from address, which is very probably the cause of your problems. This is well documented, and I recommend following the advice given in the PHPMailer contact form example.
A minor issue is that you're setting this to a non-existent value:
$mail->SMTPSecure = 'tsl';
That should be:
$mail->SMTPSecure = 'tls';
You can call multiple times:
$mail->AddAddress('emailONE#gmail.com', 'Juan');
$mail->AddAddress('emailTWO#gmail.com', 'Pepe');
I recommend use https://mailtrap.io/ for testing emails

How to send an email to multiple addresses?

How can I send an email to multiple email addresses? Right now this index.php page is being hosted online, and it connects to a send.php page, and I'd like to add another email text field that would be used to also send an email to the address entered in it.
Currently it sends an email to one email address that is entered in the email field in the form.
Index.php
<form id="contactus" method="post" action="send.php">
<input type="name" name="name" class="form-control" placeholder="Name" required>. <br/><br/>
<input type="email" name="email" class="form-control" placeholder="Email" required>. <br/><br/>
<input type="name" name="name2" class="form-control" placeholder="Player 2's Email Address" required><br/><br/>
<textarea name="message" class="form-control" placeholder="What Is Your Question?" required></textarea><br/><br/>
<input type="hidden" name="phone2">
<button type="submit" class="btn btn-success">Send Email</button>
<br><br>
</form>
Send.php
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING); //sanitize data
$email = filter_var($_POST['email'], FILTER_SANITIZE_STRING);
$message = filter_var($_POST['message'], FILTER_SANITIZE_STRING);
/*added line of code*/
if(empty($name)){
header("location: index.php?nouser");
exit();
}
if(empty($email)){
header("location: index.php?noemail");
exit();
}
if(empty($message)){
header("location: index.php?noemail");
exit();
}
/*if(empty($message)){
header("location: index.php?nomessage");
exit();
}//end validation*/
//added line; 'honeypot'
if(empty($_POST['phone2'])){
//Information that needs to be filled out to be able to send an email through phpmailer
//Will use an SMTP service. SMTP is basically like a mail server to send mail
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 1;
//$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = false;
$mail->Host = 'relay-hosting.secureserver.net';
$mail->Port = "25";
$mail->Username = "#Email_Placeholder";
$mail->Password = "Password";
$mail->SetFrom("#Email_Placeholder");
$mail->setFrom('#Email_Placeholder', 'Greeting');
// Add a recipient : used to also send to name
$mail->addAddress($email);
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('email#gmail.com');
/*$mail->addBCC('bcc#example.com');*/
//Body content
$body = "<p><strong>Hello, </strong>" . $email . " How are you?
<br>
<b> Please Notice: </b><br>
<br> <br> Your message was Delivered: " . $message . ". Hello" . $name2 .
"</p>";
$mail->isHTML(true);
//subject line of email
$mail->Subject = $name;
//for html email
$mail->Body = $body;
$mail->AltBody = strip_tags($body);
//the email gets sent
header("Location: http://www.test-domain.com");
$mail->send();
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
}
?>
This is pretty simple - if you follow the code, you just need to duplicate some lines.
Duplicate your form field
<input type="email" name="email" class="form-control" placeholder="Email" required>
<input type="email" name="email_2" class="form-control" placeholder="Email" required>
Duplicate your validation
$email = filter_var($_POST['email'], FILTER_SANITIZE_STRING);
$email_2 = filter_var($_POST['email_2'], FILTER_SANITIZE_STRING);
Duplicate the 'Add Address'
$mail->addAddress($email);
$mail->addAddress($email_2);
Fore readability sake in the code use an array and implode it to a comma separated string:-
$recipients = array(
"youremailaddress#yourdomain.com",
// more emails
);
$email_to = implode(',', $recipients); // your email address
$email_subject = "Contact Form Message"; // email subject line
$thankyou = "thankyou.htm"; // thank you page

How to get reCaptcha to work with ajax .load

I am looking to use reCaptcha with the jquery '.load' ajax function so that the information is passed across to my PHP contact form. I have established how to send across things such as name value, subject value etc with this method, however, I am unsure how to pass across the reCaptcha info.
As it currently stands when I submit the form I receive a PHP error advising 'Undefined index: g-recaptcha-response'. I believe this is to do with the Ajax side of things.
Any help on this would be amazing as I am at a total loss!
jQuery:
$("#contactForm").submit(function(event) {
event.preventDefault();
var name = $("#name").val();
var email = $("#email").val();
var subject = $("#subject").val();
var phone = $("#phone").val();
var company = $("#company").val();
var message = $("#message").val();
var submit = $("#submit").val();
$(".form-message").load("contactForm.php", {
name: name,
email: email,
subject: subject,
phone: phone,
company: company,
message: message,
submit: submit
});
PHP:
if(isset($_POST['submit'])) {
require 'dist/PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$phone = $_POST['phone'];
$company = $_POST['company'];
$message = $_POST['message'];
$secretKey = "--KEY--";
$responseKey = $_POST['g-recaptcha-response'];
$userIP = $_SERVER['REMOTE_ADDR'];
$url = "https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$responseKey&remoteip=$userIP";
$response = file_get_contents($url);
$mail->HOST = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Username = '--username--';
$mail->Password = '--password--';
$mail->setFrom('--email--', 'Contact Form Submission');
$mail->addAddress('--email--');
$mail->addReplyTo($email, $name);
$mail->isHTML(true);
$mail->Subject= $subject;
$mail->Body='<p>Name: '.$name. '<br>Email: '.$email.'<br>Subject: '.$subject.'<br>Phone: '.$phone.'<br>Company: '.$company.'<br>Message: '.$message.'</p>';
HTML:
<form method="POST" action="contactForm.php" id="contactForm">
<div class="form-group">
<input
type="text"
id="name"
name="name"
class="form-control"
placeholder="Full Name"
/>
</div>
<div class="form-group">
<input
type="text"
id="email"
name="email"
class="form-control"
placeholder="Email Address"
/>
</div>
<div class="form-group">
<input
type="text"
id="subject"
name="subject"
class="form-control"
placeholder="Subject"
/>
</div>
<div class="form-group">
<input
id="phone"
type="text"
name="phone"
class="form-control"
placeholder="Phone (optional)"
/>
</div>
<div class="form-group">
<input
id="company"
type="text"
name="company"
class="form-control"
placeholder="Company (optional)"
/>
</div>
<div class="form-group">
<textarea
class="form-control"
id="message"
name="message"
placeholder="Message"
style="height: auto"
rows="5"
></textarea>
</div>
<div
class="g-recaptcha"
data-sitekey="--KEY--"
></div>
<input
id="submit"
type="submit"
value="Submit"
class="btn btn-outline-primary btn-block mb-3"
name="submit"
/>
</form>
You can use the grecaptcha.getResponse() method to get the value of the captcha from the client side, then send that value with your ajax/jquery
<script type="text/javascript">
("#contactForm").submit(function(event) {
event.preventDefault();
var name = $("#name").val();
var email = $("#email").val();
var subject = $("#subject").val();
var phone = $("#phone").val();
var company = $("#company").val();
var message = $("#message").val();
var submit = $("#submit").val();
var captcha = grecaptcha.getResponse(); //get captcha
$(".form-message").load("contactForm.php", {
name: name,
email: email,
subject: subject,
phone: phone,
company: company,
message: message,
submit: submit,
captcha : captcha
});
</script>
Then your php
<?php
if(isset($_POST['submit'])) {
require 'dist/PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$phone = $_POST['phone'];
$company = $_POST['company'];
$message = $_POST['message'];
$secretKey = "--KEY--";
$responseKey = $_POST['captcha']; //captcha
$userIP = $_SERVER['REMOTE_ADDR'];
$url = "https://www.google.com/recaptcha/api/siteverify?secret=$secretKey&response=$responseKey&remoteip=$userIP";
$response = file_get_contents($url);
$mail->HOST = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Username = '--username--';
$mail->Password = '--password--';
$mail->setFrom('--email--', 'Contact Form Submission');
$mail->addAddress('--email--');
$mail->addReplyTo($email, $name);
$mail->isHTML(true);
$mail->Subject= $subject;
$mail->Body='<p>Name: '.$name. '<br>Email: '.$email.'<br>Subject: '.$subject.'<br>Phone: '.$phone.'<br>Company: '.$company.'<br>Message: '.$message.'</p>';

Jquery Ajax, PHPMailer doesn't send emails even if there's no error but works if i use stand alone page with fix values

I have this simple bootstrap form that i run on my localhost, it accepts name email and message and a custom captcha in a form of a math question; On submit i used jquery ajax to fetch the data to emailme.php that contains the phpmailer code.. My problem is, no email was sent even if there's no error and the network status is 200 and i can also see this:
http://localhost/emailme.php?name=myname&email=myemail#gmail.com&message=test
however it works if i just use the phpmailer stand alone code with fixed values. how can i make it work?
HTML:
<form id="emailme" method="post">
<div class="form-group">
<input type="text" class="form-control" name="name" id="name" placeholder="e.g. John Doe">
<span id="err_name" class="text-danger"></span>
</div>
<div class="form-group">
<input type="text" class="form-control" name="email" id="email" placeholder="e.g. example#domain.com">
<span id="err_email" class="text-danger"></span>
</div>
<div class="form-group">
<textarea name="message" id="message" cols="30" rows="10" class="form-control" placeholder="Your message"></textarea>
<span id="err_message" class="text-danger"></span>
</div>
<div class="form-group">
<label for="human">5 + 2 = ?</label>
<input type="text" id="human" name="human" class="form-control" placeholder="Your Answer">
<span id="err_human" class="text-danger"></span>
</div>
<input type="submit" class="btn btn-primary btn-sm" value="Send">
</form>
JQUERY:
$("#emailme").on('submit',function(e){
e.preventDefault();
var name = $("#name").val();
var email = $("#email").val();
var message = $("#message").val();
var human = $("#human").val();
var emailRegex = /(.+)#(.+)\.(com|edu|org|etc)$/g;
if(name!==''&&email!==''&&message!==''&&human!==''){
if(email.match(emailRegex)){
if(human==7){
$.ajax({
url:'emailme.php',
typ:'post',
data:{name:name,email:email,message:message},
success:function(response){
console.log(response);
$("#mail-status").html(response);
$("#emailme")[0].reset();
},
error:function(XMLHttpRequest,textStatus,errorThrown){
console.log(textStatus+errorThrown);
}
});
}
else{
$("#mail-status").html("unable to submit form, you're not human");
}
}
else{
$("#err_email").append("Invalid email format");
}
}
else if(name==''){
$("#err_name").append("Name cannot be empty");
}
else if(email==''){
$("#err_email").append("Email cannot be empty");
}
else if(message==''){
$("#err_message").append("Message cannot be empty");
}
else if(human==''){
$("#err_human").append("Please answer the security question");
}
});
PHP:
<?php
date_default_timezone_set('Etc/UTC');
require 'phpmailer/PHPMailerAutoload.php';
// Set the email address submissions will be sent to
$email_address = 'myemail#gmail.com';
// Set the subject line for email messages
$subject = 'Test email from localhost using PHPMailer';
// Check for form submission
if(isset($_POST['name'], $_POST['email'], $_POST['message'])){
//Create a new PHPMailer instance
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "myemail#gmail.com";
$mail->Password = "mypassword";
$mail->setFrom($_POST['email'], $_POST['name']);
$mail->addAddress($email_address, 'myname');
$mail->Subject = $subject;
$mail->Body = 'From:'.$_POST['name'].'(' . $_POST['email'] . ')'.'<p><b>Message</b><br/>' . $_POST['message'] . '</p>';
$mail->AltBody = 'This is a plain-text message body';
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
}
?>

Mail send function not getting called from .php Customer Form

I am new to PHP coding. I have created a Customer form, followed by send e-mail function. When a Customer submits data (including E-Mail and password) he should receive a confirmation e-mail with User ID (same as entered E-Mail Id) and entered Password.
At this moment, with my current code, when a Customer submits the form, database gets updated, but no e-mail is send to the user. Can anyone point out what needs to be changed to ensure that send e-mail function works correctly.
<!DOCTYPE html>
<html>
<head>
<title>Registration Desk</title>
</head>
<body>
<div class="form">
<form id="contactform" action="reg_submit.php" method="post">
<p class="contact"><label for="name">Name</label></p>
<input id="name" name="name" placeholder="First and last name" required="" tabindex="1" type="text">
<p class="contact"><label for="add">Address</label></p>
<textarea id="add" name="add" style="width:85%; height:60%; margin-top:1%" required=""></textarea>
<fieldset>
<label>Birthday</label>
<input type="date" name="dob" value="yyyy-mm-dd" required="">
</fieldset>
<label>I am</label> <br><br>
<input type="radio" name="sex" value="male" >Male
<input type="radio" name="sex" value="female">Female
<p class="contact"><label for="email">Email</label></p>
<input id="email" name="email" class="field" placeholder="Please enter a valid emailid" required="" type="email">
<p class="contact"><label for="password">Create a password</label></p>
<input type="password" id="password" name="password" required="">
<p class="contact"><label for="repassword">Confirm your password</label></p>
<input type="password" id="repassword" name="repassword" required="">
<br><br>
<p class="contact"><label for="phone">Mobile phone</label></p>
<input id="phone" name="phone" placeholder="phone number" required="" type="text"> <br>
<input class="buttom" name="submit" id="submit" tabindex="5" value="Sign me up!" type="submit">
</form>
</div>
</div>
</body>
</html>
<?php
error_reporting( 0);
$connect=mysqli_connect("localhost","wbtecqoj_saltee","webuildtec1","wbtecqoj_salteegroup");
if(mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if( $_POST["password"]!= $_POST["repassword"])
{
?>
<script type="text/javascript">
alert("password miss matched!!!.");
history.back();
</script>
<?php
}
if($_POST["name"] && $_POST["add"] && $_POST["phone"] && $_POST["email"] && $_POST["password"] && $_POST["dob"] )
{
$insert="INSERT INTO `db`.`new_user` (`id`, `name`, `add`, `contact`, `email`, `pass`, `dob`, `gender`,`code`) VALUES (NULL, '$_POST[name]', '$_POST[add]', '$_POST[phone]', '$_POST[email]', '$_POST[password]', '$_POST[dob]', '$_POST[sex]', 'SAL1000000')";
if(!mysqli_query($connect,$insert))
{
echo die('Error: ' . mysqli_error($connect));
}
else
{
$to=$_POST['email'];
$from= "admin#wbtec.in";
$subject="Welcome to xyz Group";
$message="Welcome " . $_POST['name'] . "Dear Customer,
Your Card Number is " .$Ccode.$cookieId. " User id -" . $_POST['email'] . "Your Password -" . $_POST['password'].
"Thanking You,
Customer Care,";
$header = "From" .$from;
if(isset($_POST['btnSend']))
{
$res = mail($to,$subject,$message,$header);
if($res)
{
echo 'Message send';
}
else
{
echo 'msg not send';
}
}
}}
mysqli_close($connect);
?>
Remove the
if(isset($_POST['btnSend']))
{
}
You are checking for
if(isset($_POST['btnSend']))
But your form doesn't contain an input named btnSend, you should be able to remove that if statement altogether
I suggest you to use PHPMailer from
http://sourceforge.net/projects/phpmailer/
With the PHPMailer class you can specify the outgoing SMTP server and authenticate with it properly. Here is an example how I used it:
<?php
require("class.phpmailer.php");
class Mailer
{
function send($email, $subject, $body, $attachment1)
{
$mail = new PHPMailer();
$mail->IsSMTP(); // send via SMTP
$mail->Host = "mail.xyz.com"; // your SMTP servers
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "authname"; // SMTP username
$mail->Password = "authpassword"; // SMTP password
$mail->From = "info#xyz.com";
$mail->FromName = "info#xyz.com";
$mail->AddAddress($email);
$mail->WordWrap = 50; // set word wrap
if ($attachment1 != "") {
$mail->AddAttachment($attachment1); // attachment1
}
$mail->IsHTML(false); // send as HTML
$mail->Subject = $subject;
$mail->Body = $body;
if(!$mail->Send())
{
return -1;
}
else
{
return 0;
}
}
}
?>
Hope that helps!
u have to download php mailer library which have to files
PHP mailer
class.phpmailer.php and other class.smtp.php then add this file at same directory which have php code which is shown below..
<?PHP
require_once('class.phpmailer.php');
if('POST' == $_SERVER['REQUEST_METHOD']) {
// process the form here
$subject = "This is subject testing1";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->IsHTML(true); // send as HTML
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port
//$mail->Username = "gmailusername"; // GMAIL username
//$mail->Password = "password"; // GMAIL password
$mail->From = 'email id';
$mail->FromName = "FrontName";
$mail->Subject = $subject;
$mail->Body = "this is html body"; //HTML Body
$emailId='email id';
$emails = explode(",",$emailId);
foreach($emails as $email)
$mail->AddAddress($email);
if($mail->Send()){
echo "Message sent successfully";
}else{
echo "Mailer Error: " . $mail->ErrorInfo;
}
}
?>
<form action="" method="post">
<input type="submit" name='button1'value="sent mail" />
</form>

Categories