PHP mail form subject line

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shaunmckinnon
Forum Newbie
Posts: 5
Joined: Sat Dec 06, 2003 2:11 pm

PHP mail form subject line

Post by shaunmckinnon »

Here's my problem.

I need to have it so that what ever is selected in the selection box will appear in the subject line of my email form. If someone can suggest the code for this, it'd be appreciated. here's my code:

<?php

//This is the location of your sendmail variable
//if you don't know, contact your sysadmin
$mail_path = "/usr/sbin/sendmail -t -i";

//The email address to be receiving emails from this form
$mail_to = "shaun@256fxdesigns.ca, crap@256fxdesigns.ca";

//the subject of the email sent by the form
$mail_subject = "Comments";

//the name of the buisness or website that the form contacts
$buisness_name = "Stop and Save Liquidation Surplus Sales";

//this is the html of the form used to create the email
$form_html = '$html = "

<form action=\"$PHP_SELF\" method=\"post\">
<table class=\"mainText\" border=\"1\" align=\"center\" bordercolor=\"0099ff\" cellspacing=\"5\">

<tr>
<td>Full Name: </td>
<td>$name</td>
</tr>
<tr>
<td>Home Phone: </td>
<td>$home</td>
</tr>
<tr>
<td>Work/Cell Phone: </td>
<td>$work</td>
</tr>
<tr>
<td>Email: </td>
<td>$reply<td>
</tr>
<tr>
<td>Inquiry: </td>
<td>$inquiry</td>
</tr>
<tr>
<td colspan=\"2\" align=\"center\">Comments</td>
</tr>
<tr>
<td colspan=\"2\" align=\"center\">$message</td>
</tr>
<tr>
<td colspan=\"2\" align=\"center\">$send</td>
</tr>
</table>
</form>
";';


ini_set("sendmail_from", $mail_from);
ini_set("sendmail_path", $mail_path);

if ($_POST)
{
if($_POST['userEmail'])
{
if( mail($mail_to,$mail_subject, "\t From: ".$_POST['userName']."\n \t Home Phone: ".$_POST['userHome']."\n \t Work or Cell Phone: ".$_POST['userWork']."\n \t Email: ".$_POST['userEmail']."\n \t Inquiry: ".$_POST['userInquiry']."\n\n\n". stripslashes($_POST['userMessage']), "From: $_POST[userEmail]\r\n"
."Reply-To: $_POST[userEmail]\r\n") )
{
$html = 'Thank you for contacting '.$buisness_name.', '.$_POST['userName'].'. <br/>Your email was sent <br/>';
}
else
{
$html = 'There was an error';
}
}
}
else
{
$message = '<textarea name="userMessage" rows="10" cols="30"></textarea>';
$name = '<input type="text" name="userName" size="30">';
$home = '<input type="text" name="userHome" size="30">';
$work = '<input type="text" name="userWork" size="30">';
$reply = '<input type="text" name="userEmail" size="30">';
$inquiry = '<select name="userInquiry" size="1">
<option selected>General Inquiries</option>
<option>Franchise Purchasing</option>
<option>Product Information</option>
<option>Store Information</option>
<option>Ebay Help</option></select>';

$send = '<input type="submit" name="submit" value="Send">';

eval($form_html);
}
?>

Thank you in advance.

shaun mckinnon
User avatar
William
Forum Contributor
Posts: 332
Joined: Sat Oct 25, 2003 4:03 am
Location: New York City

Post by William »

Why not make it with the if commands so like if blah blah blah was selected print this .ect?
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

You can read any variables sent through a form using $_POST.

For example, if your <select> was named "EmailSubjects" and you submitted the form, the PHP page which you submit the form to can read the value of "EmailSubjects" with $_POST["EmailSubjects"].

This is an example of what the PHP page might look like (the one that you submit the form to).

Code: Select all

<?php

$subject = $_POST["EmailSubject"];
mail("someone@home.com", $subject, "blabla", "From:Bob");

?>
Hope that helps.
shaunmckinnon
Forum Newbie
Posts: 5
Joined: Sat Dec 06, 2003 2:11 pm

Hey Thanks!!!

Post by shaunmckinnon »

It worked perfect. I was approaching it the wrong way. Thanks a lot. Here's the script revised with your suggestion. Feel free to anyone to use this script for a quick and simple email form.

<?php

//This is the location of your sendmail variable
//if you don't know, contact your sysadmin
$mail_path = "/usr/sbin/sendmail -t -i";

//The email address to be receiving emails from this form
$mail_to = "shaun@256fxdesigns.ca, crap@256fxdesigns.ca";

//the subject of the email sent by the form
$mail_subject = $_POST["userInquiry"];

//the name of the buisness or website that the form contacts
$buisness_name = "Stop and Save Liquidation Surplus Sales";

//this is the html of the form used to create the email
$form_html = '$html = "

<form action=\"$PHP_SELF\" method=\"post\">
<table class=\"mainText\" border=\"1\" align=\"center\" bordercolor=\"0099ff\" cellspacing=\"5\">

<tr>
<td>Full Name: </td>
<td>$name</td>
</tr>
<tr>
<td>Day Phone: </td>
<td>$home</td>
</tr>
<tr>
<td>Night Phone: </td>
<td>$work</td>
</tr>
<tr>
<td>Email: </td>
<td>$reply<td>
</tr>
<tr>
<td>Inquiry: </td>
<td>$inquiry</td>
</tr>
<tr>
<td colspan=\"2\" align=\"center\">Comments</td>
</tr>
<tr>
<td colspan=\"2\" align=\"center\">$message</td>
</tr>
<tr>
<td colspan=\"2\" align=\"center\">$send</td>
</tr>
</table>
</form>
";';


ini_set("sendmail_from", $mail_from);
ini_set("sendmail_path", $mail_path);

if ($_POST)
{
if($_POST['userEmail'])
{
if( mail($mail_to,$mail_subject, "\t From: ".$_POST['userName']."\n \t Home Phone: ".$_POST['userHome']."\n \t Work or Cell Phone: ".$_POST['userWork']."\n \t Email: ".$_POST['userEmail']."\n \t Inquiry: ".$_POST['userInquiry']."\n\n\n". stripslashes($_POST['userMessage']), "From: $_POST[userEmail]\r\n"
."Reply-To: $_POST[userEmail]\r\n") )
{
$html = 'Thank you for contacting '.$buisness_name.', '.$_POST['userName'].'. <br/>Your email was sent <br/>';
}
else
{
$html = 'There was an error';
}
}
}
else
{
$message = '<textarea name="userMessage" rows="10" cols="30"></textarea>';
$name = '<input type="text" name="userName" size="30">';
$home = '<input type="text" name="userHome" size="30">';
$work = '<input type="text" name="userWork" size="30">';
$reply = '<input type="text" name="userEmail" size="30">';
$inquiry = '<select name="userInquiry" size="1">
<option selected>General Inquiries</option>
<option>Franchise Purchasing</option>
<option>Product Information</option>
<option>Store Information</option>
<option>Ebay Help</option></select>';

$send = '<input type="submit" name="submit" value="Send">';

eval($form_html);
}
?>

then don't forget to include this <?=$html?> into the body of your html page.

Thanks again.

Shaun McKinnon
256FX Designs.ca

example here: http://www.stopandsave.ca/contact.php
AnsonM
Forum Commoner
Posts: 72
Joined: Thu Sep 25, 2003 7:21 am

Post by AnsonM »

it would be easier if you changed those $_POST things to variables..
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Those "$_POST things" are variables.
Post Reply