I created a contact form in PHP, but it is not sending emails from my website.
My contact form doesn't work perfectly, and after uploading it to a live server like cPanel or hPanel, emails are not being delivered. When a user submits the contact form, the form is submitted successfully, but I don't receive any emails in my inbox.
I have tried using the PHP mail() function, but it is not working on my hosting server. I also contacted my hosting provider, but they couldn't resolve my problem.
I am using Hostinger hPanel and GoDaddy cPanel hosting.
if(isset($_POST['sub']))
{
$name=$_POST['name'];
$email=$_POST['email'];
$msg=$_POST['msg'];
$phone=$_POST['phone'];
$FromName="My Website Title";
$FromEmail="info@mywebsite.com";
$ReplyTo=$email;
$toemail="mytoemail@gmail.com";
$subject="Contact Form";
$message='Name:-'.$name.'<br>Email :-' .$email.'<br> Phone No:-'.$phone.'<br> Message:-'.$msg;
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "From: ".$FromName." <".$FromEmail.">\n";
$headers .= "Reply-To: ".$ReplyTo."\n";
$headers .= "X-Sender: <".$FromEmail.">\n";
$headers .= "X-Mailer: PHP\n";
$headers .= "X-Priority: 1\n";
$headers .= "Return-Path: <".$FromEmail.">\n";
if(mail($toemail, $subject, $message, $headers,'-f'.$FromEmail) ){
$hide=1;
echo 'Sent';
} else {
echo "Failed";
}
}
How can I send emails from a PHP contact form using PHP SMTP?
Why is my PHP contact form not sending emails?
How do I fix the PHP contact form email not working issue on cPanel or hPanel hosting?
The PHP mail() function is still used a lot these days, but the biggest problem is that there is no 100% guarantee that your email will be delivered successfully. It has many delivery-related issues.
If you submit a contact form and, after submitting it you see a success message like "Your message has been sent successfully," but you still don't receive any contact details or email in your inbox, then there can be several reasons behind this problem. We will discuss them one by one below.
This problem happens with both beginners and experienced developers. Whenever you submit a contact form and the email is not delivered, you can check the following points to solve the problem.
1. The first thing you should know is that if you are testing your contact form on a localhost server like XAMPP or WAMP, your email will never be sent because the PHP mail() function does not work on a localhost server.So, if you are testing email sending on localhost using the PHP mail() function, it will not work.
2. Another reason is that some hosting providers disable the PHP mail() function on their servers. If your hosting provider has disabled it, your PHP contact form will not be able to send emails.
3. Sometimes, your hosting provider may also block or suspend your outgoing email service for some reason. In that case, your contact form will also stop sending emails.
4. Another common mistake is using the wrong From Email Address. For example, if your website is technosmarter.com and you set the From email address as rahul123@gmail.com, email providers like Gmail and Yahoo may treat it as spam or even reject it.
The From email address should always be your own domain email, such as:
info@technosmarter.com
support@technosmarter.com
You can install PHPMailer using Composer, or you can simply download it directly from GitHub. PHPMAILER Github
In the tutorial linked below, I have explained everything, including how to install PHPMailer, how to create a contact form using PHPMailer, how SMTP works, and how to configure your official email address, password, host, SSL, port, and other SMTP settings.
https://technosmarter.com/php/how-to-send-email-using-php-mailer-library-and-hosting-smtp
Using PHPMailer, you can send emails through your official website email address, and your contact form emails are delivered much more reliably.
1. Never test the PHP mail() function on localhost. If you do, your contact form email will not work. Always test your contact form on a live hosting server.
2. Whenever you use the PHP mail() function or PHPMailer, always use your website's official email address.
For example:
info@yourwebsite.com
support@yourwebsite.com
Always use an official domain email instead of a personal Gmail account.
3. Whether you are using cPanel, hPanel, or a VPS server, all hosting providers give you an Email Accounts section where you can create your official website email address.
From there, you can also get your SMTP Host, Port, SSL, Username, and Password, which you can configure in PHPMailer to send emails successfully.
I personally recommend that you always use PHPMailer for your PHP contact forms because it is much more reliable than the default PHP mail() function, and your emails have a much better chance of being delivered successfully.
I have already shared the complete tutorial link above. You can follow it step by step, and if you're still facing any problems, feel free to leave a comment.