- PHP Basics
- PHP Home
- PHP Environment Setup
- PHP Getting Started
- PHP Basic Syntax
- PHP echo
- PHP print
- PHP echo Vs print
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Variable Scope
- PHP gettype()
- PHP Constants
- PHP Operators
- PHP Program Control
- PHP Decision Making
- PHP if-elseif-else
- PHP switch
- PHP Loops
- PHP for Loop
- PHP while Loop
- PHP do-while Loop
- PHP foreach Loop
- PHP break & continue
- PHP Popular Topics
- PHP Arrays
- PHP print_r()
- PHP Strings
- PHP Functions
- PHP References
- PHP Object Oriented
- PHP Object Oriented
- PHP Classes & Objects
- PHP Member Variable
- PHP Member Function
- PHP Encapsulation
- PHP Data Abstraction
- PHP Inheritance
- PHP Constructor Destructor
- PHP Polymorphism
- PHP Web Developments
- PHP Web Developments
- PHP GET & POST
- PHP Read Requested Data
- PHP File Handling (I/O)
- PHP File Handling (I/O)
- PHP fopen() | Open File
- PHP Create a File
- PHP fwrite() | Write to File
- PHP fread() | Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP fclose() | Close File
- PHP unlink() | Delete File
- PHP Append to File
- PHP copy() | Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP rename() | Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP mkdir() | Create Directory
- PHP rmdir() | Remove Directory
- PHP glob() | Get Files/Directories
- PHP basename() | Get filename
- PHP dirname() | Get Path
- PHP filemtime()
- PHP file()
- PHP Advanced
- PHP Cookies
- PHP Sessions
- PHP Send Emails
- PHP Serialization
- PHP Namespaces
- PHP File Upload
- PHP Date and Time
- PHP Image Processing
- PHP Regular Expression
- PHP Predefined Variables
- PHP Error Handling
- PHP Debugging
- PHP and MySQLi Tutorial
- PHP and MySQLi Home
- PHP MySQLi Setup
- PHP MySQLi Create DB
- PHP MySQLi Create Table
- PHP MySQLi Connect to DB
- PHP MySQLi Insert Record
- PHP MySQLi Fetch Record
- PHP MySQLi Update Record
- PHP MySQLi Delete Record
- PHP MySQLi SignUp Page
- PHP MySQLi LogIn Page
- PHP MySQLi Store User Data
- PHP MySQLi Close Connection
- PHP connect_errno
- PHP connect_error
- PHP query()
- PHP fetch_row()
- PHP fetch_assoc()
- PHP fetch_array()
- PHP free_result()
- PHP error
- PHP prepare()
- PHP bind_param()
- PHP execute()
- PHP fetch()
- PHP store_result()
- PHP num_rows
- PHP bind_result()
- PHP get_result()
- PHP mysqli_result Class
- PHP Error Constants
- PHP mysqli_driver()
- PHP Misc
- PHP error_reporting()
- PHP Escape Special Characters
- PHP htmlspecialchars()
- PHP new
- PHP header()
- PHP getallheaders()
- PHP empty()
- PHP isset()
- PHP unset()
- PHP exit()
- PHP exit Vs break
- PHP include()
- PHP require()
- PHP include() Vs require()
- PHP AJAX & XML
- PHP AJAX
- PHP XML
- PHP File Handling Functions
- PHP abs()
- PHP Test
- PHP Online Test
- Give Online Test
- All Test List
PHP Send Emails
Here you will learn about how you can easily send email using PHP. You can also use PHP to send bulk email at once.
What to do Before Sending Email using PHP
Before sending any email using PHP, firstly you must have to setup your environment so that you can send email using PHP without any distruption.
Let's see how to setup environment for XAMPP users to send email from localhost using PHP.
PHP XAMPP Setup to Send Email
Using PHP, you are free to send email from your system with the help of XAMPP.
But before sending email using XAMPP from your computer system, you have to open the php.ini and sendmail.ini file and make some changes.
You can find php.ini file in the directory C:\xampp\php\ and sendmail.ini file in the directory C:\xampp\sendmail\.
Let's first edit the php.ini file as per the instruction given below.
Search for SMTP, smtp_port, sendmail_from, and sendmail_path, and then initialize smtp.gmail.com, 587, codescracker@gmail.com, and "\"C:\xampp\sendmail\sendmail.exe\" -t" to these as shown in the following code:
SMTP=smtp.gmail.com smtp_port=587 sendmail_from=codescracker@gmail.com sendmail_path="\"C:\xampp\sendmail\sendmail.exe\" -t"
Now let's edit the file named sendmail.ini as per the instruction given below:
This time search for smtp_server, smtp_port, error_logfile, debug_logfile, auth_username, auth_password, and force_sender, and then initialize smtp.gmail.com, 587, error.log, debug.log, codescracker@gmail.com, mypassword, and codescracker@gmail.com to these as shown in the following sample code:
smtp_server=smtp.gmail.com smtp_port=587 error_logfile=error.log debug_logfile=debug.log auth_username=codescracker@gmail.com auth_password=mypassword force_sender=codescracker@gmail.com
You only have to replace codescracker@gmail.com with your own email id (this email id will be used to send the emails to anyone) and mypassword with your own password.
After doing all the settings that are provided above, you have to restart your XAMPP server then you can go further to write your email sending code using PHP to start sending email using the code. Let's see how to do this.
Note - If you will find some error after successfully setup according to the above instructions then you can easily find and fix the error using the file present in the directory C:\xampp\sendmail\. In this directory search for the file named error.txt and open that file to find and fix any of your error.
PHP Send Email Example
Let's take an example to understand how to write code to send emails using PHP.
<?php // below variable contains the email address of the person // who will get the email using this example of sending email $sendEmailTo = "codescrackerParther@gmail.com"; // below variable contains the subject value $sendEmailSubject = "Hello partner, greeting email for you"; // now put the message in a php variable $sendEmailMessage = "How are you my partner"; ?> <html> <head> <title>Sending Emails using PHP Example - codescracker.com</title> </head> <body> <?php // now set the content-type before sending html email $headers = "MIME-Version: 1.0 \r\n"; $headers = $headers . "Content-type:text/html;charset=UTF-8 \r\n"; $headers = $headers . "From:codescracker@gmail.com \r\n"; $checkMail = mail($sendEmailTo, $sendEmailSubject, $sendEmailMessage, $headers); if($checkMail == true) { echo "Your email sent successfully to your friend."; exit(); } else { echo "Error occurred while sending the email, try again"; echo "<br/>exiting..."; exit(); } ?> </body> </html>
Here is the sample output produced by the above email sending example using PHP after sending the email successfully.

And here is the output you will see if some error occurred while sending the email using the above email sending example:

« Previous Tutorial Next Tutorial »