Send mail using php code

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);

// Alternatively, you can use the PHPMailer library
// require_once 'PHPMailer/PHPMailer.php';
// $mail = new PHPMailer\PHPMailer\PHPMailer();
// $mail->setFrom('[email protected]');
// $mail->addAddress('[email protected]');
// $mail->Subject = 'Test Email';
// $mail->Body = 'This is a test email sent using PHPMailer.';
// $mail->send();

?>

This code uses the built-in mail() function to send an email. The mail() function takes four arguments:

The email headers are defined using the From, Reply-To, MIME-Version, and Content-Type headers. The From header specifies the sender's email address, the Reply-To header specifies the email address that should receive replies, the MIME-Version header specifies the version of the MIME standard being used, and the Content-Type header specifies the type of content being sent (in this case, plain text).

You can also use the PHPMailer library to send emails. PHPMailer is a popular and widely-used library that provides a more flexible and powerful way of sending emails. Here is an example of how to use PHPMailer:

<?php

require_once 'PHPMailer/PHPMailer.php';
require_once 'PHPMailer/SMTP.php';

$mail = new PHPMailer\PHPMailer\PHPMailer();

$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'username';
$mail->Password = 'password';

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

$mail->send();

?>

This code uses the PHPMailer library to send an email. The PHPMailer library provides a more flexible and powerful way of sending emails, and allows you to specify the SMTP server, authentication details, and other settings.

Note that you will need to install the PHPMailer library and configure it to use your email server. You can download the PHPMailer library from the official website and follow the installation instructions.

Also, you can use other libraries such as SwiftMailer, Zend_Mail, etc.

Please keep in mind that this is a basic example, you should consider security and validation of the email address, and also you should handle the errors that may occur during the sending process.