Send mail php

Here is an example of how to send an email using PHP:

<?php
// Define the email settings
$to = '[email protected]';
$subject = 'Test Email';
$message = 'This is a test email sent using PHP.';

// Define the email headers
$headers = 'From: [email protected]'. "\r\n".
           'Reply-To: [email protected]'. "\r\n".
           'MIME-Version: 1.0'. "\r\n".
           'Content-Type: text/plain; charset=UTF-8';

// Send the email using the mail() function
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.';
}
?>

This code uses the mail() function to send an email to the specified recipient. The mail() function takes four arguments:

You can also use the PHPMailer library to send emails, it's a popular and powerful library that provides more features and flexibility than the built-in mail() function.

<?php
require 'PHPMailer/PHPMailer.php';
require 'PHPMailer/SMTP.php';

$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'your_password';

$mail->setFrom('[email protected]', 'Your Name');
$mail->addAddress('[email protected]', 'Recipient Name');
$mail->Subject = 'Test Email';
$mail->Body = 'This is a test email sent using PHPMailer.';

if (!$mail->send()) {
    echo 'Error sending email.';
} else {
    echo 'Email sent successfully!';
}
?>

You can also use the SwiftMailer library, it's another popular library that provides more features and flexibility than the built-in mail() function.

<?php
require 'SwiftMailer/Swift.php';

$mail = new Swift_Mailer();
$mail->setFrom('[email protected]', 'Your Name');
$mail->addAddress('[email protected]', 'Recipient Name');
$mail->Subject = 'Test Email';
$mail->Body = 'This is a test email sent using SwiftMailer.';

if (!$mail->send()) {
    echo 'Error sending email.';
} else {
    echo 'Email sent successfully!';
}
?>

Please note that you need to install the PHPMailer or SwiftMailer library and configure it according to your email server settings.

Also, you can use the sendmail function to send emails, it's a built-in function in PHP that allows you to send emails using the sendmail command.

<?php
sendmail('[email protected]', 'Test Email', 'This is a test email sent using sendmail.');
?>

Please note that the sendmail function is not available on all PHP configurations, and it's not recommended to use it as it's not as flexible as the other methods.