Barsoj Posted December 8, 2014 Share Posted December 8, 2014 Hi everyone. Pretty desperate, first time that I'm working with php. Learning this language, makes sense, however I can't figure out why my code is not working. Emails are not coming in at all. Additional required info (phone number, first name, last name) should be included in the message. Please help. Thank you everyone. <?php$to = "abcd@abcd.com";$subject = "From Website Contact Form";$first = $_REQUEST['first'];$last = $_REQUEST['last'];$email = $_REQUEST['email'];$phone = $_REQUEST['phone'];$MESSAGE_BODY = "Name: " . $_POST["first"] . "\n";$MESSAGE_BODY = "Name: " . $_POST["last"] . "\n";$MESSAGE_BODY = "Contact No: " . $_POST["phone"] . "\n";$MESSAGE_BODY = "Email: " . $_POST["email"] . "\n";$MESSAGE_BODY = "Requirement: " . nl2br($_POST["message"]) . "\n";$message = $_REQUEST['message' + 'email' + 'first' + 'last'];$from = $_REQUEST['email'];$headers = "From:" . $from;mail($to, $subject, $MESSAGE_BODY, $headers);echo "Your message has been sent";?> Quote Link to comment https://forums.phpfreaks.com/topic/292967-email-contact-form-code-please-help/ Share on other sites More sharing options...
Zane Posted December 8, 2014 Share Posted December 8, 2014 My vehicle 'doesn't work' either. Try and guess why? Quote Link to comment https://forums.phpfreaks.com/topic/292967-email-contact-form-code-please-help/#findComment-1499004 Share on other sites More sharing options...
Barsoj Posted December 8, 2014 Author Share Posted December 8, 2014 My vehicle 'doesn't work' either. Try and guess why? right Quote Link to comment https://forums.phpfreaks.com/topic/292967-email-contact-form-code-please-help/#findComment-1499010 Share on other sites More sharing options...
CroNiX Posted December 8, 2014 Share Posted December 8, 2014 strip it down to the basics and test. Can you send a regular email like with: mail('abcd@abcd.com', 'test email subject', 'test email body', 'FROM: root@your_server.tld'); root@your_server.tld should be your actual domain. If it's not it most likely will be rejected by the receiving server as the domains won't match. Quote Link to comment https://forums.phpfreaks.com/topic/292967-email-contact-form-code-please-help/#findComment-1499013 Share on other sites More sharing options...
Ch0cu3r Posted December 8, 2014 Share Posted December 8, 2014 One lines 4, 5, 6 and 7 you define variables but never use them? On lines 9 through to 12 you need to use the concatenation assignment operator .= not the assignment operator = Otherwise only the Requirement line will send in the email body. Line 13. Not sure what you are doing there. Could you tell use how you are testing this code? Are you sure PHP has been configured to use a mail server? Quote Link to comment https://forums.phpfreaks.com/topic/292967-email-contact-form-code-please-help/#findComment-1499015 Share on other sites More sharing options...
Barsoj Posted December 9, 2014 Author Share Posted December 9, 2014 strip it down to the basics and test. Can you send a regular email like with: mail('abcd@abcd.com', 'test email subject', 'test email body', 'FROM: root@your_server.tld'); root@your_server.tld should be your actual domain. If it's not it most likely will be rejected by the receiving server as the domains won't match. Yes, it does work. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/292967-email-contact-form-code-please-help/#findComment-1499077 Share on other sites More sharing options...
maxxd Posted December 9, 2014 Share Posted December 9, 2014 Turn on error reporting and let us know what it has to say. I'm with Ch0cu3r in that I have no clue what you're trying to do with the line '$message = $_REQUEST['message' + 'email' + 'first' + 'last'];', but I have to be it's throwing an error that you're not seeing. Put this error_reporting(-1); ini_set('display_errors',true); at the top of your script. Quote Link to comment https://forums.phpfreaks.com/topic/292967-email-contact-form-code-please-help/#findComment-1499083 Share on other sites More sharing options...
Barsoj Posted December 9, 2014 Author Share Posted December 9, 2014 Guys, day three learning this language; did the tests and found the mistakes, thanks to you guys for pointing me the right direction. Thank you Cronix and Ch0cu3r! Here's my final working code: <?php$to = "1234@1234.co.uk";$subject = "From Website Contact Form";$first = $_REQUEST['first'];$last = $_REQUEST['last'];$email = $_REQUEST['email'];$phone = $_REQUEST['phone'];$MESSAGE_BODY .= "First name: " . $_POST["first"] . "\n";$MESSAGE_BODY .= "Last name: " . $_POST["last"] . "\n";$MESSAGE_BODY .= "Phone No: " . $_POST["phone"] . "\n";$MESSAGE_BODY .= "Email: " . $_POST["email"] . "\n\n";$MESSAGE_BODY .= "Message: " . nl2br($_POST["message"]) . "\n";$from = $_REQUEST['email'];$headers = "From:" . $email;mail($to, $subject, $MESSAGE_BODY, $headers);echo "Your message has been sent";?> Awesome. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/292967-email-contact-form-code-please-help/#findComment-1499084 Share on other sites More sharing options...
boompa Posted December 9, 2014 Share Posted December 9, 2014 OK, now go read this: http://securephpwiki.com/index.php/Email_Injection And consider moving to a library like PHPmailer or SwiftMailer. Quote Link to comment https://forums.phpfreaks.com/topic/292967-email-contact-form-code-please-help/#findComment-1499107 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.