As the reference on the subject, I have been struggling to make a system which I can get an email from the form on the website. I use a server, HostGator. What I did is below
- Created the form on the contact page embraced form tags, having method="POST" action="send.php".
- Created my mail account on HostGator
I filled all the information about the host on send.php which shows a PHP Mailer basic format.
4.After cricking the button named, "submit", the error shows like "This page isn't working. localhost is currently unable to handle this request. HTTP ERROR 500". Thus, I have been stack here now.
The name of my project folder is "businessWeb". The directory of all the folders inside it is below
•businessweb/vender/composer/...
•businesswebvender/phpmailer/...
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
//Load composer's autoloader
require './vendor/autoload.php';
// get variables from the form
$name = filter_var($_POST['name'],FILTER_SANITIZE_STRING);
$phone = filter_var($_POST['phone'],FILTER_SANITIZE_STRING);
$email = filter_var($_POST['email'],FILTER_SANITIZE_STRING);
$message = filter_var($_POST['message'],FILTER_SANITIZE_STRING);
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Server settings
$mail->SMTPDebug = 0; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host ='the address in Incoming section on Cpanel in HostGator '; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'email on hostGator'; // SMTP username
$mail->Password = '***'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
//Sender
$mail->setFrom('email on HostGator', 'Joey Namiki');
//Recipients
$mail->addAddress('my gmail account', 'Joey');
//Body Content
$body = "<p>Hello<br /><br /> You have received an enquiry from<br /> " . $name . "<br /><br /> The message is <br />" . $message . "<br /><br />You can contact on <br /> " . $phone . "</p>";
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'JoeyNamiki Website Enquiry from '. $name;
$mail->Body = $body;
$mail->AltBody = strip_tags($body);
$mail->send();
header('location: thankyou.php');
} catch (Exception $e) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
?>