Open In App

PHP | Sending mails using mail() function

PHP is a server side scripting language that is enriched with various utilities required. Mailing is one of the server side utilities that is required in most of the web servers today. Mailing is used for advertisement, account recovery, subscription etc.

In order to send mails in PHP, one can use the mail() method.



Syntax:

bool mail(to , subject , message , additional_headers , additional_parameters)

Parameters: The function has two required parameters and one optional parameter as described below:



Return Type: This method returns TRUE if mail was sent successfully and FALSE on Failure.

Examples:

  1. Sending a Simple Mail in PHP




    <?php
      $to = "recipient@example.com";
      $sub = "Generic Mail";
      $msg="Hello Geek! This is a generic email.";
      if (mail($to,$sub,$msg))
          echo "Your Mail is sent successfully.";
      else
          echo "Your Mail is not sent. Try Again.";
    ?> 
    
    

    Output :

    Your Mail is sent successfully.
    
  2. Sending a Mail with Additional Options




    <?php
      $to = "recipient@example.com";
      $sub = "Generic Mail";
      $msg = "Hello Geek! This is a generic email.";
      $headers = 'From: sender@example.com' . "\r\n" .'CC: another@example.com';
      if(mail($to,$sub,$msg,$headers))
          echo "Your Mail is sent successfully.";
      else
          echo "Your Mail is not sent. Try Again.";
    ?> 
    
    

    Output :

    Your Mail is sent successfully.
    

Summary:


Article Tags :