Skip to content

PHP – Contact and Email System

Github: https://github.com/Furkan-Gulsen/PHP-Projects/tree/master/PHP%20Send%20Mail


index.php

<!DOCTYPE html>
<html lang="en">
<head>
  <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>
</head>
<body>

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

  </form>
  
</body>
</html>

result.php

<?php

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->isSMTP();
    $mail->Host       = 'smtp.gmail.com';
    $mail->CharSet  ="utf-8";
    $mail->SMTPAuth   = true;
    $mail->Username   = '**************@gmail.com';
    $mail->Password   = '*********';
    $mail->SMTPSecure = 'tls';
    $mail->Port       = 587;
    $mail->SMTPOptions		=	array(
      'ssl' => [
        'verify_peer' => false,
        'verify_peer_name' => false,
        'allow_self_signed' => true
      ],
    );

    //Recipients
    $mail->setFrom('**************@gmail.com', 'PHP informative message');
    $mail->addAddress('**************@gmail.com', 'Answering section');
    $mail->addReplyTo('**************@gmail.com', 'Information');

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

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

    $mail->send();
    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:

WordPress.com Logo

You are commenting using your WordPress.com 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: