Send email using php

Send email using php

HTML Code:

<form action="contactform.php" name="contactForm" method="post" id="contactForm">
  <div class="row form-group">
    <div class="col-md-6">
      <label for="fname">First Name</label>
      <input type="text" id="fname" name="fname" class="form-control" placeholder="Your firstname" required />
    </div>
    <div class="col-md-6">
      <label for="lname">Last Name</label>
      <input type="text" id="lname" name="lname" class="form-control" placeholder="Your lastname" required />
    </div>
  </div>
  <div class="row form-group">
    <div class="col-md-12">
      <label for="email">Email</label>
      <input type="text" id="email" name="email" class="form-control" placeholder="Your email address" required />
    </div>
  </div>
  <div class="row form-group">
    <div class="col-md-12">
      <label for="subject">Subject</label>
      <input type="text" id="subject" name="subject" class="form-control" placeholder="Your subject of this message" required />
    </div>
  </div>
  <div class="row form-group">
    <div class="col-md-12">
      <label for="message">Message</label>
      <textarea name="message" id="message" cols="30" rows="10" class="form-control" placeholder="Say something about your project" required /></textarea>
    </div>
  </div>
  <div class="form-group">
    <input type="submit" value="Send Message" class="btn btn-primary">
  </div>
</form>

PHP Code:

<?php
ini_set("display_errors", 1);
error_reporting(E_ALL);
ini_set("SMTP", "smtp.secureserver.net");
ini_set("smtp_port", 25);
?>
<?php
$to = "demo@demo.com";
$firstname = $_POST["fname"];
$lastname = $_POST["lname"];
$email = $_POST["email"];
$subjects = $_POST["subject"];
$message = $_POST["message"];

$subject = "Inquiry Received";
if ($email != "") {
    function get_client_ip()
    {
        $ipaddress = "";
        if (getenv("HTTP_CLIENT_IP")) {
            $ipaddress = getenv("HTTP_CLIENT_IP");
        } elseif (getenv("HTTP_X_FORWARDED_FOR")) {
            $ipaddress = getenv("HTTP_X_FORWARDED_FOR");
        } elseif (getenv("HTTP_X_FORWARDED")) {
            $ipaddress = getenv("HTTP_X_FORWARDED");
        } elseif (getenv("HTTP_FORWARDED_FOR")) {
            $ipaddress = getenv("HTTP_FORWARDED_FOR");
        } elseif (getenv("HTTP_FORWARDED")) {
            $ipaddress = getenv("HTTP_FORWARDED");
        } elseif (getenv("REMOTE_ADDR")) {
            $ipaddress = getenv("REMOTE_ADDR");
        } else {
            $ipaddress = "UNKNOWN";
        }
        return $ipaddress;
    }
    $IPAddress = get_client_ip();
    $header = "From: $email \r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-type: text/html\r\n";
    $header .= "X-Mailer: PHP \r\n";
    $message = "<table width='900' style='border:1px solid #798eea;' cellpadding='5' cellspacing='0' align='center'><tr><td width='150' style='margin:0; padding:0 0 0 10px; background-color:#798eea; color:#FFFFFF;'>First Name</td><td width='8' style='margin:0; padding:0 0 0 10px; background-color:#798eea; color:#FFFFFF;'>:</td><td style='padding-left:15px; margin:0; color:#000000; border-bottom:1px solid #798eea'>$firstname</td></tr><tr><td width='150' style='margin:0; padding:0 0 0 10px; background-color:#798eea; color:#FFFFFF;'>Last Name</td><td width='8' style='margin:0; padding:0 0 0 10px; background-color:#798eea; color:#FFFFFF;'>:</td><td style='padding-left:15px; margin:0; color:#000000; border-bottom:1px solid #798eea'>$lastname</td></tr><tr><td width='150' style='margin:0; padding:0 0 0 10px; background-color:#798eea; color:#FFFFFF;'>Email</td><td width='8' style='margin:0; padding:0 0 0 10px; background-color:#798eea; color:#FFFFFF;'>:</td><td style='padding-left:15px; margin:0; color:#000000; border-bottom:1px solid #798eea'>$email</td></tr><tr><td width='150' style='margin:0; padding:0 0 0 10px; background-color:#798eea; color:#FFFFFF;'>Subject</td><td width='8' style='margin:0; padding:0 0 0 10px; background-color:#798eea; color:#FFFFFF;'>:</td><td style='padding-left:15px; margin:0; color:#000000; border-bottom:1px solid #798eea'>$subjects</td></tr><tr><td width='150' style='margin:0; padding:0 0 0 10px; background-color:#798eea; color:#FFFFFF;'>Message</td><td width='8' style='margin:0; padding:0 0 0 10px; background-color:#798eea; color:#FFFFFF;'>:</td><td style='padding-left:15px; margin:0; color:#000000; border-bottom:1px solid #798eea'>$message</td></tr></table>";
    if (mail($to, $subject, $message, $header)) {
        echo "<script>alert('Thank you for Applying. We will contact you as a possible');window.location = '/';</script>";
    }
} else {
    header("Location: http://{$_SERVER["SERVER_NAME"]}/");
    die();
}
?>

The mail() function is a built-in PHP function for sending email. It requires a working email server configuration on your server to send the email successfully. Note that the mail() function may not work properly on all systems or configurations.

Leave a Reply

Your email address will not be published. Required fields are marked *

Freelance Website Designer and Developer

Hire Our Website
Designer and Developer

Recent Solutions

Freelance Website Designer and Developer
Indoc Web Design

Copyright © 2023 All rights reserved
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram