Blog

Ready-made examples of codes and scripts, programs and instructions

php example of ready-to-use working code for a request form
<?php 
 // Get data from the $_POST array
 $email = $_POST['email'];
 $message = $_POST['message'];
 
 // Initialize variable for errors
 $error = '';

 // Validation of input data
 if(trim($email) == '')
  $error = 'Invalid email';
 else if(trim($message) == '')
  $error = 'No message';
 else if(strlen($message) < 10)
  $error = 'Message too short';

 // If there are errors, display them and terminate script execution
 if($error != '') {
  echo $error;
  exit;
 }

 // Prepare email subject with UTF-8 encoding
 $subject = "=?utf-8?B?".base64_encode("Website")."?=";

 // Forming headers for sending HTML email
 $headers = "From: $email\r\nReply-to: $email\r\nContent-type: text/html;charset=utf-8\r\n";

 // Sending email to the specified mailbox
 mail('inch.systems@gmail.com', $subject, $message, $headers);

 // Redirect user after successful sending
 header('Location: /portfolio.html');

?>