Send email using mail function in PHP

Sending mail is the most common feature in web development. Even the smallest website requires sending/receiving email feature. If you are a PHP developer you should know it. There are many options available to send mail in PHP but today I am going talk about the builtin function of PHP that is mail() function. So let’s see how to send email using mail function in PHP.

PHP introduced mail() function in PHP 4. Well, that means it supports all versions of PHP. Sending mail from the local server (XAMPP) is full of setup and most of the time it doesn’t work. But It does work on a live server.

Now lets see syntax of mail() function.

mail(to, subject, message, additional_headers)

mail() function accepts four parameters but only the first three is required the fourth one is optional. As you see its single line and sell explanatory that,

to – Email receivers email address in string format.

subject– just like regular emails have subject.It is required.

message- This is body that holds the information you want to send to recipients.

additonal_headers– It includes fromccbcc any other headers like message character encoding type.

Lets have some actual implementation of mail function. Lets assume you have feedback form whenever someone fill form and hit the submit button you want to receive emails.

if (isset($_POST['submit'])) {
        $from_email = $_POST['from_email'];
                 'content-type :text/html;charset=UTF-8';
	$message='Message :'.$msg.'<br>';

	if(mail('','feedback mail',$message,$headers)){
		echo "Mail sent";
	echo "could not able to sent, try again";

Hope this article will help you. If you want to know more about mail() function visit the official website of PHP.

I will create another article about how to send emails from the XAMPP server. So that was all about send email using mail function in PHP.