PHP Sending Email
PHP allows you to send emails directly from a script.
PHP mail() Function
The PHP mail() function is used to send email from a script.
Syntax
mail(to,subject,message,headers,parameters)
| Parameters | Description |
|---|---|
| to | Required. Specifies the email recipient. |
| subject | Required. Specifies the subject of the email. **Note:** This parameter cannot contain any newline characters. |
| message | Required. Defines the message to be sent. Lines should be separated using LF (\n). Each line should be limited to 70 characters. |
| headers | Optional. Specifies additional headers, such as From, Cc, and Bcc. Additional headers should be separated using CRLF (\r\n). |
| parameters | Optional. Specifies additional parameters for the mail sending program. |
**Note:** Running the mail function in PHP requires an installed and running mail system (such as sendmail, postfix, qmail, etc.). The program used is defined via configuration settings in the php.ini file. Please see our PHP Mail Reference Manual to read more.
PHP Simple E-Mail
The simplest way to send email via PHP is to send a text email.
In the example below, we first declare variables ($to, $subject, $message, $from, $headers), then we use these variables in the mail() function to send an email:
PHP Mail Form
With PHP, you can create a feedback form on your own site. The example below sends a text message to a specified e-mail address:
Runoob Tutorial(runoob.com)
Email:
Subject:
Message:
";
}
?>
Example explanation:
- First, check whether the email input box has been filled in
- If not filled in (such as when the page is first visited), output the HTML form
- If filled in (after the form is submitted), send the email from the form
- After filling out the form and clicking the submit button, the page reloads; you can see the email input is reset and a message indicating successful email sending is displayed
**Note:** This simple email sending is insecure. In the next chapter of this tutorial, you will read more about security risks in email scripts, and we will explain how to validate user input to make it safer.
PHP Mail Reference Manual
To view more information about the PHP mail() function, please visit our PHP Mail Reference Manual.