Search code examples
phpemailwindows-server

use php mail function on ovh shared hosting windows server


I don't succeed to send mail with the php mail() function.

I would like to use my mail address which I had with hosting : postmaster@my_domain.com.

hosting is the 'perso' offer using windows server.

Is it some configuration to do on php before to use mail function ?

Can I have access to php.ini ?

or maybe something to check/uncheck in ovh manager.

I have searched on google and try some solutions but I found many things and I don't know what is usefull or not.

I have send a request to OVH technical support too but I'm still waiting for an answer.

Now, I have done a very basic script to test the function but it doesn't send mail :

<?php
$headers ='From: postmaster@my_domain.com'."\n";
$headers .='Reply-To: postmaster@my_domain.com'."\n";
$headers .='Content-Type: text/plain; charset="iso-8859-1"'."\n";
$headers .='Content-Transfer-Encoding: 8bit';
if( mail('[email protected]', 'test mail', 'Message of the mail', $headers) ){
    echo 'ok';
}else{
    echo 'erreur';
}
?>

Solution

  • 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()

    That means the mail is trying to be sent thanks to the localhost. But ovh provide a specific domain for mailing : smtp.mydomain.com. So you will have to use ini_set() since you can't change the php.ini on shared host :

    ini_set("SMTP", "smtp.mydomain.com");
    ini_set("sendmail_from", "[email protected]");
    
    $headers ='From: postmaster@my_domain.com'."\n";
    $headers .='Reply-To: postmaster@my_domain.com'."\n";
    $headers .='Content-Type: text/plain; charset="iso-8859-1"'."\n";
    $headers .='Content-Transfer-Encoding: 8bit';
    
    if (
        mail(
            '[email protected]', 
            'test mail', 
            'Message of the mail', 
            $headers
        ) 
    ){
        echo 'ok';
    } else {
        echo 'erreur';
    }
    echo "Check your email now....<br/>";
    

    But I would recommand to use a library for this, such as SwiftMailer, or PHPMailer