lifeway Posted December 6, 2006 Share Posted December 6, 2006 Im' using PHPMailer for my web site forms. I can successfully use the form but I don't get the form answers in the email notification. I need to know How to code the PHP so that it pulls the values from the form and sends them to my email. Can anyone help me?Thanks Quote Link to comment Share on other sites More sharing options...
the_oliver Posted December 6, 2006 Share Posted December 6, 2006 <?php$EmailFrom = Trim(stripslashes($_POST['EmailFrom']));$EmailTo = "bla@bla.net";$Subject = Trim(stripslashes($_POST['Subject']));$field1 = Trim(stripslashes($_POST['field1']));$field2 = Trim(stripslashes($_POST[field2']));// validation$validationOK=true;if (Trim($EmailFrom)=="") $validationOK=false;if (!$validationOK) { print "<meta http-equiv=\"refresh\" content=\"0;URL=redirectto.whatever\">"; exit;}// prepare email body text$Body = "";$Body .= "Company: ";$Body .= $field1;$Body .= "\n";$Body .= "Enquiry: ";$Body .= $field2;$Body .= "\n";// send email$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");// redirect to success pageif ($success){ //IF SUCCESFULL REDIRECT TO print "<meta http-equiv=\"refresh\" content=\"0;URL=../redirect.bla">";}else{//IF UNSUCCESFULL REDIRECT TO print "<meta http-equiv=\"refresh\" content=\"0;URL=../contact.php?hdr=merror&usr=non_idnt&area=public\">";}?> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted December 6, 2006 Share Posted December 6, 2006 Please post the code that is not working.Ken Quote Link to comment Share on other sites More sharing options...
the_oliver Posted December 6, 2006 Share Posted December 6, 2006 Sorry, ment to explain that but hit the mouse by ,mistake!This bit:[code]$Subject = Trim(stripslashes($_POST['Subject']));[/code]Is the bit that is getting the data from the form. Your form should be set as follows:[code]<form method="POST" action="send.php">[/code]and you replace the send.php with teh file where you stored the code abouve.All that is is saying is to cut out any spaces etc from that particular string posted when the form was submited. Where i have put subject or, field1 etc you put in whatever your textarea of field was called. The text/value that was in that field will then be stored in the varible at the start and you can do what you like.The //validation bit is juat checking that there is a value in a field, and if not stoping the form from being submited.The //prepare email body text does exactly what it says. Its taking all the strings you have taken out of the form, and putting them into one, is a coherent manor.//Send emailis the bit that does all the work. Its taking all the variables and actualy sending the email.The final if statment is simple redirecting the users, depending on weather it sent correctly or not.Hope that makes sence Quote Link to comment Share on other sites More sharing options...
trq Posted December 6, 2006 Share Posted December 6, 2006 Sorry [i]the_oliver[/i] but simply handing out code does not help the OP with there problem. Quote Link to comment Share on other sites More sharing options...
the_oliver Posted December 6, 2006 Share Posted December 6, 2006 fair point. will take note! Quote Link to comment Share on other sites More sharing options...
lifeway Posted December 6, 2006 Author Share Posted December 6, 2006 [quote author=kenrbnsn link=topic=117650.msg480121#msg480121 date=1165446186]Please post the code that is not working.Ken[/quote]The PHP code is working but not sending the form information to the email... here is the code that my host requires for PHPMailer:[code]<?PHP require("/sites/servername/username/home/includes/class.phpmailer.php");$mail = new PHPMailer();$mail->IsSMTP();$mail->Host = "mail.YourDomain.com";$mail->SMTPAuth = true;$mail->Username = "you@domain.com";$mail->Password = "password"; $mail->From = "you@domain.com";$mail->FromName = "Your Name";$mail->AddAddress("user@domain.com", "Display Name");$mail->IsHTML(true); $mail->Subject = "Test message sent using the PHPMailer component";$mail->Body = "This is a test message."; if(!$mail->Send()){ echo "Message could not be sent. <p>"; echo "Mailer Error: " . $mail->ErrorInfo; exit;} echo "Message has been sent";?>[/code]All my hosting tech support said was that I need to "code the PHP to pull the values from the form". But I'm not sure how to do that or where to put it in the code.Thanks Quote Link to comment Share on other sites More sharing options...
the_oliver Posted December 7, 2006 Share Posted December 7, 2006 Ok, so as i explained above you need to get the data out of the form by posting it, and then add it into your Body: (this bit)[code]$mail->Body = "This is a test message.";[/code]The way to do this is to set your form action to POST. Do this by putting the <form method="POST" action="send.php"> at the top of your form. At the bottom of your form i asume you have a button or something. This should be set to Submit the form. Esentualy posting it is making it a variable that is availible to the script that does the sending (your php script). Odviously the next stage is to get the Posted variable back in a way that is usedull to you.This is done by using [code]$_POST['field_name'])[/code]Where i have put field name you put what ever your textarea, or field, or whatever, was called in the origional for. This will get the data that was in that field back out, and you can use it as a standard variable. In your case this would be by adding it into your $mail->Body bit. Try [code]$mail->Body = "Name: ".$_POST['name_field'])." was the content of the for.";[/code]There for what ever was in the field called name_field would be included in the body of the message. Quote Link to comment Share on other sites More sharing options...
lifeway Posted December 7, 2006 Author Share Posted December 7, 2006 Thanks so much for your help both of you... I think I'm getting it... ran into an error though. According to your instructions, [i]the_oliver[/i], I inserted the code as you said. It's in bold below. but I got this error message:Parse error: parse error, unexpected ')' in /sites/yuma4/esmerling123/home/public_html/emailtest.php on line 21Any suggestions are welcome.<?PHP require("/sites/servername/username/home/includes/class.phpmailer.php");$mail = new PHPMailer();$mail->IsSMTP();$mail->Host = "mail.YourDomain.com";$mail->SMTPAuth = true;$mail->Username = "you@domain.com";$mail->Password = "password"; $mail->From = "you@domain.com";$mail->FromName = "Your Name";$mail->AddAddress("user@domain.com", "Display Name");$mail->IsHTML(true); $mail->Subject = "Test message sent using the PHPMailer component";$mail->Body = "This is a test message.";[b]$mail->Body = "Name: ".$_POST['Name'])." was the content of the for.";[/b] if(!$mail->Send()){ echo "Message could not be sent. <p>"; echo "Mailer Error: " . $mail->ErrorInfo; exit;} echo "Message has been sent";?> Quote Link to comment Share on other sites More sharing options...
corbin Posted December 7, 2006 Share Posted December 7, 2006 $mail->Body = "Name: ".$_POST['Name']." was the content of the for.";Had a weird ) in it... Quote Link to comment Share on other sites More sharing options...
lifeway Posted December 7, 2006 Author Share Posted December 7, 2006 Pure Genious... That works great :) thanks... I now have one more question. How do I put in multiple codes like that? for example using the same example like below in [b]bold[/b] I only get sent the answer for Phone and not Name. If I were to put a third, say for Email, I would only get the Email value. How can I make it so I get all the answers and not just the one?<?PHP require("/sites/servername/username/home/includes/class.phpmailer.php");$mail = new PHPMailer();$mail->IsSMTP();$mail->Host = "mail.YourDomain.com";$mail->SMTPAuth = true;$mail->Username = "you@domain.com";$mail->Password = "password"; $mail->From = "you@domain.com";$mail->FromName = "Your Name";$mail->AddAddress("user@domain.com", "Display Name");$mail->IsHTML(true); $mail->Subject = "Test message sent using the PHPMailer component";$mail->Body = "This is a test message.";[b]$mail->Body = "Name: ".$_POST['Name']." was the content of the for.";[/b][b]$mail->Body = "Phone: ".$_POST['Phone']." was the content of the for.";[/b] if(!$mail->Send()){ echo "Message could not be sent. <p>"; echo "Mailer Error: " . $mail->ErrorInfo; exit;} echo "Message has been sent";?> Quote Link to comment Share on other sites More sharing options...
the_oliver Posted December 7, 2006 Share Posted December 7, 2006 Thats because by using body the seccond time you are redefining it not addin to it. There are two ways that you can do this. This first is by adding a period after the body variable for each new part. The period is simply joining the 'new' body deffinition onto the end of the orijional definition:[code]$Body = "";$Body .= "Name: ";$Body .= $_POST['Name'];$Body .= "\n"; //MAKE A SPACE IN THE LINES (LIKE <BR>)$Body .= "Telephone: ";$Body .= $_POST['Telephone'];[/code]And you can keep doing this for as long as you like! The other way is to just keep joining them into your origional line, as you did with the first one:[code]$mail->Body = "Name: ".$_POST['Name']." has the telephone number".$_POST['Telephone']." and lives in ".$_POST['Town']."ane etc";[/code]It is also possible to send HTML emails, so you can include images, colours and a better layout. If your intrested in this one, i asked how a fiew posts ago and got a good awnser, so just dig it out! Quote Link to comment Share on other sites More sharing options...
lifeway Posted December 7, 2006 Author Share Posted December 7, 2006 Ahhh... That's what I need [i]the_oliver[/i]. You're a Genious... Thanks a bunch!!!I'm almost there... what you suggested gave me another question... the code you suggested for a break:[code]$Body .= "\n"; //MAKE A SPACE IN THE LINES (LIKE <BR>)[/code]only made a space (like Name: John Doe Phone: 555-1212) but didn't list the answers on a different line. is there a code that can make it so the answers are sent in list form such as:Name: John DoePhone: 555-1212etc. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.