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.
- How to send mail using PHP PEAR mail with SMTP authentication
- How to send mail from localhost using Gmail SMTP server in PHP
Below is the syntax for
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.
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.
- To - To email id which user enters in the form
- Subject - Subject of the email
- Mail Body - body of the mail
- 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.
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.
- You need to set you mail server name and port in php.ini to be able to send mail.
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
- 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
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.
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.