Send email php w3schools
Here is an example of how to send an email using PHP, based on the tutorial on W3Schools:
PHP Code:
<?php
// Define the email variables
$to = "[email protected]";
$subject = "Test Email";
$message = "This is a test email sent using PHP";
$headers = "From: [email protected]";
// Send the email
mail($to, $subject, $message, $headers);
// Check if the email was sent successfully
if (mail($to, $subject, $message, $headers)) {
echo "Email sent successfully!";
} else {
echo "Error sending email!";
}
?>
Explanation:
- The first line defines the recipient's email address (
$to
). - The second line defines the subject of the email (
$subject
). - The third line defines the message body of the email (
$message
). - The fourth line defines the sender's email address (
$headers
) and sets theFrom
header. - The
mail()
function is used to send the email. It takes four arguments: the recipient's email address, the subject, the message body, and the headers. - The
if
statement checks if the email was sent successfully. If it was, it echoes a success message. If not, it echoes an error message.
Note: This code assumes that your PHP script has permission to send emails. If you're running this code on a local machine, you may need to configure your email settings or use a third-party email service.
Also: This is a basic example, and you may want to add more features to your email sending script, such as:
- Validating the recipient's email address
- Adding attachments
- Using a more advanced email library
- Handling errors and exceptions