Nanne Posted January 6, 2008 Share Posted January 6, 2008 Hi everybody, this is my first post - yeay I’m trying to create a mail form with some additional fields for contact information that the user must type in. I’m interested in general tips and tricks as well. If you have better solutions for my problems then mine, please share them. And if you have any other tips for me, security leak or something like that, feel free to tell me. I’m not just interested in creating this mail form I also want to learn as much as possible, all the ins and outs with this kind of PHP scripting Problem #1 – Formatting the text in the mail, e.g. putting text in bold The user who sends the mail via the mail form must enter some contact information. That info is then entered into the mail along with the message he types. I want to put some text in bold so that it is easier for the person that receives the mail to read it. Now, I have solved this by making all the text into an html-document (applying html tags) and send this as a mail. This, however, causes a minor problem if the receiver’s mail program does not support html-mails. Is there a way to format the text in a regular text mail? Problem #2 – Problem with Swedish letters The Swedish letters that were sent did not display correctly in my mail program (MS Outlook 2007). Now, again I did solve this by adding this into the html that is sent: <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /> I also added these headers to be sent with the mail: $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n"; I used the UTF-8 character set here, is this the best choice for the Swedish language? I first tried ISO-8859-1 and ISO-8859-15 but it didn’t work. But how would this be achieved if the mail was not sent as a html-mail (see problem #1 above)? Problem #3 – Mail is sent twice if the page is reloaded After the mail is sent there is a message that tells the user that the message is on its way. But the problem here is if the user reloads the page the mail is sent once more, how can I fix this? See my code below. Problem #4 – SPAM filter I have a simple idea for a simple SPAM-filter. The script generates two random numbers between 1 and 9 and asks the user to input the sum of the two numbers. But I can’t get the variables from the block of code in which they were generated down to the block of code below where I’m using them to check against the users input. How do I fix this? I’ve read about variable scope on the net but wasn’t able to figure it out anyhow. I’ve tried to put them in their own code block at the top of the script but then I got the problem that the script gives two random numbers when the form is displayed but generates two new numbers when the submit button is pressed so that the value that the user specified is wrong and there by the mail will not be sent. How can I work around this? See my code below. Here is a link to the form so that might be easier to visualise how I want it to work. Feel free to try it, the mail() function is not implemented so no mail will be sent, the mail will just be displayed in your browser. Sorry for the Swedish language, but I hope you can figure it all out anyway http://nanne-san.com/emprove/ Here is the code, enjoy <body> <?php if (!isset($_POST["submit"])) {?> <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" /> <fieldset> <legend>Kontaktinformation</legend> <table border="0"> <tr><td><strong>Namn</strong></td><td><strong>Företag</strong></td></tr> <tr><td><input type="text" name="name" size="40" /></td> <td><input type="text" name="company" size="40" /></td></tr> <tr><td><strong>Telefonnummer</strong></td><td><strong>Email</strong></td></tr> <tr><td><input type="test" name="telephone" size="40" /></td> <td><input type="text" name="email" size="40" /></td></tr> </table> </fieldset> <fieldset> <legend>Medelande</legend> <table border="0"> <tr><td colspan="2"><strong>Ärende</strong></td></tr> <tr><td colspan="2"><input type="text" name="subject" size="88"></td></tr> <tr><td colspan="2"><strong>Medelande</strong></td></tr> <tr><td colspan="2"><textarea name="message" cols="84" rows="8"></textarea></td></tr> </table> </fieldset> <fieldset> <legend>SPAM-kontroll</legend> <table border="0"> <tr><td> <?php //random numbers for my SPAM-filter $randOne = rand(1, 9); $randTwo = rand(1, 9); echo "Vad är $randOne + $randTwo? <input type=\"text\" name=\"spam\" size=\"1\" maxlength=\"2\" />"; ?></td></tr> <tr><td colspan="2"><input class="submit" type="submit" name="submit" value="Skicka medelandet"> <input type="reset" name="Reset" value="Återställ"></td></tr> </form> </table> </fieldset> <?php } else { $name = stripslashes(htmlentities($_POST["name"], ENT_NOQUOTES, "utf-8")); $company = stripslashes(htmlentities($_POST["company"], ENT_NOQUOTES, "utf-8")); $telephone = stripslashes(htmlentities($_POST["telephone"], ENT_NOQUOTES, "utf-8")); $email = stripslashes($_POST["email"]); $subject = stripslashes($_POST["subject"]); $message = nl2br(stripslashes(htmlentities($_POST["message"], ENT_COMPAT, "utf-8"))); $spam = $_POST["spam"]; //The contact information and the message is put together into a HTML-dokument and then sent $themail = "<html>\n<head>\n <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /> \n<style type=\"text/css\">\n<!--\nbody {font-family: Arial; font-size: 12px;} \n--> \n</style> \n<title>" . $subject . "</title>\n</head>\n<body>" . "\n<br /><b>Namn:</b> " . $name . "\n<br /><b>Företag:</b> " . $company . "\n<br /><b>Telefon:</b> " . $telephone . "\n<br /><b>Email:</b> " . $email . "\n<br />----------------------------------------------------------\n<br />" . $message . "\n</body>\n</html>"; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n"; //This is just temprary to check that the message and SPAM-filer is working correctly echo $themail; echo "<p>$randOne $randTwo</p>"; if($spam == $randOne + $randTwo) echo "the sum is right"; else echo "the sum is wrong"; //Here is the mail-code and the error massages, currently disabled in order to test the other part of the script /*if($name && $subject && $email && $message ) { // if all fields were filled-in send email @mail("kristoffer@nanne-san.com","$subject","$themail","From: $name <$email>, $headers") or die("Ett fel uppstog, var god försök igen senare."); echo "Anmälan är skickad, vi ses på träningen!"; // if all went well, display message was sent } else { echo "Du har inte fyllt i alla fält korrekt.<br>"; // if not all were filled in, display error message }*/ } ?> </body> Cheers mates! Quote Link to comment https://forums.phpfreaks.com/topic/84752-i-have-a-couple-of-questions-about-my-php-mail-form/ 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.