How to change php mailer to smtp hostinger
To change the PHP mailer to use SMTP with Hostinger, you'll need to configure the PHPMailer settings in your PHP script. Here's a step-by-step guide to help you do so:
Step 1: Install PHPMailer
If you haven't already, install PHPMailer using Composer:
composer require phpmailer/phpmailer
Step 2: Configure PHPMailer
Create a new file named config.php
(or update an existing one) and add the following code:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';
$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'smtp.hostinger.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]'; // Replace with your Hostinger email address
$mail->Password = 'your_email_password'; // Replace with your Hostinger email password
$mail->SMTPSecure = 'tls'; // or 'ssl' depending on your Hostinger settings
$mail->Port = 587; // or 465 depending on your Hostinger settings
?>
Replace the placeholders with your actual Hostinger email address and password.
Step 3: Update your PHP script
In your PHP script, include the config.php
file and use the PHPMailer
object to send emails:
<?php
require 'config.php';
$mail->Subject = 'Test email from Hostinger';
$mail->Body = 'This is a test email sent from Hostinger using PHPMailer';
if (!$mail->send()) {
echo 'Error sending email: '. $mail->ErrorInfo;
} else {
echo 'Email sent successfully!';
}
?>
Step 4: Test your setup
Run your PHP script to test the email sending functionality. If everything is set up correctly, you should receive an email at your Hostinger email address.
Additional tips:
- Make sure to replace the placeholders in the
config.php
file with your actual Hostinger email address and password. - If you're using SSL/TLS encryption, ensure that your Hostinger account is configured to use it.
- If you're still experiencing issues, check your Hostinger email account settings and ensure that the SMTP settings are correct.
- You can also use the
PHPMailer
object to send emails using other SMTP servers, such as Gmail or Outlook, by modifying theHost
,Username
, andPassword
settings accordingly.