PHP allows you to send emails directly from scripts.
The PHP mail() function is used to send emails from scripts.
grammar
mail(to,subject,message,headers,parameters)
parameter | describe |
---|---|
to | Required. Specify email recipients. |
subject | Required. Specifies the subject of the email. Note: This parameter cannot contain any newline characters. |
message | Required. Define the message to be sent. LF (n) should be used to separate lines. 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 (rn). |
parameters | Optional. Specifies additional parameters for the mailer. |
Note: Running mail functions in PHP requires an installed and running mail system (such as sendmail, postfix, qmail, etc.). The program used is defined through configuration settings in the php.ini file. Read more in our PHP Mail reference manual.
The simplest way to send email via PHP is to send a text email.
In the following example, we first declare the variables ($to, $subject, $message, $from, $headers), and then we use these variables in the mail() function to send an email:
<?php$to = "[email protected]"; // Email recipient $subject = "Parameter email"; // Email title $message = "Hello! This is the content of the email."; // Email body $ from = "[email protected]"; // Email sender $headers = "From:" . $from; // Header information settings mail($to,$subject,$message,$headers);echo "Email has been sent";?>
With PHP, you can create a feedback form on your site. The following example sends a text message to the specified e-mail address:
<html><head><meta charset="utf-8"><title>Coder Tutorial (codercto.com)</title></head><body><?phpif (isset($_REQUEST['email' ])) { // If the email parameter is received, send an email // Send an email $email = $_REQUEST['email'] ; $subject = $_REQUEST['subject'] ; $message = $_REQUEST['message'] ; mail("[email protected]", $subject, $message, "From:" . $email); echo "Mail sent successfully";} else { // If there is no email parameter then Display form echo "<form method='post' action='mailform.php'> Email: <input name='email' type='text'><br> Subject: <input name='subject' type='text'><br> Message:<br> <textarea name='message' rows='15' cols='40'> </textarea><br> <input type='submit'> </form >";}?></body></html>Example explanation:
First, check whether the email input box is filled in
If not filled in (such as when the page is visited for the first time), output the HTML form
If filled in (after the form has been filled in), send an email from the form
After filling out the form and clicking the submit button, the page is reloaded and you can see that the email input is reset and a message that the email was sent successfully is displayed.
Note: This simple e-mail is not secure. In the next chapter of this tutorial, you will read more about the security risks in e-mail scripts. We will explain how to validate user input to make it more secure.
For more information about the PHP mail() function, visit our PHP Mail Reference Manual.