• Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us
TecAdmin
Menu
  • Home
  • Ubuntu 18.04
    • Whats New?
    • Upgrade Ubuntu
    • Install Java
    • Install Node.js
    • Install Docker
    • Install Git
    • Install LAMP Stack
  • Tutorials
    • AWS
    • Shell Scripting
    • Docker
    • Git
    • MongoDB
  • Funny Tools
  • FeedBack
  • Submit Article
  • About Us

A Sample PHP Scripts for Sending Emails

Written by Rahul, Updated on February 21, 2019

PHP (Hypertext Preprocessor) is an easier programming language used for faster development. In this article, we will learn, How to Send Email using PHP Script.

1. Send Email using PHP

Lets create a php file sendEmail.php in your web document root with following content. Change the $to_email with your recipient email address, $subject and $body as per your need, Keep as it is for testing purpose, $from_email with sender email address.

vim /var/www/html/sendEmail.php

Add below content

1
2
3
4
5
6
7
8
9
10
11
12
<?php
   $to_email = "[email protected]";
   $subject = "Simple Email Test via PHP";
   $body = "Hi,nn This is test email send by PHP Script";
   $headers = "From: [email protected]";
 
   if ( mail($to_email, $subject, $body, $headers)) {
      echo("Email successfully sent to $to_email...");
   } else {
      echo("Email sending failed...");
   }
?>

Now we can test it by access in a web browser or simply execute it from the command line like below.

php sendEmail.php

2. Send Email using PHP with HTML Form

Lets create a PHP script file sendEmail.php in your web document root of your domain with following content. This will show a simple form in the browser, using that we can also send an email for testing purpose.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?php
if (!isset($_POST["submit"])) { ?>
 
  <form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
  To: <input type="text" name="to_email"><br>
  From: <input type="text" name="from_email"><br>
  Subject: <input type="text" name="subject"><br>
  Message: <textarea rows="10" cols="20" name="message"></textarea><br>
  <input type="submit" name="submit" value="Send Email">
  </form>
<?php
} else {
 
  if (isset($_POST["to_email"])) {
    $to_email = $_POST["to_email"];
    $from_email = $_POST["from_email"];
    $subject = $_POST["subject"];
    $body = $_POST["message"];
 
    if ( mail($to_email, $subject, $body, $headers)) {
      echo("Email successfully sent to $to_email...");
    } else {
      echo("Email sending failed...");
    }
  }
}
?>

Now Access in web browser and fill the all fields and hit Send Email button to finally send the email.

 http://localhost/sendEmail.php

For more details visit php official website for details about mail function.

Share it!
Share on Facebook
Share on Twitter
Share on LinkedIn
Share on Reddit
Share on Tumblr
Share on Whatsapp
Rahul
Rahul
Connect on Facebook Connect on Twitter

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

7 Comments

  1. Avatar Sourav Das Reply
    October 2, 2019 at 5:24 pm

    how to send mail from localhost without using FTP server in php

  2. Avatar Takinmall Reply
    September 15, 2019 at 10:03 am

    Thanks please post how to send email using google SMTP.

  3. Avatar Frank Reply
    September 12, 2019 at 8:17 am

    thanks, it really worked

  4. Avatar Ayo Celestine Reply
    August 21, 2019 at 7:41 pm

    Thanks for this script. It works fine

  5. Avatar Zubair shah Reply
    June 27, 2019 at 4:07 pm

    thanks for the sugestion

  6. Avatar Raj Reply
    June 16, 2019 at 7:28 pm

    Thanks please post how to send email using google SMTP.

  7. Avatar Noel Springer Reply
    July 24, 2018 at 1:33 am

    Hi Rahul,

    Should the line endings in the script be “\r\n” ?

    Also, on Debian 9 with php-mail installed to provide PEAR’s Mail:: package how would this be used to send SMTP email?

    Cheers,
    Noel

Leave a Reply Cancel reply

Popular Posts

  • How to Install Python 3.9 on CentOS/RHEL 7 & Fedora 32/31 0
  • How To Install VNC Server on Ubuntu 20.04 1
  • How To Install NVM on macOS with Homebrew 0
  • (Solved) apt-add-repository command not found – Ubuntu & Debian 0
  • How to Install .NET Core on Debian 10 0
© 2013-2020 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy