...

View Full Version : passing values from html <input> to php mail()



Yakisoba
06-22-2005, 09:08 AM
There has to be an easy way to do this, I just can't seem to find it...

I have an .html form that once submitted will run "mail.php" (action="mail.php" method="post").

I would like mail.php to grab the information from the form and send it to a specified (hardcoded) email address.

below is mail.php
(Note: this script works if I hardcode all the variables i.e - $msgBody = "blah blah blah")


$to= "some@address.co.jp";
//$from = $_POST["from"]; not necessary (for me) at the moment. or is it?
$subject = $_POST["subject"];
$msgBody = $_POST["msgBody"];
mail("$to","$subject","$msgBody");
echo "finished!";


here is the .html form:


<form action="php/mail.php" method="post" >
<!-- <label>To:<input type="text" size="30" disabled></label><br/> -->
<input class="from" type="text" size="30" name="from" title="from" value="from_person@somwhere.co.jp" disabled>
<br/><br/>
<input class="subject" type="text" size="30" name="subject" title="subject" value="Subject">
<br/><br/>
<textarea class="msgBody" rows="13" cols="65" wrap="hard" title="msgBody" name="msgBody">type message here</textarea>
<br/><br/>
<input class="btn_send" type="submit" name="send" value="send" align="middle" />
<br/>
</form>


How do I get the values from the text boxes and textarea to post to mail.php?

Thanks

Yak

delinear
06-22-2005, 10:58 AM
You don't need the quotes for the variables within the mail function, you can just use:

mail($to,$subject,$msgBody);
Also, if you want to get all the data from the form into the message body, you could use a loop-through, something like this:

$to= "some@address.co.jp";
$subject = $_POST["subject"];

$msgBody = '';
foreach($_POST['msgBody'] as $key => $value) {
$msgBody .= $key ": " $value "\r\n";
}
mail($to, $subject, $msgBody);
echo "finished!";
You might want to strip out some of the post data though, as that will include things like the button name and value. You can strip out unwanted stuff with a line in the foreach loop, something like: if($key != 'send') etc.

Yakisoba
06-23-2005, 06:50 AM
alright, I know this is going to sound a little weird, bear with me...

It was my understanding, originally, that the quotes were not necessary around the variables in the mail() function. However, once I remove them from my script, nothing works. Also when I add anything extra/different to the script, nothing works.
When I say nothing works, I mean absolutly nothing happens. The word "finished!" doens't even echo.

for example...

this works:


$to = "valid@address.co.jp";
$subject = "Mail Test";
$msg = "If I don't get this working soon I will destroy this computer";
$headers = "From: anothervalid@address.com\r\n";
mail("$to", "$subject", "$msg", "$headers");
echo "finished!";


this does nothing:


$to = "valid@address.co.jp";
$subject = "Mail Test";
$msg = "If I don't get this working soon I will destroy this computer";
$headers = "From: anothervalid@address.com\r\n";
mail($to, $subject, $msg, $headers);
echo "finished!";


this does nothing:


$to = "valid@address.co.jp";
$subject = $_POST["subject"];
$msg = "If I don't get this working soon I will destroy this computer";
$headers = "From: anothervalid@address.com\r\n";
mail("$to", "$subject", "$msg", "$headers"); //with or without "quotes"
echo "finished!";


and, I hate to say it...this does nothing:


$to= "some@address.co.jp";
$subject = $_POST["subject"];

$msgBody = '';
foreach($_POST['msgBody'] as $key => $value) {
$msgBody .= $key ": " $value "\r\n";
}
mail($to, $subject, $msgBody);
echo "finished!";



Any suggestions?

Thanks,

Yak

It may have to do with the fact I'm using a Japanese computer ( the character set is S_JIS ). This has caused me grief in the past.

jasong
06-23-2005, 06:57 AM
The following works for me:



$email = $_POST["email"];
$mail_to = "you@blah.com";
$mail_subject = "Subject";
$mail_from = "From: $email";
$mail_message = "You have a order!";

mail( $mail_to, $mail_subject, $mail_message, $mail_from );


I hope that helps :)

Jason

Yakisoba
06-23-2005, 08:47 AM
Thanks jasong...I forgot I could do it that way.

I seem to have it working...I'm still not sure why my method doesn't work.

Also, I still need to keep the "quotes" in the mail().


here's the working code...



$form_subject = $_POST["subject"];
$form_msg = $_POST["msgBody"];

$to = "valid@address.co.jp";
$subject = "$form_subject";
$msg = "$form_msg";
$headers = "From: another.valid@adrress.com\r\n";
mail("$to", "$subject", "$msg", "$headers");
echo "finished!";



anybody have any suggestions to why my previous methods were not working?

Thanks,

Yak

westmatrix99
02-24-2007, 09:35 PM
Here is some code try it.



<?php
require('class.phpmailer.php');
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host = 'localhost';
$mail->From = 'email@domain.com';
$mail->FromName = 'Name';
$mail->AddBCC (email@domain.com, Name);
$mail->AddReplyTo('email@domain.com', 'Name');// optional name
$mail->WordWrap = 50; // set word wrap to 50 characters
//$mail->AddAttachment('image.jpg', 'name of image'); // optional name
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = 'Some subject';
$mail->Body = '';
$mail->AltBody = '';
if(!$mail->Send())
{
echo 'Message could not be sent. <p>';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
echo 'Message has been sent';
?>


Cheers
West

_Aerospace_Eng_
02-24-2007, 09:36 PM
I use the contact script here and it works fine
http://www.joe2torials.com/view_tutorial.php?view=68

CFMaBiSmAd
02-24-2007, 09:46 PM
westmatrix99, check the date of posts that you add responses to. This one of from over a year and a half ago.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum