How to send auto generated mail to user based on radio button selection

Hello,

I am new to php. I have created an html form where the user selects any one option from the radio button. Every option has a price that user will be paying if he orders. What I want is to send that price to the user as auto generated mail when the user submits the form. My html is as follows:

<input type="radio" name="grade"  class="form-control" required><img src="images/grade_2.png" alt="no image">&nbsp;
<input type="radio" name="grade"  class="form-control" required><img src="images/grade_3.png" alt="no image">&nbsp;
<input type="radio" name="grade"  class="form-control" required><img src="images/grade_4.png" alt="no image">&nbsp;
<input type="radio" name="grade"  class="form-control" required><img src="images/grade_5.png" alt="no image">&nbsp;
<input type="radio" name="grade"  class="form-control" required><img src="images/grade_6.png" alt="no image">&nbsp;
<input type="radio" name="grade"  class="form-control" required><img src="images/grade_7.png" alt="no image">

For example, if the user selects the third option and the price is 5000, then the user gets the mail that "The cost for the option you have selected is 5000."

I am not been able to write the php code for this. Please help me. Also I want to send these images to the receiver when the user submits the form. How can I do this?

Thanks.

Regards
Saxena
S SaxenaWitageAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Leonidas DosasCommented:
HTML-JQUERY code:
<!DOCTYPE html>
<html>
    <head>
        <title>Expert Exchange</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    </head>
    <body>
        <form action="" method="post">
<input type="radio" name="grade"  data-value="5000"  class="form-control" required><img src="images/grade_2.png" alt="no image">&nbsp;
<input type="radio" name="grade"  data-value="6000"  class="form-control" required><img src="images/grade_3.png" alt="no image">&nbsp;
<input type="radio" name="grade"  data-value="7000"  class="form-control" required><img src="images/grade_4.png" alt="no image">&nbsp;
<input type="radio" name="grade"  data-value="8000"  class="form-control" required><img src="images/grade_5.png" alt="no image">&nbsp;
<input type="radio" name="grade"  data-value="9000"  class="form-control" required><img src="images/grade_6.png" alt="no image">&nbsp;
<input type="radio" name="grade"  data-value="10000"  class="form-control" required><img src="images/grade_7.png" alt="no image">

</form> 
<input type="button" id="target" value="Go">
<script> 
$(document).ready(function (){
  var radioValue;

    $( "#target" ).click(function( event ) {
     radioValue=$('input[name="grade"]:checked').data('value');    
     $.ajax({
  method: "POST",
  url: "emailPhp.php",
  data: { value: radioValue},
  dataType: 'json'
})
  .done(function( msg ) {
    console.log(msg);
  });
     event.preventDefault();
});  
});

</script>
    </body>
</html>

Open in new window


PHP code (emailPhp.php):
<?php

if(isset($_POST['value'])){
    $a=array();
    array_push($a, $_POST['value']);
    echo json_encode($a);
    $to = "somebody@example.com";
$subject = "My subject";
$txt = $a;
$headers = "From: webmaster@example.com" . "\r\n" .
"CC: somebodyelse@example.com";

mail($to,$subject,$txt,$headers);
}
?>

Open in new window

0
S SaxenaWitageAuthor Commented:
Thanks Leonidas Dosas,

Give me a little time. I will just try this code and will let you know.

Regards
Saxena
0
Julian HansenCommented:
Firstly, your radio buttons have no values - no matter what you click you are going to get a blank result on the server.
Give your input's value attributes equal to the number of the grade for instance

Just have one email with a field for price that you fill when you create the email.

Here is a simple example - I would however recommend using PHPMailer to handle your emails.
HTML
  <form action="t2790.php" method="post">
    <input type="radio" name="grade" value="2" class="form-control" required><img src="images/grade_2.png" alt="2">&nbsp;
    <input type="radio" name="grade" value="3" class="form-control" required><img src="images/grade_3.png" alt="3">&nbsp;
    <input type="radio" name="grade" value="4" class="form-control" required><img src="images/grade_4.png" alt="4">&nbsp;
    <input type="radio" name="grade" value="5" class="form-control" required><img src="images/grade_5.png" alt="5">&nbsp;
    <input type="radio" name="grade" value="6" class="form-control" required><img src="images/grade_6.png" alt="6">&nbsp;
    <input type="radio" name="grade" value="7" class="form-control" required><img src="images/grade_7.png" alt="7">
    <button type="submit" class="btn btn-primary">Submit</button>
  </form>

Open in new window

PHP script
<?php
$to="email@ofrecipient.com";
$subject="Subject of the email";

// Get the posted value
$grade = isset($_POST['grade']) ? $_POST['grade'] : false;

// don't continue if no valid input obtained
if (!$grade || $grade < 1 || $grade > 7) die();

// Prices by grade
// Starts from grade 2 - no grade 0 or 1 so zero values
$prices = [0,0,200,300,400,500,600,700];

// Read template in and merge with above data
ob_start();
require_once('t2790.email.php');
$body = ob_get_clean();

// Send the email
if (!mail($to, $subject, $body)) {
   // handle error here
   header('location: fail.html');
}

// redirect to next page here
header('location: success.html');

Open in new window


I have put a working sample here. This does not actually send an email for obvious reason but it does show the code that would send the email.

NB: Take a look at PHPMailer - to create more robust emails.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

S SaxenaWitageAuthor Commented:
Thanks Julian,

I am already using phpmailer. I am attaching my html for your reference. I will try your solution and will come back again.

Following is my php.

Thanks
Saxena

<?php
/**
* Simple example script using PHPMailer with exceptions enabled
* @package phpmailer
* @version $Id$
*/

require 'class.phpmailer.php';

try {
$mail = new PHPMailer(true); //New instance, with exceptions enabled

$to = "email@somewhere.com";
$mail->AddAddress($to);
$mail->From = $_POST['email'];
$mail->FromName = $_POST['name'];
$mail->Subject = "Contact Form";

$body = "<table>
<tr>
<th colspan='2'>Contact Form</th>
</tr>

<tr>
<td style='font-weight:bold'>Full Name:</td>
<td>".$_POST['name']."</td>
</tr>

<tr>
<td style='font-weight:bold'>E-Mail:</td>
<td>".$_POST['email']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Mobile:</td>
<td>".$_POST['mobile_number']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Age:</td>
<td>".$_POST['age']."</td>
</tr>

<tr>
<td style='font-weight:bold'>City: </td>
<td>".$_POST['city']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Select Method: </td>
<td>".$_POST['method']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Grade of Baldness: </td>
<td>".$_POST['grade']."</td>
</tr>

<table>";

$body = preg_replace('/\\\\/','', $body); //Strip backslashes
$mail->MsgHTML($body);

$mail->IsSMTP(); // tell the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 25; // set the SMTP server port
//$mail->Host = "email@somewhere.com"; // SMTP server
//$mail->Username = "name@domain.com"; // SMTP server username
//$mail->Password = "password"; // SMTP server password

$mail->IsSendmail(); // tell the class to use Sendmail
$mail->AddReplyTo("email@somewhere.com");
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap = 80; // set word wrap
  

$mail->IsHTML(true); // send as HTML
$mail->Send();
echo 'Thank You. Your form has been submitted. Please check your mail for details.';
} catch (phpmailerException $e) {
echo $e->errorMessage();
}


?>

Open in new window

0
S SaxenaWitageAuthor Commented:
Hello Julian,

As I said I am new to php so  If you look at my php, my form comes to me in a tabular form. if I add one more $to=$_POST['email'];, will the form go to the user also?
I do not want to send the form to user. I want to send only the price message to his mail.

Regards
Saxena
0
Julian HansenCommented:
if I add one more $to=$_POST['email'];, will the form go to the user also?
No - that is the body of the message - you need to use the PHPMailer AddAddress (as you have on line 14) for each recipient you want the email to go to.

Note: it is not a good idea to use $_POST values directly for a number of reasons
1. They may not exist causing your script to terminate unexpectedly
2. They may not contain the data you expect

It is a good idea to extract the $_POST variables first - check they exist AND they are what you expect before using them. If you want to know more do a search on "sanitizing post variables php"
0
Julian HansenCommented:
I also added code tags to your post. To add code tags highlight your code snippet and click the CODE Button in the toolbar. Code tags make your code easier to read and refer to.
0
S SaxenaWitageAuthor Commented:
Hello Julian,

Actually I want to send this mail to any user who fills the form. That is the reason, I am using $to=$_POST['email'];
I will not be able to know the mail ids of the users. I will be grateful if you can add your code to my php that I have already posted. I am really sorry for this kiddish question but as I said I am totally new to php.

Regards
Saxena
0
Julian HansenCommented:
First up
I am really sorry for this kiddish question
Understand that every one of us went through this stage and that is why we (at EE) are here - to pass on the knowledge we have gained as others passed it on to us. So feel free to ask whatever questions you need.

Second to your question - not sure what you want me to change but going to guess
$to = "email@somewher.com";
$mail->AddAddress($to);
$mail->From = $_POST['email'];

Open in new window

The above is sending the email to you from the user. I imagine you would want this
$from = "email@somewhere.com";
$to = isset($_POST['email']) ? $_POST['email'] : false;
// We would normally do a check here to see a) the address was provided and b) it is valid
// and if either of these fails abort the process. I will leave this to you to fill in as required

// You should send a copy to yourself so you know what was sent
$mail->AddAddress($from);

// Add the recipient address
$mail->AddAddress($to);

$mail->From = $from;

Open in new window


That's the way I would do it.
0
S SaxenaWitageAuthor Commented:
The code that I have written is with the help of experts like you. My php is only for sending the mail to me from the user. In my code $to is me. When I want to send the auto mail to user with the radio button value, $to will be the user. I have tried to add the codes you have given to my php.  Do you mean that my php should be as follows:

<?php
/**
* Simple example script using PHPMailer with exceptions enabled
* @package phpmailer
* @version $Id$
*/

require 'class.phpmailer.php';

try {
$mail = new PHPMailer(true); //New instance, with exceptions enabled

$to = "email@somewhere.com";
$mail->AddAddress($to);
$mail->From = $_POST['email'];
$mail->FromName = $_POST['name'];
$mail->Subject = "Contact Form";

$from = "email@somewhere.com";
$to = isset($_POST['email']) ? $_POST['email'] : false;
// We would normally do a check here to see a) the address was provided and b) it is valid
// and if either of these fails abort the process. I will leave this to you to fill in as required

// You should send a copy to yourself so you know what was sent
$mail->AddAddress($from);

// Add the recipient address
$mail->AddAddress($to);

$mail->From = $from;
 
$grade = isset($_POST['grade']) ? $_POST['grade'] : false;
// don't continue if no valid input obtained
if (!$grade || $grade < 1 || $grade > 7) die();
// Prices by grade
// Starts from grade 2 - no grade 0 or 1 so zero values
$prices = [0,0,200,300,400,500,600,700];
ob_start();
require_once('email_handler.php');
$body = ob_get_clean();
?>
The message that would be sent is as follows
<div style="color: red; border: 1px solid red; padding: 15px">
<em>
<?php echo $body;?>
</em>
</div>

$body = "<table>
<tr>
<th colspan='2'>Contact Form</th>
</tr>

<tr>
<td style='font-weight:bold'>Full Name:</td>
<td>".$_POST['name']."</td>
</tr>

<tr>
<td style='font-weight:bold'>E-Mail:</td>
<td>".$_POST['email']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Mobile:</td>
<td>".$_POST['mobile_number']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Age:</td>
<td>".$_POST['age']."</td>
</tr>

<tr>
<td style='font-weight:bold'>City: </td>
<td>".$_POST['city']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Select Method: </td>
<td>".$_POST['method']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Grade of Baldness: </td>
<td>".$_POST['grade']."</td>
</tr>

<table>";

$body = preg_replace('/\\\\/','', $body); //Strip backslashes
$mail->MsgHTML($body);

$mail->IsSMTP(); // tell the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 25; // set the SMTP server port
//$mail->Host = "email@somewehre.com"; // SMTP server
//$mail->Username = "name@domain.com"; // SMTP server username
//$mail->Password = "password"; // SMTP server password

$mail->IsSendmail(); // tell the class to use Sendmail
$mail->AddReplyTo("email@somewhere.com");
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap = 80; // set word wrap
  

$mail->IsHTML(true); // send as HTML
$mail->Send();
echo 'Thank You. Your form has been submitted. Please check your mail for details.';
} catch (phpmailerException $e) {
echo $e->errorMessage();
}


?>

Open in new window

0
Julian HansenCommented:
No, I was implying you should replace your code with the code I posted.

I am a bit confused because in your one post you said
Actually I want to send this mail to any user who fills the form
And now you are saying
My php is only for sending the mail to me from the user. In my code $to is me
The code I posted was to change your email from sending to you (from the user) to sending to the user (from you)

In other words you would replace

$to = "email@somewher.com";
$mail->AddAddress($to);
$mail->From = $_POST['email'];

Open in new window


To the code I posted
<?php
/**
* Simple example script using PHPMailer with exceptions enabled
* @package phpmailer
* @version $Id$
*/

require 'class.phpmailer.php';

try {
$mail = new PHPMailer(true); //New instance, with exceptions enabled

// REMOVE THIS
//$to = "email@somewhere.com";
//$mail->AddAddress($to);
//$mail->From = $_POST['email'];
//$mail->FromName = $_POST['name'];
// TO HERE

$mail->Subject = "Contact Form";

// ADD THIS
$from = "email@somewhere.com";
$to = isset($_POST['email']) ? $_POST['email'] : false;
// We would normally do a check here to see a) the address was provided and b) it is valid
// and if either of these fails abort the process. I will leave this to you to fill in as required

// You should send a copy to yourself so you know what was sent
$mail->AddAddress($from);

// Add the recipient address
$mail->AddAddress($to);

$mail->From = $from;
$mail->FromName = 'Your name here';
// TO HERE

$grade = isset($_POST['grade']) ? $_POST['grade'] : false;
// don't continue if no valid input obtained
if (!$grade || $grade < 1 || $grade > 7) die();
// Prices by grade
// Starts from grade 2 - no grade 0 or 1 so zero values
$prices = [0,0,200,300,400,500,600,700];
ob_start();
require_once('email_handler.php');
$body = ob_get_clean();
?>
The message that would be sent is as follows
<div style="color: red; border: 1px solid red; padding: 15px">
<em>
<?php echo $body;?>
</em>
</div>

$body = "<table>
<tr>
<th colspan='2'>Contact Form</th>
</tr>

<tr>
<td style='font-weight:bold'>Full Name:</td>
<td>".$_POST['name']."</td>
</tr>

<tr>
<td style='font-weight:bold'>E-Mail:</td>
<td>".$_POST['email']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Mobile:</td>
<td>".$_POST['mobile_number']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Age:</td>
<td>".$_POST['age']."</td>
</tr>

<tr>
<td style='font-weight:bold'>City: </td>
<td>".$_POST['city']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Select Method: </td>
<td>".$_POST['method']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Grade of Baldness: </td>
<td>".$_POST['grade']."</td>
</tr>

<table>";

$body = preg_replace('/\\\\/','', $body); //Strip backslashes
$mail->MsgHTML($body);

$mail->IsSMTP(); // tell the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 25; // set the SMTP server port
//$mail->Host = "email@somewhere.com"; // SMTP server
//$mail->Username = "name@domain.com"; // SMTP server username
//$mail->Password = "password"; // SMTP server password

$mail->IsSendmail(); // tell the class to use Sendmail
$mail->AddReplyTo("email@somewhere.com");
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap = 80; // set word wrap
 

$mail->IsHTML(true); // send as HTML
$mail->Send();
echo 'Thank You. Your form has been submitted. Please check your mail for details.';
} catch (phpmailerException $e) {
echo $e->errorMessage();
}


?> 

Open in new window


NB: Please use code tags for your posts. Highlight your code and then click the CODE Button in the toolbar.

Also, there is a lot of overlap between this and your other question - things are going to get confusing for you trying to merge what experts are saying in the other thread with what is happening in this thread.
0
S SaxenaWitageAuthor Commented:
As you have suggested, I have made necessary changes in my php file. I tested this by sending the form as a user also from my other mail id. But I am receiving only the form send by user. I am not receiving the other form with the radio values. Following is my php after changes.

<?php
/**
* Simple example script using PHPMailer with 

exceptions enabled
* @package phpmailer
* @version $Id$
*/

require 'class.phpmailer.php';

try {
$mail = new PHPMailer(true); //New instance, 

with exceptions enabled

$mail->Subject = "Contact Form";

// ADD THIS
$from = "email@somewhere.com";
$to = isset($_POST['email']) ? $_POST['email'] : 

false;
// We would normally do a check here to see a) 

the address was provided and b) it is valid
// and if either of these fails abort the 

process. I will leave this to you to fill in as 

required

// You should send a copy to yourself so you 

know what was sent
$mail->AddAddress($from);

// Add the recipient address
$mail->AddAddress($to);

$mail->From = $from;
$mail->FromName = 'Your name here';
// TO HERE

$grade = isset($_POST['grade']) ? $_POST

['grade'] : false;
// don't continue if no valid input obtained
if (!$grade || $grade < 1 || $grade > 7) die();
// Prices by grade
// Starts from grade 2 - no grade 0 or 1 so zero 

values
$prices = [0,0,200,300,400,500,600,700];
ob_start();
require_once('email_handler.php');
$body = ob_get_clean();
?>
The message that would be sent is as follows
<div style="color: red; border: 1px solid red; 

padding: 15px">
<em>
<?php echo $body;?>
</em>
</div>

$body = "<table>
<tr>
<th colspan='2'>Contact Form</th>
</tr>

<tr>
<td style='font-weight:bold'>Full Name:</td>
<td>".$_POST['name']."</td>
</tr>

<tr>
<td style='font-weight:bold'>E-Mail:</td>
<td>".$_POST['email']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Mobile:</td>
<td>".$_POST['mobile_number']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Age:</td>
<td>".$_POST['age']."</td>
</tr>

<tr>
<td style='font-weight:bold'>City: </td>
<td>".$_POST['city']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Select Method: 

</td>
<td>".$_POST['method']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Grade of Baldness:
</td>
<td>".$_POST['grade']."
</td>
</tr>



<table>";

$body = preg_replace('/\\\\/','', $body); 

//Strip backslashes
$mail->MsgHTML($body);

$mail->IsSMTP(); // tell the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP 

authentication
$mail->Port = 25; // set the SMTP server port
//$mail->Host = 

"email@somewhere.com"; // SMTP 

server
//$mail->Username = "name@domain.com"; // SMTP 

server username
//$mail->Password = "password"; // SMTP server 

password

$mail->IsSendmail(); // tell the class to use 

Sendmail
$mail->AddReplyTo

("email@somewhere.com");
$mail->AltBody = "To view the message, please 

use an HTML compatible email viewer!"; // 

optional, comment out and test
$mail->WordWrap = 80; // set word wrap
  

$mail->IsHTML(true); // send as HTML
$mail->Send();
echo 'Thank You. Your form has been submitted. 

Please check your mail for details.';
} catch (phpmailerException $e) {
echo $e->errorMessage();
}


?>

Open in new window

0
S SaxenaWitageAuthor Commented:
Hello Julian,

Can you please tell me what wrong I am doing. I know you must be getting irritated by now. But you are my only hope.

Regards
Saxena
0
Julian HansenCommented:
I know you must be getting irritated by now.
Not with the questions, but Please use code tags! It makes your posts much easier to read - I have added them (again) for you in your last post.

I am not receiving the other form with the radio values
What other form?

I think we have drifted into the space where the specs have become murky.

Can you describe exactly what it is your code is meant to do.
0
S SaxenaWitageAuthor Commented:
What my code is meant to do is when the user submits the form, the form comes to me and the user gets an automated mail as "The cost for the option you have is---". This cost as you have given me the script will come from radio button selection.
As I said that I have changed my php as you suggested and filled the form from my other mail id.
I am receiving the form as email@somewhere.com which is the recipient. But I am not receiving the mail as a user informing me the cost.

Saxena
0
Julian HansenCommented:
Do you each get the same email or a different one - I am assuming the same.

Also you are using my code verbatim - which means there are bits in there that were only for demo. I have cleaned it up below.

This code works for me BUT I had to change the method for sending email. Your code had SMTP enabled with auth but then the user and password lines where commented out and later on you were using SendMail so you need to fix that in the code below.
<?php
/**
* Simple example script using PHPMailer with 

exceptions enabled
* @package phpmailer
* @version $Id$
*/

require 'includes/class.phpmailer.php';

try {
$mail = new PHPMailer(true); //New instance, with exceptions enabled

$mail->Subject = "Contact Form";

// ADD THIS
$from = "email@somewhere.com";
$to = isset($_POST['email']) ? $_POST['email'] :  false;

// We would normally do a check here to see a) 
// the address was provided and b) it is valid
// and if either of these fails abort the 
// process. I will leave this to you to fill in as 
// required
// You should send a copy to yourself so you 
// know what was sent

$mail->AddAddress($from);

// Add the recipient address
$mail->AddAddress($to);

$mail->From = $from;
$mail->FromName = 'Your name here';
// TO HERE

$grade = isset($_POST['grade']) ? $_POST['grade'] : false;
// don't continue if no valid input obtained
if (!$grade || $grade < 1 || $grade > 7) die();
// Prices by grade
// Starts from grade 2 - no grade 0 or 1 so zero 
// values

$prices = [0,0,200,300,400,500,600,700];

$body = "<table>
<tr>
<th colspan='2'>Contact Form</th>
</tr>

<tr>
<td style='font-weight:bold'>Full Name:</td>
<td>".$_POST['name']."</td>
</tr>

<tr>
<td style='font-weight:bold'>E-Mail:</td>
<td>".$_POST['email']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Mobile:</td>
<td>".$_POST['mobile_number']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Age:</td>
<td>".$_POST['age']."</td>
</tr>

<tr>
<td style='font-weight:bold'>City: </td>
<td>".$_POST['city']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Select Method: 

</td>
<td>".$_POST['method']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Grade of Baldness:
</td>
<td>".$_POST['grade']."
</td>
</tr>
<table>";

$body = preg_replace('/\\\\/','', $body); 

//Strip backslashes
$mail->MsgHTML($body);

//$mail->IsSMTP(); // tell the class to use SMTP
//$mail->SMTPAuth = true; // enable SMTP  authentication
//$mail->Port = 25; // set the SMTP server port
//$mail->Host = "email@somewhere.com"; // SMTP server
//$mail->Username = "name@domain.com"; // SMTP server username
//$mail->Password = "password"; // SMTP server password

//$mail->IsSendmail(); // tell the class to use Sendmail
$mail->AddReplyTo("email@somewhere.com");
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap = 80; // set word wrap
 
$mail->IsHTML(true); // send as HTML
$mail->Send();
echo 'Thank You. Your form has been submitted. 
Please check your mail for details.';
} catch (phpmailerException $e) {
	echo $e->errorMessage();
}


?>

Open in new window

0
S SaxenaWitageAuthor Commented:
As a user, I am not getting any mail. But as a recipient, I am getting the form but not the cost. I have removed the comments from the host, user and password. I have also removed send mail and added your script to send mail. My revised php is as follows. Even after this revised php, I am not receiving the mail as user. Please help.

<?php
/**
* Simple example script using PHPMailer with 

exceptions enabled
* @package phpmailer
* @version $Id$
*/

require 'includes/class.phpmailer.php';

try {
$mail = new PHPMailer(true); //New instance, 

with exceptions enabled

$mail->Subject = "Contact Form";

// ADD THIS
$from = "email@somewhere.com";
$to = isset($_POST['email']) ? $_POST['email'] : 

 false;

// We would normally do a check here to see a) 
// the address was provided and b) it is valid
// and if either of these fails abort the 
// process. I will leave this to you to fill in 

as 
// required
// You should send a copy to yourself so you 
// know what was sent

$mail->AddAddress($from);

// Add the recipient address
$mail->AddAddress($to);

$mail->From = $from;
$mail->FromName = 'Aspire';
// TO HERE

$grade = isset($_POST['grade']) ? $_POST

['grade'] : false;
// don't continue if no valid input obtained
if (!$grade || $grade < 1 || $grade > 7) die();
// Prices by grade
// Starts from grade 2 - no grade 0 or 1 so zero 
// values

$prices = [0,0,200,300,400,500,600,700];

$body = "<table>
<tr>
<th colspan='2'>Contact Form</th>
</tr>

<tr>
<td style='font-weight:bold'>Full Name:</td>
<td>".$_POST['name']."</td>
</tr>

<tr>
<td style='font-weight:bold'>E-Mail:</td>
<td>".$_POST['email']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Mobile:</td>
<td>".$_POST['mobile_number']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Age:</td>
<td>".$_POST['age']."</td>
</tr>

<tr>
<td style='font-weight:bold'>City: </td>
<td>".$_POST['city']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Select Method: 

</td>
<td>".$_POST['method']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Grade of Baldness:
</td>
<td>".$_POST['grade']."
</td>
</tr>
<table>";

$body = preg_replace('/\\\\/','', $body); 

//Strip backslashes
$mail->MsgHTML($body);

$mail->IsSMTP(); // tell the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP  

authentication
//$mail->Port = 25; // set the SMTP server port
$mail->Host = 

"email@somewhere.com"; // SMTP 

server
$mail->Username = 

"email@somewhere.com"; // SMTP 

server username
$mail->Password = "saxena@123"; // SMTP server 

password

<pre>
if (!mail($to, $subject, $body)) {
   // handle error here
   header('location: fail.html');
}
 
// redirect to next page here
header('location: success.html');
</pre>

$mail->AddReplyTo

("email@somewhere.com");
$mail->AltBody = "To view the message, please 

use an HTML compatible email viewer!"; // 

optional, comment out and test
$mail->WordWrap = 80; // set word wrap
 
$mail->IsHTML(true); // send as HTML
$mail->Send();
echo 'Thank You. Your form has been submitted. 
Please check your mail for details.';
} catch (phpmailerException $e) {
	echo $e->errorMessage();
}


?>

Open in new window

0
Julian HansenCommented:
As a user, I am not getting any mail. But as a recipient, I am getting the form but not the cost.
Can we define what is a
- user
- recipeint
- form
- cost

There are too many terms that have not been defined.
Mail - I have just done a test with that code and the $from address and the $_POST['email'] address both get mails - the same mail.

Form and cost - you seem to be implying that different people should get different mails - please explain this in detail. You refer to it from the perspective of your own frame of reference where this all makes sense but for those of us who are coming in fresh it is not clear.
0
NerdsOfTechTechnology ScientistCommented:
After this is solved, please ask cleanup to remove all of the real email addresses from this question.
0
S SaxenaWitageAuthor Commented:
Hello Julian,

1. User is the person who fills the form.
2. Recipient is me who receives the form.
3. Form is a simple html form where user fills his information and submits.
4. Cost is the prices that have been mentioned when the user selects any of the radio button from value 2 to value 7.

Objective:

I have a simple html form on my site where the user fills his details such as name, email id, phone number etc. and then he selects one of the radio buttons depending on his requirement. Every radio option has a price which the user has to pay if he wants to buy the product. What I want is that when the user submits the form, I should get his details in my mail and the user should get a message in his mail. The message should be "Hello, Thank you for contacting us. The cost for the option that you have selected is $1234."

Regards
Saxena
0
S SaxenaWitageAuthor Commented:
I have received the sample form that you have send. Can you please give me the full php code that you have used.

Saxena
0
Julian HansenCommented:
The form was sent with the PHP from the above post - no changes. I have reposted it.
Note that mail not being delivered does not mean it was not sent. You need to check spam filters and mail logs to see if the email was rejected.
<?php
/**
* Simple example script using PHPMailer with 

exceptions enabled
* @package phpmailer
* @version $Id$
*/

require 'includes/class.phpmailer.php';

try {
$mail = new PHPMailer(true); //New instance, with exceptions enabled

$mail->Subject = "Contact Form";

// ADD THIS
$from = "email@somewhere.com";
$to = isset($_POST['email']) ? $_POST['email'] :  false;

// We would normally do a check here to see a) 
// the address was provided and b) it is valid
// and if either of these fails abort the 
// process. I will leave this to you to fill in as 
// required
// You should send a copy to yourself so you 
// know what was sent

$mail->AddAddress($from);

// Add the recipient address
$mail->AddAddress($to);

$mail->From = $from;
$mail->FromName = 'Your name here';
// TO HERE

$grade = isset($_POST['grade']) ? $_POST['grade'] : false;
// don't continue if no valid input obtained
if (!$grade || $grade < 1 || $grade > 7) die();
// Prices by grade
// Starts from grade 2 - no grade 0 or 1 so zero 
// values

$prices = [0,0,200,300,400,500,600,700];

$body = "<table>
<tr>
<th colspan='2'>Contact Form</th>
</tr>

<tr>
<td style='font-weight:bold'>Full Name:</td>
<td>".$_POST['name']."</td>
</tr>

<tr>
<td style='font-weight:bold'>E-Mail:</td>
<td>".$_POST['email']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Mobile:</td>
<td>".$_POST['mobile_number']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Age:</td>
<td>".$_POST['age']."</td>
</tr>

<tr>
<td style='font-weight:bold'>City: </td>
<td>".$_POST['city']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Select Method: 

</td>
<td>".$_POST['method']."</td>
</tr>

<tr>
<td style='font-weight:bold'>Grade of Baldness:
</td>
<td>".$_POST['grade']."
</td>
</tr>
<table>";

$body = preg_replace('/\\\\/','', $body); 

//Strip backslashes
$mail->MsgHTML($body);

//$mail->IsSMTP(); // tell the class to use SMTP
//$mail->SMTPAuth = true; // enable SMTP  authentication
//$mail->Port = 25; // set the SMTP server port
//$mail->Host = "email@somewhere.com"; // SMTP server
//$mail->Username = "name@domain.com"; // SMTP server username
//$mail->Password = "password"; // SMTP server password

//$mail->IsSendmail(); // tell the class to use Sendmail
$mail->AddReplyTo("email@somewhere.com");
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap = 80; // set word wrap
 
$mail->IsHTML(true); // send as HTML
$mail->Send();
echo 'Thank You. Your form has been submitted. 
Please check your mail for details.';
} catch (phpmailerException $e) {
	echo $e->errorMessage();
}


?>

Open in new window

0
S SaxenaWitageAuthor Commented:
Hi,

I have tried this script again but after submitting form I am not receiving any thankyou message and it just shows me a blank page. In the browser's address bar I get the following:

http://www.aspirehairrestoration.com/calculator_form/php/email_handler_1.php

The mails are also not received. I checked spam filters and mail logs also. but no mail.
0
Julian HansenCommented:
Just checking that you updated the script with specific settings your your environment - such as which mail method to use and which email addresses to send to?

If I post to that URL I get a 404.
0
S SaxenaWitageAuthor Commented:
Hi,
Now I am getting the form at both the places. But what is happening is that the user means the person who is submitting the form also getting the form in his mail. What I wanted is the person who is submitting the form should only get this message in his mail that

 "Thank you for contacting us. The price for the option you have selected is $...."

If you see your php from line 38 to line 45, you have shown me how to get radio button value in price. But how to send the above message to the person who is submitting the form. Secondly he should not receive the form.
0
Julian HansenCommented:
Which brings me back to my earlier question about process - please refer to my comment in this thread
https://www.experts-exchange.com/questions/29067124/How-to-send-auto-generated-mail-to-user-based-on-radio-button-selection.html#a42360859
Do you each get the same email or a different one - I am assuming the same.

What you are saying now - is that you need to send two DIFFERENT emails please can you confirm this before we proceed.
0
S SaxenaWitageAuthor Commented:
I am really sorry if you feel offended. What exactly I wanted is when the person submits the form, the form comes to me and he gets the message in his mail that

"Hello, Thank you for contacting us. The price for the option you have selected is $1234"

That is the reason that when you asked me to define user, cost, recipient and form, I mentioned my this objective also.

I am once again sorry if you feel offended.

Saxena
0
Julian HansenCommented:
I am not offended. I am trying to help you get to your solution - and part of that is about making it clear exactly what you need.

Can we go on the premise that there will be two emails

The form data comes to you
The client gets an email listing the cost

What you need to do is take what you have already remove lines 19 and 32 (see note below)

That will send the email to you. Now you need to send an email to the client. I would recommend that you send it to yourself as well so no need to clear the recipients - however if you only want it to go to the client then add this
$mail->ClearAllRecipients();

Open in new window

I would not recommend this as it is always good to receive a copy of what is sent to your clients.

Next set the body to the message for your client and setup the addressing for your client (Remove line 19 and 32 from the post above  and place them as shown below);
$grade = isset($_POST['grade']) ? $_POST['grade'] :  false;
$to = isset($_POST['email']) ? $_POST['email'] :  false;

// Only proceed if we have valid data
if ($to && isset($prices[$grade])) { 
  // Change this as required
  $body = "Thank you the price will be {$prices[$grade]}";
  $mail->MsgHTML($body);
  $mail->AddAddress($to);
  $mail->Send();
}
else {
   // handle invalid input here
}

Open in new window

0
S SaxenaWitageAuthor Commented:
This is a little confusing for me.

I already have your line 2 in line 19 of my code.
Where do you want me to put your line 1 and the rest of the lines of your this code.
In your line 10 you have send the mail. That I am already doing. See my code from line 97 onwards.
Secondly, now I will be having two bodies. I have already used MsgHTML($body); in line 95 of my code.

Can you please add your lines in my code and explain me. You are really a nice teacher and I am learning a lot from you.
0
Julian HansenCommented:
Apologies - I was under some pressure with a deadline and could not work on a complete solution. The code below is probably how I would code what you want to do.

Some notes
1. I have used HEREDOC notation for the messages - if I were doing this I would prefer to keep the template separate and include the template as per my original sample

2. I have added email validation on the client's email AFTER sending the form to you. The reason for this is that you want to receive a notification in case the client typed their email incorrectly - you have their other details (Mobile / Telephone) to follow up on. However, if the email is invalid you don't want to try and send an email to them. I used the filter_var() to do the validation.

3. The script does not clear the recipients - as in my view you should receive a copy of what was sent to the client

<?php
/**
* Simple example script using PHPMailer with 

exceptions enabled
* @package phpmailer
* @version $Id$
*/

require 'includes/class.phpmailer.php';

try {
  $mail = new PHPMailer(true); //New instance, with exceptions enabled

  // ADD THIS
  $from = "email@somewhere.com";
  $mail->AddAddress($from);

  /******************************************************************** 
  ******             THIS IS WHERE WE SEND THE FORM TO YOU         ****
  *********************************************************************/
  $mail->From     = $from;
  $mail->FromName = 'Your name here';
  $mail->Subject  = "Contact Form";
  
  // WE EXTRACT THE FORM VARIABLES TO SAFE VERSIONS 
  $name   = isset($_POST['name'])          ? $_POST['name']          : '(no name specified)';
  $email  = isset($_POST['email'])         ? $_POST['email']         : false;
  $mobile = isset($_POST['mobile_number']) ? $_POST['mobile_number'] : '(no mobile specified)';
  $age    = isset($_POST['age'])           ? $_POST['age']           : '(no age specified)';
  $city   = isset($_POST['city'])          ? $_POST['city']          : '(no city specified)';
  $method = isset($_POST['method'])        ? $_POST['method']        : '(no method specified)';
  $grade  = isset($_POST['grade'])         ? $_POST['grade']         : 0;

  // SET UP THE FORM USING HEREDOC
  $body = <<< HTML
<table>
  <tr>
    <th colspan='2'>Contact Form</th>
  </tr>
  <tr>
    <td style='font-weight:bold'>Full Name:</td>
    <td>{$name}</td>
  </tr>
  <tr>
    <td style='font-weight:bold'>E-Mail:</td>
    <td>{$email}</td>
  </tr>
  <tr>
    <td style='font-weight:bold'>Mobile:</td>
    <td>{$mobile}</td>
  </tr>
  <tr>
    <td style='font-weight:bold'>Age:</td>
    <td>{$age}</td>
  </tr>
  <tr>
    <td style='font-weight:bold'>City: </td>
    <td>{$city}"</td>
  </tr>
  <tr>
    <td style='font-weight:bold'>Select Method:</td>
    <td>{$method}</td>
  </tr>
  <tr>
    <td style='font-weight:bold'>Grade of Baldness:</td>
    <td>{$grade}</td>
  </tr>
<table>
HTML;

  $body = preg_replace('/\\\\/','', $body); 

  //Strip backslashes
  $mail->MsgHTML($body);

  //$mail->IsSMTP(); // tell the class to use SMTP
  //$mail->SMTPAuth = true; // enable SMTP  authentication
  //$mail->Port = 25; // set the SMTP server port
  //$mail->Host = "email@somewhere.com"; // SMTP server
  //$mail->Username = "name@domain.com"; // SMTP server username
  //$mail->Password = "password"; // SMTP server password

  //$mail->IsSendmail(); // tell the class to use Sendmail
  $mail->AddReplyTo("email@somewhere.com");
  $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
  $mail->WordWrap = 80; // set word wrap
   
  // SEND THE EMAIL
  $mail->IsHTML(true); // send as HTML
  $mail->Send();

  /******************************************************************** 
  ******          THIS IS WHERE WE SEND THE EMAIL TO THE CLIENT  ******
  *********************************************************************/

  // SETUP OUR PRICE LIST
  $prices = [0,0,200,300,400,500,600,700];

  // GRADE AND TO ALREADY OBTAINED ABOVE
  // CHECK IF GRADE EXISTS AND EMAIL VALID
  if (filter_var($email, FILTER_VALIDATE_EMAIL)
      && isset($prices[$grade])) { 
  
    // WE SETUP THE EMAIL TO SEND WITH THE PRICE
    $body = <<< CLIENT
Dear {$name},<br/>
<p>Thank you for your enquiry, the price will be $ {$prices[$grade]}</p>
CLIENT;
    
	// SET THE SUBJECT
    $mail->Subject = "Website enquiry";
	
    // ADD IT TO THE EMAIL
    $mail->MsgHTML($body);

    // ADDRESS THE EMAIL
    $mail->AddAddress($email);
    
    // AND SEND IT
    $mail->Send();
  }
  else {
     // IF WE GET HERE IT BECAUSE EITHER
     //   * NO EMAIL SPECIFIED
     //   * GRADE IS INVALID
  }
  echo 'Thank You. Your form has been submitted. Please check your mail for details.';
} 
catch (phpmailerException $e) {
  echo $e->errorMessage();
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
S SaxenaWitageAuthor Commented:
Thanks Julian,
With little minor changes, it is working perfectly for me. I am really thankful to you for all your help. You are really a genius.

This is just off the topic. I am 63 years old and still trying to learn new things. I am a professional animator and have given number of visual effects in movies and now learning web development. I makes me feel really good to see a genius young generation coming up to take the world by storm.

Can I come back to Expert Exchange to learn more things from you?

Thanks and God Bless You.

Saxena
0
Julian HansenCommented:
. I makes me feel really good to see a genius young generation coming up to take the world by storm
Not so young  ...

Can I come back to Expert Exchange to learn more things from you?
Of course - that is what we are here for.
0
S SaxenaWitageAuthor Commented:
Julian is a genius, expert in his subject and extremely helpful. God bless him.
0
Julian HansenCommented:
Thank you S Saxena, appreciate the comment and you are most welcome.
0
S SaxenaWitageAuthor Commented:
Hello Julian,

The php script that you gave me earlier was working perfectly till yesterday morning. But suddenly, The form is not being submitted and I am getting the following error after submitting the form. I asked my hosting provider and he said that they have not made any changes or up gradation. This is the error:

Could not execute: /usr/sbin/sendmail -t -i

Could you please tell me what could be the problem.

Regards
Saxena
0
Julian HansenCommented:
That is a hosting issue - the error is saying that mail was passed to send mail and the process failed. The hosting provider's response, for me, translates into "please move your hosting business to a more competent provider" because saying they have not changed anything is a weak response. Things often break without changing anything. Sendmail is in their domain they should at least take a look to see why it failed.

In the meantime you can potentially use the SMTP functionality of PHPMailer and work through an SMTP server.
0
S SaxenaWitageAuthor Commented:
Thanks Julian, I will contact my hosting provider.
0
S SaxenaWitageAuthor Commented:
And one more question I want to ask.

I tried the form on localhost that is Wampsever. There also I am getting the same error. What could be the reason.
0
S SaxenaWitageAuthor Commented:
Hello Julian,
This is the reply I got from my hosting provider.

Hello,
It is not a server related issue.
Thats why I put a sample form mail script on your domain at http://www.aspirehairrestoration.com/formmailtest/index.php
which is working, you can also test it. This is the sample script he has given.

<?php
require_once "Mail.php";
$from = "email removed";

$to = "email removed";

$subject = "Hi!";
$body = "Hi,\n\nHow are you?";

$host = "host removed";

$port = "port";

$username = "email removed";

$password = "password removed";

$headers = array ('From' => $from,
  'To' => $to,
  'Subject' => $subject);

$smtp = Mail::factory('smtp',
  array ('host' => $host,
    'auth' => true,
 'port' => $port,
    'username' => $username,
    'password' => $password));

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
  echo("
" . $mail->getMessage() . "

");

 } else {
  echo("
Message successfully sent!

");
 }
?>

Open in new window


I am not been able to understand what to do. Can you please help.

Saxena
0
Julian HansenCommented:
I tried the form on localhost that is Wampsever. There also I am getting the same error. What could be the reason.
That one is easily explained.
WAMP = Windows
SendMail = Linux

On Windows you would need to either

a) Setup PHP mail functionality in your php.ini file so that you can use the default mail functionality to send your email
b) Setup the SMTP settings for PHPMailer

B) is the most portable option in my view.
0
S SaxenaWitageAuthor Commented:
Thanks Julian,
I understood about WAMP. Please see my other comment where I have written the answer I got from hosting provider. I am in a fix now as what to do and I have a deadline of tomorrow to make this site live.
If you see his sample script, he is not using sendmail. Are there any possibilities that I also use some other option rather than sendmail. My hosting server  is Linux.
Please help
Saxena
0
Julian HansenCommented:
Firstly, be very careful about posting unchecked code in a public forum. I have removed username's and passwords from your earlier post - but you should consider changing your password as that information was exposed.

Secondly, I have updated the original script I posted to use SMTP - you will need to fill in the hosts, usernames and passwords.

Before running the script you will need to make sure you have all the right files. If you want to use my script as is (bar the changes I mentioned in my second point above - then you need to follow these instructions

1. Go to this link and download the following files
PHPMailer.php
SMTP.php
Exception.php
2. Save these to a sub-folder (includes) off the folder where you save the script below

3. Change the script to include your host / username / password settings. Look for all fields with ***** YOUR .... ******

Script
<?php
/**
* Simple example script using PHPMailer with 

exceptions enabled
* @package phpmailer
* @version $Id$
*/

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'includes/PHPMailer.php';
require 'includes/SMTP.php';
require 'includes/Exception.php';

try {
  $mail = new PHPMailer(true); //New instance, with exceptions enabled

  // ADD THIS
  $from = "***** YOUR EMAIL HERE ******";
  $mail->AddAddress($from);

  /******************************************************************** 
  ******             THIS IS WHERE WE SEND THE FORM TO YOU         ****
  *********************************************************************/
  $mail->From     = $from;
  $mail->FromName = 'YOUR NAME HERE';
  $mail->Subject  = "Contact Form";
  
  // WE EXTRACT THE FORM VARIABLES TO SAFE VERSIONS 
  $name   = isset($_POST['name'])          ? $_POST['name']          : '(no name specified)';
  $email  = isset($_POST['email'])         ? $_POST['email']         : false;
  $mobile = isset($_POST['mobile_number']) ? $_POST['mobile_number'] : '(no mobile specified)';
  $age    = isset($_POST['age'])           ? $_POST['age']           : '(no age specified)';
  $city   = isset($_POST['city'])          ? $_POST['city']          : '(no city specified)';
  $method = isset($_POST['method'])        ? $_POST['method']        : '(no method specified)';
  $grade  = isset($_POST['grade'])         ? $_POST['grade']         : 0;

  // SET UP THE FORM USING HEREDOC
  $body = <<< HTML
<table>
  <tr>
    <th colspan='2'>Contact Form</th>
  </tr>
  <tr>
    <td style='font-weight:bold'>Full Name:</td>
    <td>{$name}</td>
  </tr>
  <tr>
    <td style='font-weight:bold'>E-Mail:</td>
    <td>{$email}</td>
  </tr>
  <tr>
    <td style='font-weight:bold'>Mobile:</td>
    <td>{$mobile}</td>
  </tr>
  <tr>
    <td style='font-weight:bold'>Age:</td>
    <td>{$age}</td>
  </tr>
  <tr>
    <td style='font-weight:bold'>City: </td>
    <td>{$city}"</td>
  </tr>
  <tr>
    <td style='font-weight:bold'>Select Method:</td>
    <td>{$method}</td>
  </tr>
  <tr>
    <td style='font-weight:bold'>Grade of Baldness:</td>
    <td>{$grade}</td>
  </tr>
<table>
HTML;

  $body = preg_replace('/\\\\/','', $body); 

  //Strip backslashes
  $mail->MsgHTML($body);

  $mail->IsSMTP(); // tell the class to use SMTP
  $mail->SMTPAuth = true; // enable SMTP  authentication
  $mail->Port = 587; // set the SMTP server port
  $mail->Host = "***** YOUR SMTP HOST HERE ******"; // SMTP server
  $mail->Username = "***** YOUR USERNAME HERE ******"; // SMTP server username
  $mail->Password = "***** YOUR PASSWORD HERE ******"; // SMTP server password

  //$mail->IsSendmail(); // tell the class to use Sendmail
  $mail->AddReplyTo("***** YOUR REPLY ADDRESS HERE ******");
  $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
  $mail->WordWrap = 80; // set word wrap
   
  // SEND THE EMAIL
  $mail->IsHTML(true); // send as HTML
  $mail->Send();

  /******************************************************************** 
  ******          THIS IS WHERE WE SEND THE EMAIL TO THE CLIENT  ******
  *********************************************************************/

  // SETUP OUR PRICE LIST
  $prices = [0,0,200,300,400,500,600,700];

  // GRADE AND TO ALREADY OBTAINED ABOVE
  // CHECK IF GRADE EXISTS AND EMAIL VALID
  if (filter_var($email, FILTER_VALIDATE_EMAIL)
      && isset($prices[$grade])) { 
  
    // WE SETUP THE EMAIL TO SEND WITH THE PRICE
    $body = <<< CLIENT
Dear {$name},<br/>
<p>Thank you for your enquiry, the price will be $ {$prices[$grade]}</p>
CLIENT;
    
	// SET THE SUBJECT
    $mail->Subject = "Website enquiry";
	
    // ADD IT TO THE EMAIL
    $mail->MsgHTML($body);

    // ADDRESS THE EMAIL
    $mail->AddAddress($email);
    
    // AND SEND IT
    $mail->Send();
  }
  else {
     // IF WE GET HERE IT BECAUSE EITHER
     //   * NO EMAIL SPECIFIED
     //   * GRADE IS INVALID
  }
  echo 'Thank You. Your form has been submitted. Please check your mail for details.';
} 
catch (phpmailerException $e) {
  echo $e->errorMessage();
}

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.