W3school php mail
W3Schools provides a simple example of how to send an email using PHP. Here is the code:
<?php
$to = "[email protected]";
$subject = "Test email from W3Schools";
$message = "Hello! This is a test email sent from W3Schools.";
$headers = "From: [email protected]";
mail($to, $subject, $message, $headers);
?>
Let's break down what each line does:
$to = "[email protected]";
: This sets the recipient's email address.$subject = "Test email from W3Schools";
: This sets the subject of the email.$message = "Hello! This is a test email sent from W3Schools.";
: This sets the body of the email.$headers = "From: [email protected]";
: This sets the "From" header of the email. This is the email address that will appear in the recipient's email client.mail($to, $subject, $message, $headers);
: This sends the email using themail()
function. The arguments are:$to
: The recipient's email address.$subject
: The subject of the email.$message
: The body of the email.$headers
: The headers of the email.
Note that this is a very basic example, and you may want to add more functionality to your email script, such as:
- Validating the recipient's email address
- Adding a CC or BCC field
- Including attachments
- Handling errors
- Using a more secure method of sending emails (such as using a mail server or a third-party API)
Also, keep in mind that the mail()
function is not always reliable, and you may need to use a more advanced method of sending emails, such as using a PHP mail library or a third-party API.