How to send mail using mail() function in PHP with example

Using php mail() function is a simple and quick way to send email. You can easily send email from your PHP program by calling this function with some parameters. In this topic, we will discuss about sending email using PHP mail() function.

There are more secured and structured ways to send email. For example, PEAR Mail or PHPMailer, they are more secured and provide many useful and important features. If you are interested you can also read below two topics for sending email.

  1. How to send mail using PHP PEAR mail with SMTP authentication
  2. How to send mail from localhost using Gmail SMTP server in PHP

Below is the syntax for mail() function.

All you have to do is just give an email id to whom your are going to send email, give a subject, write mail body and use headers (like from, cc, bcc etc.).

Let us write index.php with a simple form with three input fields and a submit button. An input text field for "to_email", a subject field and a message textarea.

Send Mail in PHP using php mail() function

Let us look at the code below for the form:

Send Mail PHP code

Once the form is submitted to send mail, below PHP code is used for sending mail:

You can see it takes three input values for to_email, subject and message. Then it validates them. If everything looks correct, it formats the mail body in html and uses mail() function to send the mail.

mail() function uses 4 parameters.

  1. To - To email id which user enters in the form
  2. Subject - Subject of the email
  3. Mail Body - body of the mail
  4. Header - header of the message which we have formed it here in the code as $headers. Usually it contains the document type, form email id, cc and bcc.

If mail() function works successfully we print a successful message, otherwise we print error message.

I have given index.php and custom stylesheet here. You can also download the code from download section.



Send Mail configuration in PHPImportant Note

  1. You need to set you mail server name and port in php.ini to be able to send mail.
  2. PHP mail() function may not be available in some hosting servers because of security. mail() function could be disabled. As I told before, it is always better to use SMTP authentication. You can use PEAR Mail using SMTP server name, port and userid/password to authenticate. You can read the topic How to send mail using PHP PEAR mail with SMTP authentication
  3. You can also setup your local PHP configuration parameters to send mail using your Gmail account from your localhost. Read the topic How to send mail from localhost using Gmail SMTP server in PHP.

download Source code to Send Mail in PHPDownload Source Code

I have put all codes in a zip file. You can download it by clicking on the Download button below. You do not need to register yourself to download it. You can directly use the code or you can modify them as per your requirements.

Send Mail in PHP mail() functionConclusion

Based on the requirement in your project, you can trigger mail after an activity is completed. For example, when an appointment is made or when a user's registration is done, an email can be triggered. You can write a function for sending mail and call it from anywhere in the application with the parameters. Refer to the related topics below for other ways to send mail using PHP. Hope it will be useful for you.