How to Send Email in PHP with Example (Gmail SMTP + PHP Mailer 2026)


How to send email in PHP with example

Hi friends, in this tutorial you will learn how to send email in PHP with the help of the mail() function. This function is used to send emails inside the PHP script. Email sending is a very common feature of any kind of web application and we must know it. Now, I am going to explain with an example using an HTML form as shown below

How to send email in PHP with example

Send email in PHP using HTML form inputs

Before sending the emails, we must know the syntax of the mail() function as shown below

mail($to,$subject, $message, $headers)

whereas
$to = The email address of the receiver to whom the email will be sent.
$subject = The subject of the email (You can specify it directly in the PHP script or you can take the subject from the HTML input field)

$message = It defines the body text of the email ( You can specify directly in the PHP script or you can take the message text from the HTML input form)

$headers = In the headers section, there are multiple headers but you must set the From header. You can add an additional header like CC, BCC, etc. The headers should be separated with (r\n).

HTML Code:- (sendmail.html)

<!DOCTYPE html>
<html lang="en">
<head>
  <title></title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container" style="width: 40%;">
  <br><br>
  <h3>Send Email in PHP</h3>
  <form method="post" action="send.php">
    <div class="form-group">
      <label for="email">Subject:</label>
      <input type="text" name="subject" class="form-control form-control-sm">
    </div>
    <div class="form-group">
      <label for="email">Email address:</label>
      <input type="email" class="form-control form-control-sm" name="email">
    </div>
    <div class="form-group">
      <label for="email">Message:</label>
      <textarea name="message" class="form-control form-control-sm" rows="5"></textarea>
    </div>
    <div class="form-group">
      <button type="submit" class="btn btn-primary" name="send">Send</button>
    </div>
  </form>
</div>
</body>
</html>

PHP Code:- (send.php)

<?php
if(isset($_POST['send']))
{
	$subject = $_POST['subject'];
	$email = $_POST['email'];
	$message = $_POST['message'];

	//set the email
    $headers = "From: Sender Address senderemail\r\n";
    $headers .= "Reply-To: senderemail\r\n";
    $headers .= "Return-Path: senderemail\r\n";
    
    //send the email

    if(mail($to, $subject, $message, $headers)){
        echo 'Email has been sent';
    }
    else{
        echo 'Email could not send';
    }
}
?>

Note:- mail() function will not work on your local server. It only works in a live server or shared hosting server.

Also read, How to send mail in laravel 8 using SMTP Gmail

Conclusion:- I hope this tutorial will help you to understand the concept of email sending. If there is any doubt then please leave a comment below.


Leave a Comment

parlay

RATU311

KUPU178

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

KUPU178

KUPU178

RATU311

RATU311

RATU311

CERI188

CERI188

CERI188

KUPU178

RATU311: Alternatif karnpuracollege

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

KUPU178

RATU311

RATU311

RATU311

judi bola

live casino

sv388

sv388

sabung ayam online

sabung ayam online

Thor311 Terbaru

CERI188: Taruhan Bola Sbobet

THOR311 DEMO SLOT

THOR311 MAHJONG

RATU311: BONUS SABUNG AYAM

CERI188

parlay

RATU311

KUPU178

RATU311

THOR311

RATU311

RATU311

RATU311

RATU311

RATU311

CERI188

CERI188: Masuk Akun Slot Super Cepat

KUPU178: Slot Online Mahjong Bet 200

THOR311 PRO

THOR311

CERI188

CERI188

CERI188

CERI188

CERI188

CERI188

CERI188

CERI188

CERI188

THOR311

HOKI311

HOKI311

RATU311

RATU311

KUPU178

THOR311

KUPU178

THOR311

RATU311

HOKI311

KUPU178

KUPU178

RATU311

THOR311 TOTO4D

KUPU178

Live Casino Roullate

RATU311

RATU311

RATU311

RATU311

THOR311

THOR311

THOR311

RATU311

CERI188

DAFTAR KUPU178

LINK DAFTAR CERI188

CERI188

APK KUPU178

KUPU178

CERI188

CERI188

RATU311

KUPU178

THOR311 DOMINO QQ

judi bola online

Mix Parlay

CERI188

CERI188

CERI188

RATU311

live casino online

RATU311

Mix Parlay

Sabung Ayam Online

Slot PG Soft

CERI188

RATU311

RATU311

CERI188

THOR311

CERI188

THOR311

Game Thor311

THOR311

HOKI311

live casino

sv388

HOKI311

HOKI311

slot mahjong

Thor311 Alternatif

RATU311

HOKI311

HOKI311

HOKI311

HOKI311

CERI188

THOR311 RESMI

THOR311 AKSES

THOR311

THOR311 RTP

THOR311

parlay

HOKI311

THOR311

Thor311 Aplikasi

RATU311 NONTON BOLA

CERI188 Link RTP LIVE

THOR311

Thor311 Live Casino

CERI188

akses VIP THOR311

THOR311

link daftar THOR311

THOR311

situs thor311

RATU311

HOKI311 Link SABUNG AYAM RESMI

HOKI311 Link QRIS EWALLET

RATU311

LINK CERI188

parlay

HOKI311 Link RTP Gacor

THOR311

HOKI311 Link Apk Resmi

CERI188

KUPU178 Link Daftar

CERI188

CERI188

CERI188

CERI188

CERI188

HOKI311

RATU311

RATU311

HOKI311 Lapak Arena Slot

RATU311 judi bola

HOKI311 Link Spaceman Online

HOKI311 Link Daftar Resmi

CERI188 Toto SGP Online

KUPU178

HOKI311 TEMBAK IKAN ONLINE

KUPU178

HOKI311 Link Login Alternatif

HOKI311 Live Casino Online

HOKI311 Situs Slot Gacor

THOR311

THOR311

situs judi bola

Ceri188 Game Online

CERI188

link login CERI188

link login CERI188

CERI188

THOR311

HOKI311

HOKI311

CERI188

parlay

THOR311

RATU311

HOKI311

HOKI311

HOKI311

RATU311

THOR311

KUPU178

HOKI311

KUPU178

KUPU178

HOKI311

parlay

slot gacor

kupu178 link alternatif

kupu178 slot

kupu178 daftar

kupu178 login

link daftar kupu178

judi bola

HOKI311

RATU311

link alternatif RATU311

Link daftar RATU311

link login RATU311

RATU311

RATU311

KUPU178

KUPU178

THOR311

KUPU178

KUPU178

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

RATU311

THOR311

KUPU178

KUPU178

HOKI311

THOR311

KUPU178

THOR311

THOR311

slot thailand

judi bola

THOR311

THOR311

KUPU178

THOR311

THOR311

slot gacor

slot gacor

yakuza303

unobet77

slot gacor

slot gacor

RATU311

parlay

parlay

slot gacor

parlay

THOR311