...

View Full Version : A small problem with sending e-mail



swiltch
11-25-2010, 12:33 PM
Hi guys, I've recently started a web design course and have been introduced to php. I was given a template that talked me through how to use php to send the information to my email address, and send a copy to the email the user provided.

My code works in sending some information to the email I put in at the top, but not the values I want, and it doesn't send an email to the address provided in the field.

I need it to send the information from the fields from enquiry.html to my email, and send an email to the users address that they provided, I'm just really stuck! Any help is really appreciated.

the form is HERE. (http://www.swiltch.com/enquiry.htm)
the php is HERE (http://www.swiltch.com/charityFormMail.php)


<?PHP

/*
FormMail.php
The program is in two parts:
the first part receives the values form your Form and assigns them to variables.
An email is then sent to the specified address.
The second part creates a simple html response page.

Note that a php script is essentially just anordinary html script with some php statements
embedded within the code using <php and ?> as the start and end php tags.

This program as it is expects that
1. you have written a Form with 'action=FormMail.php method=post'
2. your Form contains 3 fields 'name', 'email', and 'message'
(You can, if you are careful, amend this program to deal with more fields)
3. your Form contains a hidden field named 'subject' which conatains the phrase
you wish to appear in the 'Subject' line of the email that is sent to you

To use this program as it is you *must* change the next line after these comments
"you@uwe.ac.uk" must be replaced with your email address
*/

$YourEmail = "email@example.com";

/*
To use the program as it is you do not need to change anything more.
Comments are included to explain what is happening
(and to note places where you could edit the program.)

The next lines receive the field values from your Form and assign them
to $variables which are used later on in the program.
You could create more variables to handle
more Form fields - or edit the statements to reflect the names in your Form
The syntax is $variable=$_POST[fieldname];
*/

$name=$_POST[name];
$email=$_POST[email];
$message=$_POST[message];
$Age=$_POST[Age];

/*
The next lines build your email
(You have already assigned a value to 'YourEmail' above.)
*/

$sendto= "$YourEmail";
$headers.= "MIME-Version: 1.0\r\n";
$headers.= "Content-type: text/html; ";
$headers.= "charset=iso-8859-1\r\n";
$headers.= "From: $email";
$subject=$_POST[subject];
// Build the email body text
$emailcontent = "

Name: $name
Email: $email
Message:
$message
"
/* and the email is sent later - after we have started the html page
so that we can include an appropriate message within the page */
?>

<!-- HTML PAGE
Except for the inclusion of php variables and the 'mail' statement
the following is straightforward html code.
You can add more html code but it should be outside the <php .. ?> tags.
-->
<html>
<head>
</script>
<link rel="stylesheet" type="text/css"
href="charity.css" >
<title>Form Mail Response</title>
</head>
<body>
<div id="title">Thank you for your message</div>

<?php
// Send out the email or output an error message
if (mail($sendto, $subject, $emailcontent, $headers))

{

?>

<div class="row"><label class="col1comment"><b><center>Thank You <?php echo $name; ?></b></center></label>
<span class="col2comment"><p><center>An email has been sent to the address you gave.</p></center>
</span>
</div>

<div class="row"><label class="col1comment"><b><center>You sent the following</b></center></label>
<span class="col2comment">
<p><center>
Name:<b><?php echo $name; ?> </b>
<br>
Email:<b><?php echo $email; ?> </b>
<br>
</center></p></span>
</div>

<div class="row"><label class="col1comment"></label>
<span class="col2comment">
<p><center>
Message:<b><?php echo $message; ?> </b>
</center></p></span>
</div>


<div class="row"><label class="col1comment"></label>
<span class="col2comment">
<span class="col2comment">
<p><center>
Age:<b><?php echo $Age; ?> </b>
<br>
Gender:<b><?php echo $sex; ?> </b>
<br>
Preferences:<b><?php echo $check; ?><?php echo $check1; ?><?php echo $check2; ?><?php echo $check3; ?> </b>
</p></center>
</span>
</div>

<div align="center" class="submit">
<b>We will be in touch as soon as possible.</b>
</div>

<?php
}

else { // email send has been unsuccessful
?>
<p>&nbsp;</p>
<p><b>Sorry <?php echo $name; ?></b></p>
<p>For some reason the form submission has not been successful.</p>


<img src="http://www.funnyjunksite.com/wp-content/uploads/2007/04/funny_cat_pictures_085.jpg" alt="Sad cat" />


<p>You sent the following:</p>
<table border="0" align="center" cellpadding="6">
<tr>
<td>Name: </td><td><b><?php echo $name; ?> </b> </td>
</tr>
<tr>
<td>Email: </td><td> <b><?php echo $email; ?> </b></td>
</tr>
<tr>
<td>Message: </td><td><b><?php echo $message; ?> </b> </td>
</tr>
<tr>
<td>Age: </td><td><b><?php echo $Age; ?> </b> </td>
</tr>
<tr>
<td>Gender: </td><td><b><?php echo $sex; ?> </b> </td>
</tr>
<tr>
<td>Preferences: </td><td><b><?php echo $check; ?> </b> </td>
</tr>
</table>


<p> Please try again.</p>
<?php
}
?>
</body>
</html>

vinyl-junkie
11-25-2010, 03:01 PM
I see a couple of problems, which I've corrected in red below. The way you're doing those lines of code, it is treating your PHP variables as literals instead. I don't know if they're all that is wrong, but maybe it will get you a little further.

Hope this helps.


<?PHP

/*
FormMail.php
The program is in two parts:
the first part receives the values form your Form and assigns them to variables.
An email is then sent to the specified address.
The second part creates a simple html response page.

Note that a php script is essentially just anordinary html script with some php statements
embedded within the code using <php and ?> as the start and end php tags.

This program as it is expects that
1. you have written a Form with 'action=FormMail.php method=post'
2. your Form contains 3 fields 'name', 'email', and 'message'
(You can, if you are careful, amend this program to deal with more fields)
3. your Form contains a hidden field named 'subject' which conatains the phrase
you wish to appear in the 'Subject' line of the email that is sent to you

To use this program as it is you *must* change the next line after these comments
"you@uwe.ac.uk" must be replaced with your email address
*/

$YourEmail = "email@example.com";

/*
To use the program as it is you do not need to change anything more.
Comments are included to explain what is happening
(and to note places where you could edit the program.)

The next lines receive the field values from your Form and assign them
to $variables which are used later on in the program.
You could create more variables to handle
more Form fields - or edit the statements to reflect the names in your Form
The syntax is $variable=$_POST[fieldname];
*/

$name=$_POST[name];
$email=$_POST[email];
$message=$_POST[message];
$Age=$_POST[Age];

/*
The next lines build your email
(You have already assigned a value to 'YourEmail' above.)
*/

$sendto= $YourEmail;
$headers.= "MIME-Version: 1.0\r\n";
$headers.= "Content-type: text/html; ";
$headers.= "charset=iso-8859-1\r\n";
$headers.= "From: " & $email;
$subject=$_POST[subject];
// Build the email body text
$emailcontent = "

Name: $name
Email: $email
Message:
$message
"
/* and the email is sent later - after we have started the html page
so that we can include an appropriate message within the page */
?>

<!-- HTML PAGE
Except for the inclusion of php variables and the 'mail' statement
the following is straightforward html code.
You can add more html code but it should be outside the <php .. ?> tags.
-->
<html>
<head>
</script>
<link rel="stylesheet" type="text/css"
href="charity.css" >
<title>Form Mail Response</title>
</head>
<body>
<div id="title">Thank you for your message</div>

<?php
// Send out the email or output an error message
if (mail($sendto, $subject, $emailcontent, $headers))

{

?>

<div class="row"><label class="col1comment"><b><center>Thank You <?php echo $name; ?></b></center></label>
<span class="col2comment"><p><center>An email has been sent to the address you gave.</p></center>
</span>
</div>

<div class="row"><label class="col1comment"><b><center>You sent the following</b></center></label>
<span class="col2comment">
<p><center>
Name:<b><?php echo $name; ?> </b>
<br>
Email:<b><?php echo $email; ?> </b>
<br>
</center></p></span>
</div>

<div class="row"><label class="col1comment"></label>
<span class="col2comment">
<p><center>
Message:<b><?php echo $message; ?> </b>
</center></p></span>
</div>


<div class="row"><label class="col1comment"></label>
<span class="col2comment">
<span class="col2comment">
<p><center>
Age:<b><?php echo $Age; ?> </b>
<br>
Gender:<b><?php echo $sex; ?> </b>
<br>
Preferences:<b><?php echo $check; ?><?php echo $check1; ?><?php echo $check2; ?><?php echo $check3; ?> </b>
</p></center>
</span>
</div>

<div align="center" class="submit">
<b>We will be in touch as soon as possible.</b>
</div>

<?php
}

else { // email send has been unsuccessful
?>
<p>&nbsp;</p>
<p><b>Sorry <?php echo $name; ?></b></p>
<p>For some reason the form submission has not been successful.</p>


<img src="http://www.funnyjunksite.com/wp-content/uploads/2007/04/funny_cat_pictures_085.jpg" alt="Sad cat" />


<p>You sent the following:</p>
<table border="0" align="center" cellpadding="6">
<tr>
<td>Name: </td><td><b><?php echo $name; ?> </b> </td>
</tr>
<tr>
<td>Email: </td><td> <b><?php echo $email; ?> </b></td>
</tr>
<tr>
<td>Message: </td><td><b><?php echo $message; ?> </b> </td>
</tr>
<tr>
<td>Age: </td><td><b><?php echo $Age; ?> </b> </td>
</tr>
<tr>
<td>Gender: </td><td><b><?php echo $sex; ?> </b> </td>
</tr>
<tr>
<td>Preferences: </td><td><b><?php echo $check; ?> </b> </td>
</tr>
</table>


<p> Please try again.</p>
<?php
}
?>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum