May 11, 2021 PHP
PHP allows you to send e-mail directly from the script. This section will show you how PHP can send e-mail messages.
The PHP mail() function is used to send e-mail messages from the script.
Grammar
Parameters
parameter | describe |
---|---|
to | Required.Specifies the Email recipient. |
subject | Required.Specify the topic of Email. Note: This parameter cannot contain any new line characters. |
message | Required.Define the messages to be sent.LF (\ n) should be used to separate each line.Each line should be limited to 70 characters. |
headers | Optional.The additional title is specified, such as from, CC, and BCC.The additional title should be separated by CRLF (\ r \ n). |
parameters | Optional.Additional parameters are specified for the mail sender. |
Note: P HP requires an installed and running messaging system to make mail functions available. T he program used is defined by .ini configuration settings in the php file. Read more in our PHP Mail reference manual.
The easiest way to send an e-mail message via PHP is to send a text email.
In the following example, we first declare variables ($to, $subject, $message, $from, $headers), and then we use these variables in the mail() function to send an e-mail:
<?php
$to = "[email protected]";
$subject = "Test mail";
$message = "Hello! This is a simple email message.";
$from = "[email protected]";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
PhP allows you to create a feedback form on your site. The following instance sends a text message to the specified e-mail address:
<html>
<body>
<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
//send email
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
mail("[email protected]", $subject,
$message, "From:" . $email);
echo "Thank you for using our mail form";
}
else
//if "email" is not filled out, display the 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 to see if the message entry box is 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 filled out), send an e-mail message from the form
When you click the submit button after filling out the form, the page reloads and you can see that the message entry was reset and a message was sent successfully
Note: This easy e-mail is not secure, and in the next chapter of this tutorial you'll read more about security vulnerabilities in email scripts, and we'll show you how to verify user input to make it more secure.
For more information about the PHP mail() function, please visit our PHP Mail reference manual.
In this section, we explained the basic PHP method of sending e-mail, but there are shortcomings, this deficiency, we will continue to explain for you in the next section!