Jump to content

Need help to send an email in PHP


saurav99990

Recommended Posts

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.
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.