PHP Mail PHP
- PHP Tutorial
- What is PHP
- Install PHP
- PHP Example
- PHP Echo
- PHP Print
- PHP Variables
- PHP $ and $$ Variables
- PHP Constants
- Magic Constants
- PHP Data Types
- PHP Operators
- PHP Comments
- PHP Boolean
- PHP is_bool() function
- PHP Integer
- PHP is_int() function
- PHP Float
- PHP is_float Function
- PHP Compound Types
- PHP Special Types
- PHP is_null() function
- PHP If Else
- PHP Switch
- PHP For Loop
- PHP While Loop
- PHP do while loop
- PHP Break
- PHP Functions
- PHP Parameterized Function
- PHP Call By Value
- PHP Call By Reference
- PHP Default Argument Values Function
- PHP Variable Length Argument Function
- PHP Recursive Function
- PHP Arrays
- PHP Indexed Array
- PHP Associative Array
- PHP Multidimensional Array
- PHP Array Functions
- PHP String
- PHP String Functions
- PHP Math functions
- PHP Form Handling | GET and POST method
- PHP Include File
- PHP Cookie
- PHP Session
- PHP File Handling
- PHP Open File
- PHP Read File
- PHP Write File
- PHP Append to File
- PHP Delete File
- PHP File Upload
- PHP Download File
- PHP Mail
- PHP MySQL Connect
- PHP MySQL Create Database
- PHP MySQL Create Table Example
- PHP MySQL Insert Record
- PHP Prepared Statements
- PHP MySQL Update Record
- PHP MySQL Delete Record
- PHP MySQL Select
- PHP MySQL Order By
- PHP Limit Data MySql
- PHP JSON
- PHP XML Parsers
- PHP SimpleXML Parser
- PHP SimpleXML - Get Node/Attribute Values
- PHP XML Expat Parser
- PHP XML DOM Parser
- PHP - AJAX intro
- MySQL CREATE VIEW
PHP Mail
PHP Mail
PHP mail() function is used to send email in PHP. You can send text message, html message and attachment with message using PHP mail() function.
PHP mail() function
Syntax
- bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
$to: specifies receiver or receivers of the mail. The receiver must be specified one of the following forms.
- user@example.com
- user@example.com, anotheruser@example.com
- User <user@example.com>
- User <user@example.com>, Another User <anotheruser@example.com>
$subject: represents subject of the mail.
$message: represents message of the mail to be sent.
Note: Each line of the message should be separated with a CRLF ( \r\n ) and lines should not be larger than 70 characters.
$additional_headers (optional): specifies the additional headers such as From, CC, BCC etc. Extra additional headers should also be separated with CRLF ( \r\n ).
PHP Mail Example
File: mailer.php
- <?php
- ini_set("sendmail_from", "sonoojaiswal@javatpoint.com");
- $to = "sonoojaiswal1987@gmail.com";//change receiver address
- $subject = "This is subject";
- $message = "This is simple text message.";
- $header = "From:sonoojaiswal@javatpoint.com \r\n";
- $result = mail ($to,$subject,$message,$header);
- if( $result == true ){
- echo "Message sent successfully...";
- }else{
- echo "Sorry, unable to send mail...";
- }
- ?>
If you run this code on the live server, it will send an email to the specified receiver.
PHP Mail: Send HTML Message
To send HTML message, you need to mention Content-type text/html in the message header.
- <?php
- $to = "abc@example.com";//change receiver address
- $subject = "This is subject";
- $message = "<h1>This is HTML heading</h1>";
- $header = "From:xyz@example.com \r\n";
- $header .= "MIME-Version: 1.0 \r\n";
- $header .= "Content-type: text/html;charset=UTF-8 \r\n";
- $result = mail ($to,$subject,$message,$header);
- if( $result == true ){
- echo "Message sent successfully...";
- }else{
- echo "Sorry, unable to send mail...";
- }
- ?>
PHP Mail: Send Mail with Attachment
To send message with attachment, you need to mention many header information which is used in the example given below.
- <?php
- $to = "abc@example.com";
- $subject = "This is subject";
- $message = "This is a text message.";
- # Open a file
- $file = fopen("/tmp/test.txt", "r" );//change your file location
- if( $file == false )
- {
- echo "Error in opening file";
- exit();
- }
- # Read the file into a variable
- $size = filesize("/tmp/test.txt");
- $content = fread( $file, $size);
- # encode the data for safe transit
- # and insert \r\n after every 76 chars.
- $encoded_content = chunk_split( base64_encode($content));
- # Get a random 32 bit number using time() as seed.
- $num = md5( time() );
- # Define the main headers.
- $header = "From:xyz@example.com\r\n";
- $header .= "MIME-Version: 1.0\r\n";
- $header .= "Content-Type: multipart/mixed; ";
- $header .= "boundary=$num\r\n";
- $header .= "--$num\r\n";
- # Define the message section
- $header .= "Content-Type: text/plain\r\n";
- $header .= "Content-Transfer-Encoding:8bit\r\n\n";
- $header .= "$message\r\n";
- $header .= "--$num\r\n";
- # Define the attachment section
- $header .= "Content-Type: multipart/mixed; ";
- $header .= "name=\"test.txt\"\r\n";
- $header .= "Content-Transfer-Encoding:base64\r\n";
- $header .= "Content-Disposition:attachment; ";
- $header .= "filename=\"test.txt\"\r\n\n";
- $header .= "$encoded_content\r\n";
- $header .= "--$num--";
- # Send email now
- $result = mail ( $to, $subject, "", $header );
- if( $result == true ){
- echo "Message sent successfully...";
- }else{
- echo "Sorry, unable to send mail...";
- }
- ?>