LERAN PHP COMPLETE

Learn one of the most powerful programming languages in the world and become a rockstar developer.

PHP Sending Emails


PHP makes use of mail() function to send an email. This function requires three mandatory arguments that specify the recipient's email address, the subject of the the message and the actual message additionally there are other two optional parameters.

Here is the description for each parameters.

mail(to,subject,message,headers,parameters);
Sr.No Parameter & Description
1

to

Required. Specifies the receiver / receivers of the email

2

subject

Required. Specifies the subject of the email. This parameter cannot contain any newline characters

3

message

Required. Defines the message to be sent. Each line should be separated with a LF (\n). Lines should not exceed 70 characters

4

headers

Optional. Specifies additional headers, like From, Cc, and Bcc. The additional headers should be separated with a CRLF (\r\n)

5

parameters

Optional. Specifies an additional parameter to the send mail program

<?php
    $msg = "First line of text\nSecond line of text";
    // use wordwrap() if lines are longer than 70 characters
    $msg = wordwrap($msg,70);
                            
    // send email
    mail("someone@example.com","My subject",$msg);
?>
								  
							  

Send an email with extra headers:

<?php
    $to = "somebody@example.com";
    $subject = "My subject";
    $txt = "Hello world!";
    
    $headers = "From: webmaster@example.com" . "\r\n" .
    "CC: somebodyelse@example.com";
    
    mail($to,$subject,$txt,$headers);
?>

Send HTML email

<?php
    $to = "somebody@example.com, somebodyelse@example.com";
    $subject = "HTML email";
    $message = "<html>
            <head>
                <title>HTML email</title>
            </head>
            <body>
                <p>This email contains HTML Tags!</p>
                <table>
                    <tr>
                        <th>Firstname</th>
                        <th>Lastname</th>
                    </tr>
                    <tr>
                        <td>John</td>
                        <td>Doe</td>
                    </tr>
                </table>
            </body>
        </html>";
    
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
    $headers .= 'From: <webmaster@example.com>' . "\r\n";
    $headers .= 'Cc: myboss@example.com' . "\r\n";
    mail($to,$subject,$message,$headers);
?>

x
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Privacy Policy That's Fine