As the name suggests, the function called mail() is used to send e-mails to the specified locations. The functionality of mail() is coded within a script and, in general, it contains a message that needs to be sent and a function that allows the message to be sent to a specified location or locations.

Syntax for mail() function 

mail ($to, $subject, $message, [, $additional_headers [, $additional_parameters]]);

Where:

  • $to contains the email address of the recipient.
  • $subject contains the subject of the message.
  • $message is the actual message.
  • $additional_headers are extra header to be added in the end of the message including (From, Cc and Bcc)
  • $additional_parameters are the parameters used to pass additional flags as command line option to the program configured to be used when sending mail.
NOTE: All variables found inside the mail() function have to be strings.

Simple example with mail()

<?php
    $message = "1st line <br />2nd line <br />3rd line";
    $message = wordwrap($message, 100, "<br />"); // wraps message if it's longer than 70 characters
    mail ('brenko.web@gmail.com', 'Subject: test email', $message);
?>

Simple example with mail() and additional headers

<?php
    $message = "1st line <br />2nd line <br />3rd line";
    $message = wordwrap($message, 100, "<br />"); // wraps message if it's longer than 70 characters
    $headers = "From: someone@gmail.com" . "<br />" . "RE: someone@gmail.com" . "<br />" . "Please reply...";
    
    mail ('brenko.web@gmail.com', 'Subject: test email', $message);
?>

Example of sending an HTML email with a memo

<?php
    $to = "someone@gmail.com";
    $message = "
    <html>
    <head>
        <title>Congratulations</title>
    </head>
    <body>
        <h1>We inform you that You have one lottery!</h1>
        <p>$nbsp;</p>
        <p>Please send us a check so we can ship you the lottery money.</p>
        <p>p.s. This is not a spam.</p>
    </body>
    ";
    $headers .= "From: FriendFromNigeria@yahoo.com<br />";
    
    mail ($to, "Subject", $message, $headers);
?>

 

›› go to examples ››