Mail send code in php

Here is an example of a PHP code that sends an email using the mail() function:

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

  if (mail($to, $subject, $message, $headers)) {
    echo 'Email sent successfully!';
  } else {
    echo 'Error sending email!';
  }
?>

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_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 = '[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 'Email sent successfully!';
  } else {
    echo 'Error sending email!';
  }
?>

This code sends an email using the PHPMailer library, which is configured to use a SMTP server at smtp.example.com. The email is sent from the sender at [email protected] to the recipient at [email protected].

Make sure to replace the placeholders with your actual email addresses and settings.