Question

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('valid_destination_adress@gmail.com', 'test mail', 'Message of the mail', $headers) ){
    echo 'ok';
}else{
    echo 'erreur';
}
?>
Was it helpful?

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", "postmaster@mydomain.com");

$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(
        'valid_destination_adress@gmail.com', 
        '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

OTHER TIPS

Your code looks correct, so it is surely not a PHP problem, unless the function is not deactivated. You should check the logs of your SMTP server if you have access. Alternatively you can try uisng a tiny PHP library for mailing, most of the known libraries provide an excellent exception handling and logging possibilities,

I recommend you to use Swiftmailer ( http://swiftmailer.org/ )

To send a email is as simple as download the library, unzip it, include the main .php file and for example this lines of code:

require 'lib/swift_required.php';

$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl")
  ->setUsername(SENDER_USERNAME)
  ->setPassword(SENDER_PASSWORD);


$mailer = Swift_Mailer::newInstance($transport);

$message = Swift_Message::newInstance($subject)
  ->setFrom(array(MAIL_FROM))
  ->setTo(array(MAIL_TO))
  ->setBody($message);

$result = $mailer->send($message);

As you see it should be simple configure you smtp server or a existing mail provides, as in the example with gmail.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top