Re: How do you create a contact form with PHP mailer?

Hello.

It is easy, but I tried to make a sample.
The code is not very clean, but please refer to it if you like.

First is the form part.

----- index.html -----

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="message-Type" message="text/html; charset=UTF-8">
<title>Contact</title>
</head>
<body>
<h1>Contact</h1>
<p>
 Please fill in the necessary information in the form below. <br>
<form action="" method="post">
<label for=""
<input type="text" id="name" name="name" required><br>
<label for="" address</label>
<input type="text" id="email" name="email" required><br>
<label for=""
<textarea id="message" name="message" required></textarea>
<input type="submit" value="Send">
</p>
</body>
</html>

--------------------

Next is the email transmission part.
Rewrite the PHPMailer path and SMTP information as necessary.

----- mail.php -----

<?php
mb_internal_encoding("UTF-8");

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require_once("/path/to/PHPMailer/Exception.php");
require_once("/path/to/PHPMailer/PHPMailer.php");
require_once("/path/to/PHPMailer/SMTP.php");

// config
$subject = "mail subject";
$from_name = "your name";
$from_address = "your address";
$smtp_host = "your smtp host";
$smtp_port = smtp port number;
$smtp_secure = smtp secure mode(ssl or tls);
$smtp_auth = True if SMTP authentication is required;
$smtp_username = "your smtp username";
$smtp_password = "your smtp password";

// Simple validation process of sent data
if (isset($_POST["name"])){
$name = $_POST["name"];
}else{
$name = "";
}

if (isset($_POST["email"])){
$email = $_POST["email"];
}else{
$email = "";
}

if (isset($_POST["message"])){
$message = $_POST["message"];
}else{
$message = "";
}

if ($name == "" || preg_match("/^\s+$/", $name)){
html_output("Error", "Please input your name.");
exit;
}

if ($email == ""){
html_output("Error", "Please input your mail address.");
exit;
}elseif (filter_var($email, FILTER_VALIDATE_EMAIL) === false){
html_output("Error", "Mail address format is invalid.");
exit;
}

if ($message == ""){
html_output("Error", "Please input your message.");
exit;
}
$message = htmlspecialchars($message, ENT_QUOTES,"utf-8");

// Mail send process
$mailer = new PHPMailer(false);
$mailer->CharSet = "utf-8";
$mailer->XMailer = " ";
$mailer->isSMTP();
$mailer->Host = $smtp_host;
$mailer->SMTPAuth = $smtp_auth;
$mailer->Username = $smtp_username;
$mailer->Password = $smtp_password;
$mailer->SMTPSecure = $smtp_secure;
$mailer->Port = $smtp_port;
$mailer->From = $from_address;
$mailer->FromName = mb_encode_mimeheader($from_name);
$mailer->Subject = mb_encode_mimeheader($subject);
$mailer->addAddress($from_address, "");

$send_content = <<<MSG
 There was an inquiry from the inquiry form.
 The contents are as follows.

 Name: $name
 Mail Address: $email
 Message:
$message

MSG;
$mailer->Body = $send_content;
if ($mailer->Send()){
html_output("Finished", "We have received your inquiry. Thank you very much.");
}else{
html_output("Error", "There was an error sending email.");
}
exit;

function html_output($title, $body){
echo <<<HTML
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="message-Type" message="text/html; charset=UTF-8">
<title>$title</title>
</head>
<body>
<h1>$title</h1>
<p>
$body<br>
<input type="button" value="Back" _onClick_="history.back();"><br>
</p>
</body>
</html>
HTML;
}
?>

--------------------

Best regards.

-- 
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
  • ... AudioGames . net Forum — Developers room : riku via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : riku via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : riku via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : tdani via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : thetechguy via Audiogames-reflector

Reply via email to