What SMTP server settings should be specified in PHP scripts to avoid SMTP server response errors like "We do not relay non-local mail"?

When sending emails from a PHP script, you may encounter SMTP server response errors like "We do not relay non-local mail" if the SMTP server requires authentication to send emails to external domains. To avoid this error, you should specify the correct SMTP server settings including the SMTP host, port, username, password, and encryption method (if applicable) in your PHP script.

// Specify the SMTP server settings to avoid "We do not relay non-local mail" error
$smtpHost = 'mail.example.com';
$smtpPort = 587;
$smtpUsername = 'your_smtp_username';
$smtpPassword = 'your_smtp_password';
$smtpEncryption = 'tls';

// PHPMailer library is used for sending emails
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

$mail = new PHPMailer(true);

try {
    $mail->isSMTP();
    $mail->Host = $smtpHost;
    $mail->Port = $smtpPort;
    $mail->SMTPAuth = true;
    $mail->Username = $smtpUsername;
    $mail->Password = $smtpPassword;
    $mail->SMTPSecure = $smtpEncryption;

    // Add your email sending code here

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