Skip to content

PHP – Contact and Email System



<!DOCTYPE html>
<html lang="en">
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" type="text/css" href="./form.css">
  <title>PHP Email</title>

  <form action="result.php" method='post'>
    <div class="container">
    <div class="row">
        <h1>contact us</h1>
    <div class="row">
        <h4 style="text-align:center">We'd love to hear from you!</h4>
    <div class="row input-container">
        <div class="col-xs-12">
          <div class="styled-input wide">
            <input type="text" name='fullname' required />
        <div class="col-md-6 col-sm-12">
          <div class="styled-input">
            <input type="text" name='email' required />
        <div class="col-md-6 col-sm-12">
          <div class="styled-input" style="float:right;">
            <input type="text" name='subject' required />
        <div class="col-xs-12">
          <div class="styled-input wide">
            <textarea name='message' required></textarea>
        <div class="col-xs-12">
          <button type='submit' class="btn-lrg submit-btn">Send Message</button>




header("Content-Type:text/html; charset=UTF-8");

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

require 'PHPMailer/src/Exception.php';
require 'PHPMailer/src/PHPMailer.php';
require 'PHPMailer/src/SMTP.php';

function setFilter($val){
	$step1	=	trim($val);
	$step2	=	strip_tags($step1);
	$step3	=	htmlspecialchars($step2, ENT_QUOTES);
	$result		=	$step3;
		return $result;

$fullname		=	setFilter($_POST["fullname"]);
$email			=	setFilter($_POST["email"]);
$subject		=	setFilter($_POST["subject"]);
$message	  =	setFilter($_POST["message"]);

$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->SMTPDebug = 0;
    // (0): Disable debugging (you can also leave this out completely, 0 is the default).
    // (1): Output messages sent by the client.
    // (2): as 1, plus responses received from the server (this is the most useful setting).
    // (3): as 2, plus more information about the initial connection - this level can help diagnose STARTTLS failures.
    // (4): as 3, plus even lower-level information, very verbose, don't use for debugging SMTP, only low-level problems.

    $mail->Host       = '';
    $mail->CharSet  ="utf-8";
    $mail->SMTPAuth   = true;
    $mail->Username   = '**************';
    $mail->Password   = '*********';
    $mail->SMTPSecure = 'tls';
    $mail->Port       = 587;
    $mail->SMTPOptions		=	array(
      'ssl' => [
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true

    $mail->setFrom('**************', 'PHP informative message');
    $mail->addAddress('**************', 'Answering section');
    $mail->addReplyTo('**************', 'Information');

    // Attachments
    // $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); v 

    // Content
    $mail->Subject = $subject;
    $mail->Body    = "Hello.<br/> My name is $fullname. This is an informative message.";
    $mail->AltBody = 'This is an informative message.';

    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: