How to send email using PHP mail() function?
Sending email using PHP is very easy then you can think. There is already have an inbuilt PHP mail() function that can be used to send emails from a web server.
On many websites and online portal, you will find sending email for several purposes. It can be sending registration confirmation on a website or can send an activation link via email to activate your account after you register on a website and in many other purposes.
Below is given the syntax of mail function:
$to, $subject and $message are required. Without these three parameters, you can not send the email. The other two are email headers and email parameters which are optional.
$to specifies the receiver email. You can specify multiple emails to send email to multiple receivers.
$subject specifies the email subject.
The email headers Specify additional headers, like From, Cc, and Bcc. The additional headers always should be separated with a CRLF (\r\n).
$parameters can be used when it needs to send an additional parameter to the sendmail program.
The $message specifies the message content. It can be text and also it can contain HTML for HTML type email.
Below is a simple example which will send an email:
<?php // the message text for the email that will be sent $message = "This is first line of text\nThis is second line of text\nThis is third line of text"; // using the wordwrap() PHP function if lines are longer than 60 characters $message = wordwrap($message,60); // send email mail("[email protected]","My email subject",$message); ?>
The above example contains the minimum required values to send an email from the server. As you can see that it is so simple and easy. Isn’t it? What do you think about this?
Now below is another example which contains the headers. The below example includes the From and CC email:
<?php $to = "[email protected]"; $subject = "My Email subject"; $message = "This is the message text"; // Set the From and CC email $headers = "From: [email protected]" . "\r\n" . "CC: [email protected]"; // Sending the email using mail function mail($to,$subject,$message,$headers); ?>
You can also send HTML email. To do it, you need these two headers code for sending HTML email:
$headers = "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
Below is given the PHP code snippets that can send HTML email:
<?php $to = "[email protected], [email protected]"; $subject = "HTML email testing"; $message = " <html> <head> <title>HTML email for testing</title> </head> <body> <p>This is an HTML email</p> <table> <tr> <th>Name</th> <th>Email</th> </tr> <tr> <td>Mollick</td> <td>[email protected]</td> </tr> </table> </body> </html> "; // Should set content-type when sending a HTML email $headers = "MIME-Version: 1.0" . "\r\n"; $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n"; // More headers $headers .= 'From: <[email protected]>' . "\r\n"; $headers .= 'Cc: [email protected]' . "\r\n"; mail($to,$subject,$message,$headers); ?>
The above email will send HTML email to multiple receivers. You just need to separate the receiver emails with comma signs.
I have discussed in this tutorial how to send both text and HTML email. I hope you can also do it with yourself.
I hope you like this tutorial. So you have seen that sending email using PHP mail() function is incredibly easy