saurav99990 Posted May 10, 2015 Share Posted May 10, 2015 I am using this code to send an email from a contact form. <?php if (isset($_POST['contact_name']) && isset ($_POST['contact_email']) && isset($_POST['contact_text'])) { $contact_name = $_POST['contact_name']; $contact_email = $_POST['contact_email']; $contact_text = $_POST['contact_text']; if (!empty($contact_name) && !empty($contact_email) && !empty($contact_text)) { $to = 'saurav.chill05@gmail.com'; $subject = 'Contact form Submitted.'; $body = $contact_name."\n".$contact_text; $headers = 'From: '.$contact_email; if (@mail($to, $subject, $body, $headers)) { echo 'Thanks for contacting us.'; } else { echo 'Sorry.'; } } else { echo 'All feild are required.'; } } ?> <form action="index.php" method="POST"> Name:<br><input type="text" name="contact_name"><br><br> Email Address:<br><input type="text" name="contact_email"><br><br> Message:<br> <textarea name="contact_text" rows="6" cols="30"></textarea><br><br> <input type="submit" value="Send"> </form> This is not working on localhost. I tried to host this file on a hosting site and then filled the form it says thank you for contacting us, but i don't get any email. please help me with this. Quote Link to comment https://forums.phpfreaks.com/topic/296188-need-help-to-send-an-email-in-php/ Share on other sites More sharing options...
fastsol Posted May 11, 2015 Share Posted May 11, 2015 You can't send an email in localhost unless you setup a local email server or use smtp to someplace like gmail or yahoo. There could be many reasons beyond your code as to why you don't get the email. First, did you check you spam box? It's seriously best to use a php library like PHPMailer to send emails. There is a TON of things that go into actually sending and receiving an email, far beyond the simple mail() in php. PHPMailer handles pretty much all of those other things for you, which will "help" with delivery. Go HERE and download the zip file for PHPMailer. Then HERE for a basic working example code on how to use it. As for your code, remove the @ in front of the mail(). You should never use the @ to suppress php errors, you'll have a very difficult time diagnosing issues like that. Quote Link to comment https://forums.phpfreaks.com/topic/296188-need-help-to-send-an-email-in-php/#findComment-1511358 Share on other sites More sharing options...
saurav99990 Posted May 11, 2015 Author Share Posted May 11, 2015 how can I do it without PHP mailer? Quote Link to comment https://forums.phpfreaks.com/topic/296188-need-help-to-send-an-email-in-php/#findComment-1511362 Share on other sites More sharing options...
fastsol Posted May 11, 2015 Share Posted May 11, 2015 I tested your code and it works just fine. The issue is that gmail is likely not accepting your email cause it lacks an immense amount of required things that a legitimate email should have. Hence the highly suggested use of PHPMailer You can achieve it by simply using the mail() with more appropriate headers being set but take it from someone that has spent an tremendous amount of time on this subject for my own online business, use PHPMailer, it's stupid not to. Quote Link to comment https://forums.phpfreaks.com/topic/296188-need-help-to-send-an-email-in-php/#findComment-1511365 Share on other sites More sharing options...
CroNiX Posted May 12, 2015 Share Posted May 12, 2015 For one thing, if your host is named "myhost.com", you need to send email FROM someone@myhost.com. If the FROM in an email differs from the HOST, most ISPs will reject it outright or send it to spam. Email is extremely tricky and one of the most complex things in terms of getting high deliverability. You need things like PTR DNS records, Reverse DNS records, DKIM, your IP must have a good "reputation" and not be on any RBLs, etc. If you have critical emails that MUST get delivered, you should really look to using a service like SendGrid which will do all of that for you. Quote Link to comment https://forums.phpfreaks.com/topic/296188-need-help-to-send-an-email-in-php/#findComment-1511556 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.