PHP allows us to send email directly from the script. Mail () functionThe Mail () function is used to send e-mail from a script. Syntax:
- /**
- * To: Required. Specify email recipients.
- * Subject: Required. Specify the subject of email. Note: This parameter cannot contain any new line characters.
- * Message: Required. Defines the message to be sent. You should use LF (\ n) to separate the rows.
- * Headers: Optional. Specify additional headings, such as from, Cc, and BCC. You should use CRLF (\ r \ n) to separate additional headings.
- * Parameters: Optional. Specify additional parameters for the mail sender.
- */
- Mail (to,subject,message,headers,parameters)
Copy CodeNote: PHP requires an installed and running messaging system to make the mail function available. The program used is defined by the configuration settings in the php.ini file. Example: The simplest way to send an email 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 message:
-
- $to = "someone@example.com";
- $subject = "Test mail";
- $message = "hello! A simple email message. ";
- $from = "someonelse@example.com";
- $headers = "From: $from";
- Mail ($to, $subject, $message, $headers);
- echo "Mail Sent.";
- ?>
Copy CodeWith PHP, we are able to make a feedback form on our site. The following example sends a text message to the specified e-mail address:
- 1. First, check whether the Message entry box is filled in
- if (isset ($_request[' email '))
- 2. If it is filled out (after the form is filled out), send the message from the form
- {
- Send email
- $email = $_request[' email '];
- $subject = $_request[' subject ');
- $message = $_request[' message '];
- Send To Dream Mailbox
- 4. When the Submit button is clicked, reload the page to show the message that the message was sent successfully
- Mail ("someone@zmit.cn", "Subject: $subject",
- $message, "From: $email");
- echo "Thank you to using our mail form";
- }
- Else
- 3. If not filled out (for example, when the page is first accessed), Export the HTML form
- {
- echo "";
- }
- ?>
Copy CodeReprint: PHP Send Email "original" |