PHP

How to send emails by Gmail SMTP Emails in PHP

Ravindra Kumar

I’ll show you how to make this Bootstrap form send emails using SMTP.

We’ll do this in PHP with PHPMailer, because:

  • It’s reliable

  • Supports SMTP authentication

  • Works with Gmail, Outlook, and custom mail servers

Install PHPMailer

You can either:

  • Via Composer (recommended)

  • composer require phpmailer/phpmailer

HTML Form


<form action="sendmail.php" method="post" autocomplete="off">
<div class="mb-3">
<label for="name" class="form-label">Full Name</label>
<input type="text" name="name" class="form-control" id="name" required autocomplete="off">
</div>

<div class="mb-3">
<label for="email" class="form-label">Email address</label>
<input type="email" name="email" class="form-control" id="email" required autocomplete="off">
</div>

<div class="mb-3">
<label for="message" class="form-label">Message</label>
<textarea name="message" class="form-control" id="message" rows="3" required></textarea>
</div>

<button type="submit" class="btn btn-primary">Send Email</button>
</form>

PHP: sendmail.php


<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php'; // If using Composer

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$message = htmlspecialchars($_POST['message']);

$mail = new PHPMailer(true);

try {
// SMTP Configuration
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com'; // Change to your SMTP server
$mail->SMTPAuth = true;
$mail->Username = 'your-email@gmail.com'; // Your email
$mail->Password = 'your-app-password'; // App password, not normal password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;

// From & To
$mail->setFrom('your-email@gmail.com', 'Website Contact');
$mail->addAddress('your-email@gmail.com', 'Your Name'); // Where to send

// Email Content
$mail->isHTML(true);
$mail->Subject = 'New Contact Form Submission';
$mail->Body = "
<h3>New Message from Website</h3>
<p><strong>Name:</strong> {$name}</p>
<p><strong>Email:</strong> {$email}</p>
<p><strong>Message:</strong><br>{$message}</p>
";

// Send Email
$mail->send();
echo "Message sent successfully!";
} catch (Exception $e) {
echo "Message could not be sent. Error: {$mail->ErrorInfo}";
}
}

Notes for Gmail SMTP

  • You can’t use your normal Gmail password — you need an App Password.

  • Steps:

    1. Enable 2-Step Verification in Google Account.

    2. Go to Google App Passwords.(https://myaccount.google.com/apppasswords)

    3. Generate one for “Mail” and copy it.

    4. Use it in $mail->Password.

Ravindra Kumar

Ravindra is a passionate full stack developer and dedicated blogger with a flair for crafting user-friendly web applications and insightful articles. With expertise spanning front-end and back-end technologies, Ravindra brings ideas to life through innovative coding solutions.

Suggested Reading

How to Create a Simple Login System in PHP

A login system is one of the most essential features of any website or web application. Whether it’s an e-commerce site, a blogging platform, or an internal company portal, user authentication ensures that only authorized users can access specific areas. In this article, we’ll walk through creating a simple login system in PHP using MySQL. […]

Clear Cache After Logout in PHP | Prevent going to back after logout in PHP

One of the most common issues developers face when building a login system in PHP is users being able to press the back button after logging out and still seeing the dashboard or other protected pages. This happens because browsers often cache pages, so even without an active session, the back button shows the previously […]

How to Create a PHP Registration Form with Gmail SMTP Email Confirmation

Building a secure and user-friendly registration system is one of the most common requirements in web development. In this tutorial, we’ll create a registration form in PHP that: Accepts username, email, and password Validates for unique username and email Stores passwords in a secure hashed format Sends a confirmation email using Gmail SMTP with PHPMailer […]

PHP Login Dashboard Template with Bootstrap Free Download

A secure and stylish login dashboard is one of the most essential components of any web application. Whether you’re building an admin panel, school management system, e-commerce backend, or a personal project, having a professional login system with a responsive dashboard can save you both time and effort. In this article, we’ll discuss how you […]

How to Create Your Own CAPTCHA (Number Addition) with Form in PHP

CAPTCHA (Completely Automated Public Turing test to tell Computers and Humans Apart) is a simple security mechanism used to prevent automated bots from submitting forms on your website. While there are third-party CAPTCHA services like Google reCAPTCHA, creating a basic number-based CAPTCHA using PHP can be an effective and lightweight solution for small projects. In […]

How to increase phpmyadmin import file size in xampp localhost

To increase the PHPMyAdmin import file size in XAMPP on your localhost, follow these steps: Step 1: Modify php.ini Open XAMPP Control Panel. Click Config next to Apache and select PHP (php.ini). Find and update the following values: upload_max_filesize = 100M post_max_size = 100M memory_limit = 256M max_execution_time = 300 max_input_time = 300 (You can […]