How to send mail in php code

In PHP, you can send an email using the mail() function, which is a built-in function in PHP. Here is an example of how to use it:

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

$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';

mail($to, $subject, $message, $headers);
?>

This code sends an email to the recipient at [email protected] with the subject "Test email" and the message "This is a test email sent using PHP.". The email is sent from the sender at [email protected].

You can also use the PHPMailer library, which is a popular and powerful email sending library for PHP. Here is an example of how to use it:

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

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

$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

$mail->setFrom('[email protected]', 'Sender 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: '. $mail->ErrorInfo;
} else {
    echo 'Email sent successfully!';
}
?>

This code sends an email to the recipient at [email protected] with the subject "Test email" and the message "This is a test email sent using PHPMailer.". The email is sent from the sender at [email protected] using the SMTP server at smtp.example.com.

You can also use the SwiftMailer library, which is another popular email sending library for PHP. Here is an example of how to use it:

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

$mail = new Swift_Mailer(new Swift_SmtpTransport('smtp.example.com', 587, 'tls'));
$mail->setFrom('[email protected]', 'Sender Name');
$mail->addTo('[email protected]', 'Recipient Name');
$mail->setSubject('Test email');
$mail->setBody('This is a test email sent using SwiftMailer.');

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

This code sends an email to the recipient at [email protected] with the subject "Test email" and the message "This is a test email sent using SwiftMailer.". The email is sent from the sender at [email protected] using the SMTP server at smtp.example.com.

Note that you need to replace the placeholders ([email protected], [email protected], smtp.example.com, etc.) with your actual email addresses and settings.