Is it best practice to use ini_set to specify the SMTP server and port in PHP for sending emails?

When sending emails in PHP, it is generally not recommended to use ini_set to specify the SMTP server and port. This is because it can lead to potential security risks, such as exposing sensitive information in the code. Instead, it is best practice to use a dedicated email library, such as PHPMailer, which provides a more secure and reliable way to send emails.

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

require 'vendor/autoload.php'; // Include PHPMailer autoload file

// Create a new PHPMailer instance
$mail = new PHPMailer();

// Set SMTP settings
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->Port = 587;
$mail->SMTPAuth = true;
$mail->Username = 'your_username';
$mail->Password = 'your_password';

// Add more email configuration settings as needed

// Send email
if($mail->send()) {
    echo 'Email sent successfully';
} else {
    echo 'Email sending failed';
}