1

I am using the same php code I have always used to try and send a form via email and I am getting this message:

Warning: mail() [function.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 sendMailSuscribete.php on line 44

this are lines 40 - 54:

$header .= "From: $email" . "\r\n";
$header .= "Reply-To: $email" . "\r\n";
$header .= "Return-Path: $email" . "\r\n";

if(mail($to, $subject, $msg, $header)){
    //Message sent!
    redirect("http://www.domain.com/suscribete.html");
}else{
    // Display error message if the message failed to send
    echo "
    <div class=\"MsgError\">
        <h1>Error&hellip;</h1>
        <p>Disculpa <b><?=$name;?></b>, tu mensaje fall&oacute; en ser enviado. Por favor vuelve a intentar.</p>
</div>";
}

I already checked using phpinfo() and I know that the smtp is set to localhost and the port it uses is 25 so I really have no idea what the error may be.

Update I forgot to say it is running on a Windows server and this php file is the one I've always used for Unix servers, should it contain something different?

4
  • 1
    Are you running a SMTP server on localhost port 25? Commented Dec 21, 2010 at 1:08
  • Try testing it with a Gmail SMTP server or something to eliminate the code being at fault. Commented Dec 21, 2010 at 1:11
  • To do that I should just try to use it via Gmail? (adding the account) or is it differently? Commented Dec 21, 2010 at 1:12
  • No, it would involved updating your SMTP server settings in the php.ini file or through ini_set. I'll post a proper answer here to help you test it. Commented Dec 21, 2010 at 1:25

2 Answers 2

3

mail cannot send e-mails directly (at least not on Windows), it needs an SMTP server. There is no SMTP server running on the host on which the PHP-Script is being executed. Solutions are:

  1. Use a library that can send e-mails directly using SMTP, like PHPMailer or Swiftmailer (github repo).
  2. Edit the php.ini directives to point to a SMTP server that accepts e-mails from you. Ask your hosting provider if you do not know which one to use.
  3. Set up an SMTP server on localhost.

Point 1. is a good idea, because those libraries provide abstractions that make it unnecessary to deal with low level stuff like putting MIME messages together correctly. Point 2. is probably the least trouble. I advice against Point 3. unless you are serious about administering an e-mail server by yourself.

Sign up to request clarification or add additional context in comments.

Comments

3

Can you try using an external SMTP mail server, for example, Gmail's? If you have a Gmail account, you can try adding this before your mail command:

ini_set("SMTP","ssl://smtp.gmail.com");
ini_set("smtp_port","465");
ini_set("username","<myaccount.gmail.com>"); # You need to change this
ini_set("password","YOUR_PASSWORD"); # You need to change this

I haven't tested these settings, but it should send you in the right direction.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.