PHP allows us to send emails directly from scripts.
mail() function
The mail() function is used to send emails from scripts.
Syntax:
- /**
- * 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.
- * 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.
- */
- mail(to,subject,message,headers,parameters)
-
Copy code
Note: PHP requires an installed and running mail system in order for the mail functions to be available. The program used is defined through configuration settings in the php.ini file.
Example:
The easiest way to send an 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 e-mail:
- $to = "someone@example.com";
- $subject = "Test mail";
- $message = "Hello! This is a simple email message.";
- $from = " someonelse@example.com";
- $headers = "From: $from";
- mail($to,$subject,$message,$headers);
- echo "Mail Sent.";
- ?>
-
Copy code
With PHP we can create a feedback form on our site. The following example sends a text message to the specified e-mail address:
Reprint: PHP Send Email [Original]
|