Search code examples
phpserversmtpportphpmailer

php mail function not working in new server


Background

My visitmeemure.com was hosted in ipage server and everything worked fine.

Then I have moved my site to smartersap.net and now I'm receiving below error when someone submit the contact us form.

Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in H:\root\home\cmadurawala-001\www\aaaa\contact.php on line 77

Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in H:\root\home\cmadurawala-001\www\www\aaaa\contact.php on line 79

So I have contacted the server admins and they said that the don't support php mail function and I have to apply smtp authentication in application.

Can anyone please tell me how to fix this. I am currently using php mail function in contact page of my site.

Thanks


Solution

  • There is a library called PHPMailer. You could use it instead of the native mail() function. This is how the code looks like with PHPMailer

    <?php
    use PHPMailer\PHPMailer\PHPMailer;
    use PHPMailer\PHPMailer\Exception;
    
    require 'vendor/autoload.php';
    // here you create the mail object
    $mail = new PHPMailer(true);
    

    I suggest using composer to install the library and its dependencies. But, since your app is already in the server, you can just download if from GitHub and put it manually there

    More information: https://github.com/PHPMailer/PHPMailer