How can setting up a proper mail server help prevent emails sent from PHP being flagged as spam?

Setting up a proper mail server can help prevent emails sent from PHP being flagged as spam by ensuring that the emails are being sent from a legitimate and verified email address. This can help improve the email deliverability rate and reduce the chances of the emails being marked as spam by email providers.

// Example PHP code snippet using PHPMailer to send emails through a properly configured mail server

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

$mail = new PHPMailer(true);

try {
    $mail->isSMTP();
    $mail->Host = 'your-mail-server.com';
    $mail->SMTPAuth = true;
    $mail->Username = 'your-smtp-username';
    $mail->Password = 'your-smtp-password';
    $mail->SMTPSecure = 'ssl';
    $mail->Port = 465;

    $mail->setFrom('your-email@example.com', 'Your Name');
    $mail->addAddress('recipient@example.com');

    $mail->isHTML(true);
    $mail->Subject = 'Subject';
    $mail->Body = 'Email body';

    $mail->send();
    echo 'Email sent successfully';
} catch (Exception $e) {
    echo 'Email could not be sent. Mailer Error: ' . $mail->ErrorInfo;
}