All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

PHP Code To Send Email From Contact Form

Last Updated : Mar 11, 2024

PHP Code To Send Email From Contact Form

In this article we will show you the solution of PHP code to send email from contact form, by using, php we can send a contact form to an email id.

To send an email we used an SMTP or mail server. We used a much easier method php mailer package in this tutorial.

To install phpmailer we’ll use composer on the command line.

This will install the package in the vendor folder the same folder as the php file.

Step By Step Guide On PHP Code To Send Email From Contact Form :-

First, let’s see the code for the HTML contact form:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title> PHP code to send email from contact form </title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
</head>
<body>
    <h1 style=" color : rgb(113, 221, 113) ;"> TALKERSCODE </h1>
    <h2> PHP code to send email from contact form </h2>
    <h3> CONTACT FORM: </h3>
    <form method="post" action="send-email.php">
        <label for="name"> Name </label>
        <input type="text" name="name" id="name" required>
        <label for="email"> Email </label>
        <input type="email" name="email" id="email" required>
        <label for="subject"> Subject </label>
        <input type="text" name="subject" id="subject" required>
        <label for="message"> Message </label>
        <textarea name="message" id="message" cols="30" rows="10"></textarea>
        <br>
        <button> SEND </button>
    </form>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now the <head> tag is used to contain information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
  4. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  5. Created the <style> tag to add an external CSS to the HTML page.
  6. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  7. Create a form using <form> tag with an action of the php file.
  8. By using the <input> tag add an input name, email, subject, and message port.
  9. Create a <button> with tag SEND.
  10. Now we close the HTML file with </html> tag

Now let us see the PHP code send-email.php:

<?php
$name = $_POST["name"];
$email = $_POST["email"];
$subject = $_POST["subject"];
$message = $_POST["message"];
require "vendor/autoload.php";
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->SMTPAuth = true;
$mail->Host = "smtp.example.com";
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
$mail->Username = "you@example.com";
$mail->Password = "password";
$mail->setFrom($email, $name);
$mail->addAddress("talkerscode@example.com", "talkerscode");
$mail->Subject = $subject;
$mail->Body = $message;
$mail->send();
echo “email sent”;
?>
  1. we write <?php tag to write PHP within it.
  2. Create the variables used in the form with $_POST method.
  3. Access the vendor folder
  4. We create an instance of php mailer class passing in true as the first argument.
  5. Calling the SMTP method and setting SMPTAuth method to true.
  6. We are using the Gmail SMTP server set the host and port as Gmail.
  7. Set the username and password.
  8. Set the from address and add the address.
  9. Then send() method to send the email
  10. Then use an echo to see the confirmation message.

Conclusion :-

At last, here in conclusion, here we can say that with this article’s help, we know to send email from a contact form using PHP.

I hope this article on PHP code to send email from contact form helps you and the steps and method mentioned above are easy to follow and implement.