How to put condition while sending mail using php

In PHP, you can use conditional statements to check certain conditions before sending an email using the mail() function or a PHP mail library like PHPMailer. Here are a few examples:

Using mail() function

<?php
  // assume $to, $subject, and $message are defined

  if ($condition) { // e.g. check if a form field is filled
    mail($to, $subject, $message);
  } else {
    echo "Please fill out the required fields.";
  }
?>

Using PHPMailer

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

  $mail = new PHPMailer\PHPMailer\PHPMailer();
  $mail->isSMTP();
  $mail->Host = 'your_smtp_host';
  $mail->Port = 587;
  $mail->SMTPAuth = true;
  $mail->Username = 'your_smtp_username';
  $mail->Password = 'your_smtp_password';

  if ($condition) { // e.g. check if a form field is filled
    $mail->setFrom('[email protected]', 'Your Name');
    $mail->addAddress($to);
    $mail->Subject = $subject;
    $mail->Body = $message;
    $mail->send();
  } else {
    echo "Please fill out the required fields.";
  }
?>

In both examples, you can replace $condition with a logical expression that checks for a specific condition, such as:

You can also use more complex conditions, such as checking if a user has filled out a specific form field or if a certain value is present in a database.

Remember to replace the placeholders ($to, $subject, $message, etc.) with the actual values you want to use for your email.