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:
- to: Specifies the email id of the recipient(s). Multiple email ids can be passed using commas
- subject: Specifies the subject of the mail.
- message: Specifies the message to be sent.
- additional-headers(Optional): This is an optional parameter that can create multiple header elements such as From (Specifies the sender), CC (Specifies the CC/Carbon Copy recipients), BCC (Specifies the BCC/Blind Carbon Copy Recipients. Note: In order to add multiple header parameters one must use ‘\r\n’.
- additional-parameters(Optional): This is another optional parameter and can be passed as an extension to the additional headers. This can specify a set of flags that are used as the sendmail_path configuration settings.
Return Type: This method returns TRUE if mail was sent successfully and FALSE on Failure.
Examples:
- 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.";elseecho"Your Mail is not sent. Try Again.";?>chevron_rightfilter_noneOutput :
Your Mail is sent successfully.
- 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.";elseecho"Your Mail is not sent. Try Again.";?>chevron_rightfilter_noneOutput :
Your Mail is sent successfully.
Summary:
- Using mail() method one can send various types of mails such as standards, html mail.
- The mail() method opens the SMTP socket, attempts to send the mail, closes the socket thus is a secure option.
- mail() method should not be used for bulk mailing as it is not very cost-efficient.
- The mail() method only checks for parameter or network failure, thus a success in the mail() method doesn’t guarantee that the intended person will receive the mail.
Recommended Posts:
- Sending Push Notifications : Progressive Web Apps
- How to configure XAMPP to send mail from localhost using PHP ?
- How to call a function that return another function in JavaScript ?
- How to get the function name inside a function in PHP ?
- How to get the function name from within that function using JavaScript ?
- p5.js | box() Function
- PHP | dir() Function
- D3.js | d3.rgb() Function
- PHP | min( ) Function
- p5.js | hex() function
- PHP | pos() Function
- PHP | end() Function
- PHP | Ds\Map xor() Function
- PHP | Ds\Map last() Function
- PHP | key() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



