Uips Posted March 22, 2016 Share Posted March 22, 2016 Hello im new to PHP and i have problems getting PHP working for my websites contac form Here the HTML form code <form action="" method="post"> <div class="form_settings"> <p><span>Name</span><input class="contact" type="text" name="your_name" value="" /></p> <p><span>Email Address</span><input class="contact" type="text" name="your_email" value="" /></p> <p><span>Message</span><textarea class="contact textarea" rows="8" cols="50" name="your_enquiry"></textarea></p> <p style="padding-top: 15px"><span> </span><input class="submit" type="submit" name="contact_submitted" value="submit" /></p> </div> </form> Heres the PHP code <?php $field_name = $_POST['cf_name']; $field_email = $_POST['cf_email']; $field_message = $_POST['cf_message']; $mail_to = 'example@gmail.com'; $subject = 'Message from '.$field_name; $body_message = 'From: '.$field_name."\n"; $body_message .= 'E-mail: '.$field_email."\n"; $body_message .= 'Message: '.$field_message; $headers = 'From: '.$field_email."\r\n"; $headers .= 'Reply-To: '.$field_email."\r\n"; $mail_status = mail($mail_to, $subject, $body_message, $headers); if ($mail_status) { ?> <script language="javascript" type="text/javascript"> alert('Sent'); window.location = 'kontakt.html'; </script> <?php } else { ?> <script language="javascript" type="text/javascript"> alert('Error'); window.location = 'kontakt.html'; </script> <?php } ?> I would be really thankful if someone could fix the PHP code for me and make it work with the html above. Quote Link to comment https://forums.phpfreaks.com/topic/301064-email-contact-form-php-help/ Share on other sites More sharing options...
cyberRobot Posted March 22, 2016 Share Posted March 22, 2016 The name used in your input fields <input class="contact" type="text" name="your_name" value="" /> ...needs to match the text used in your $_POST variables. $field_name = $_POST['cf_name']; For the above field, try the following: $field_name = $_POST['your_name']; Quote Link to comment https://forums.phpfreaks.com/topic/301064-email-contact-form-php-help/#findComment-1532336 Share on other sites More sharing options...
Jacques1 Posted March 22, 2016 Share Posted March 22, 2016 This is a help forum, not a we'll-guess-what-your-problem-is-and-then-fix-the-code-for-you-forum. If you have a problem, you need to actually tell us what that is. You're also expected to actively work on the issue and not just hand over the code. What have you tried? What exactly do you not understand? What I can tell you is that you need to start thinking about security. You can't just take user input from random people and insert it straight into the e-mail headers and the message, because this effectively turns your server into an open mail relay where anybody can send e-mails to arbitrary addresses. Use a proper mailer library like PHPMailer instead of the terrible mail() function. Quote Link to comment https://forums.phpfreaks.com/topic/301064-email-contact-form-php-help/#findComment-1532337 Share on other sites More sharing options...
Uips Posted March 22, 2016 Author Share Posted March 22, 2016 Thx cyberRobot, i finaly got it working, slightly different code. Its very basic but works for me since im new to PHP <?php $name = $_POST['name']; $email = $_POST['email']; $telefon = $_POST['telefon']; $liik = $_POST['liik']; $message = $_POST['message']; $email_message = " Nimi: ".$name." E-post: ".$email." Telefon: ".$telefon." Töö Liik: ".$liik." Töö kirjeldus: ".$message." "; mail ("@outlook.com" , "Hinnapäring", $email_message); if (mail) { ?> <script language="javascript" type="text/javascript"> alert('Saadetud'); window.location = 'hinnaparing.html'; </script> <?php } else { ?> <script language="javascript" type="text/javascript"> alert('Saatmisel esines viga'); window.location = 'hinnaparing.html'; </script> <?php } ?> Im curious if its possible to change the sender to email typed in the form. Right now the web host sends me the message. I could Reply faster if it would refer the email as sender. If its possible can someone give me a head start what should i do? Quote Link to comment https://forums.phpfreaks.com/topic/301064-email-contact-form-php-help/#findComment-1532346 Share on other sites More sharing options...
Jacques1 Posted March 22, 2016 Share Posted March 22, 2016 Since you also seem to be new to the Internet: The blue underlined text is a link. You click on it with your left mouse button. >> Click me << Quote Link to comment https://forums.phpfreaks.com/topic/301064-email-contact-form-php-help/#findComment-1532349 Share on other sites More sharing options...
cyberRobot Posted March 23, 2016 Share Posted March 23, 2016 Im curious if its possible to change the sender to email typed in the form. Right now the web host sends me the message. You would use the additional_headers argument for mail(). More information can be found here: http://php.net/manual/en/function.mail.php Note that your original code utilized the argument. With that said, you'll want to work on the security aspect of the code, as suggested by Jacques1. Adding a user-supplied address in the email header, for example, opens your script to email injection attacks. More information can be found here: http://securephpwiki.com/index.php/Email_Injection Quote Link to comment https://forums.phpfreaks.com/topic/301064-email-contact-form-php-help/#findComment-1532375 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.