What is the recommended approach for sending emails via PHP when the website and mailer are hosted by the same provider?

When the website and mailer are hosted by the same provider, it is recommended to use the provider's SMTP server for sending emails via PHP. This ensures better deliverability and avoids potential issues with the emails being marked as spam.

// Set the SMTP settings provided by the hosting provider
$smtpHost = 'mail.example.com';
$smtpPort = 587;
$smtpUsername = 'your_email@example.com';
$smtpPassword = 'your_password';

// Create a new PHPMailer instance
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = $smtpHost;
$mail->Port = $smtpPort;
$mail->SMTPAuth = true;
$mail->Username = $smtpUsername;
$mail->Password = $smtpPassword;

// Add your email content and recipients
$mail->setFrom('your_email@example.com', 'Your Name');
$mail->addAddress('recipient@example.com', 'Recipient Name');
$mail->Subject = 'Subject of the email';
$mail->Body = 'This is the body of the email.';

// Send the email
if ($mail->send()) {
    echo 'Email sent successfully';
} else {
    echo 'Error sending email: ' . $mail->ErrorInfo;
}