PHP Mail PHP

PHP Mail  

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

  1. 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

  1. <?php  
  2.    ini_set("sendmail_from", "sonoojaiswal@javatpoint.com");  
  3.    $to = "sonoojaiswal1987@gmail.com";//change receiver address  
  4.    $subject = "This is subject";  
  5.    $message = "This is simple text message.";  
  6.    $header = "From:sonoojaiswal@javatpoint.com \r\n";  
  7.   
  8.    $result = mail ($to,$subject,$message,$header);  
  9.   
  10.    if( $result == true ){  
  11.       echo "Message sent successfully...";  
  12.    }else{  
  13.       echo "Sorry, unable to send mail...";  
  14.    }  
  15. ?>  

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.

  1. <?php  
  2.    $to = "abc@example.com";//change receiver address  
  3.    $subject = "This is subject";  
  4.    $message = "<h1>This is HTML heading</h1>";  
  5.   
  6.    $header = "From:xyz@example.com \r\n";  
  7.    $header .= "MIME-Version: 1.0 \r\n";  
  8.    $header .= "Content-type: text/html;charset=UTF-8 \r\n";  
  9.   
  10.    $result = mail ($to,$subject,$message,$header);  
  11.   
  12.    if( $result == true ){  
  13.       echo "Message sent successfully...";  
  14.    }else{  
  15.       echo "Sorry, unable to send mail...";  
  16.    }  
  17. ?>  

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.

  1. <?php  
  2.   $to = "abc@example.com";  
  3.   $subject = "This is subject";  
  4.   $message = "This is a text message.";  
  5.   # Open a file  
  6.   $file = fopen("/tmp/test.txt", "r" );//change your file location  
  7.   if( $file == false )  
  8.   {  
  9.      echo "Error in opening file";  
  10.      exit();  
  11.   }  
  12.   # Read the file into a variable  
  13.   $size = filesize("/tmp/test.txt");  
  14.   $content = fread( $file, $size);  
  15.   
  16.   # encode the data for safe transit  
  17.   # and insert \r\n after every 76 chars.  
  18.   $encoded_content = chunk_split( base64_encode($content));  
  19.     
  20.   # Get a random 32 bit number using time() as seed.  
  21.   $num = md5( time() );  
  22.   
  23.   # Define the main headers.  
  24.   $header = "From:xyz@example.com\r\n";  
  25.   $header .= "MIME-Version: 1.0\r\n";  
  26.   $header .= "Content-Type: multipart/mixed; ";  
  27.   $header .= "boundary=$num\r\n";  
  28.   $header .= "--$num\r\n";  
  29.   
  30.   # Define the message section  
  31.   $header .= "Content-Type: text/plain\r\n";  
  32.   $header .= "Content-Transfer-Encoding:8bit\r\n\n";  
  33.   $header .= "$message\r\n";  
  34.   $header .= "--$num\r\n";  
  35.   
  36.   # Define the attachment section  
  37.   $header .= "Content-Type:  multipart/mixed; ";  
  38.   $header .= "name=\"test.txt\"\r\n";  
  39.   $header .= "Content-Transfer-Encoding:base64\r\n";  
  40.   $header .= "Content-Disposition:attachment; ";  
  41.   $header .= "filename=\"test.txt\"\r\n\n";  
  42.   $header .= "$encoded_content\r\n";  
  43.   $header .= "--$num--";  
  44.   
  45.   # Send email now  
  46.   $result = mail ( $to, $subject, "", $header );  
  47.   if( $result == true ){  
  48.       echo "Message sent successfully...";  
  49.    }else{  
  50.       echo "Sorry, unable to send mail...";  
  51.    }  
  52. ?>  

Download free E-book of PHP


#askProgrammers
Learn Programming for Free


Join Programmers Community on Telegram


Talk with Experienced Programmers


Just drop a message, we will solve your queries