3

I keep getting the error

warning: mail() [<a href='function.mail'>function.mail</a>]: Failed to connect to mailserver at &quot;localhost&quot; port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() 

in my php code

<?php
// the message
$msg = "Testing";

// use wordwrap() if lines are longer than 70 characters
$msg = wordwrap($msg,70);

// send email
mail("someone@example.com","My subject",$msg);
?>

php.ini

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP = localhost
; http://php.net/smtp-port
smtp_port = 26
; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = you@yourdomain

; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
;sendmail_path = ""
WeSt
  • 2,628
  • 5
  • 22
  • 37
minimomo
  • 581
  • 1
  • 6
  • 16
  • 1
    are you testing this on a live server or local test server? If local, do you have a mailserver setup on LAN? – Professor Abronsius Feb 18 '15 at 15:57
  • i dont think i have setup on LAN. what should i do that? @RamRaider – minimomo Feb 18 '15 at 16:03
  • On a dev box try this :http://antix.co.uk/Projects/SMTP-Impostor-an-SMTP-server-for-developers - it's a fake SMTP server (though there also one bundled in XAMPP) – CD001 Feb 18 '15 at 16:07
  • 1
    Probable duplicate of : http://stackoverflow.com/questions/4532486/failed-to-connect-to-mailserver-at-localhost-port-25 – CD001 Feb 18 '15 at 16:08
  • Without a mailserver you will not be able to send emails. If this script is being tested on a live host - on the interweb - then most likely this script will run. You could try uploading to your webhost and running directly to see if you get the email. If you cannot do that you might have to setup a local mailserver ( trickier than you might want to experience ) and test again on local development server. – Professor Abronsius Feb 18 '15 at 16:08

1 Answers1

0

You need to have a SMTP server running, which accept your emails, which are send via PHP. You need to point to it in php.ini.

Your Configuration points to localhost. I think, there is no E-Mail Server on localhost running? If so, double check, if it accept E-Mails from localhost (PHP).

Here is how to setup a server:

For IIS:

http://geekswithblogs.net/tkokke/archive/2009/05/31/sending-email-from-php-on-windows-using-iis.aspx

Note, that it is configured over IIS6 Console, also if you have the latest one. For Some reason, it can't be configured with the >=IIS7 Console, but still works with the old one, which is also installed.

For Linux:

How to send email from localhost using PHP on Linux

For Login via SMTP to Mail Account

There is also a good E-Mail Class called PHPMailer, which comes in handy, if we are working with SMTP directly on PHP side:

$mail = new PHPMailer;
$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication

$mail->Username = 'user@example.com';                 // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->From = 'from@example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
$mail->addAddress('ellen@example.com');               // Name is optional

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}
Community
  • 1
  • 1
Christian Gollhardt
  • 16,510
  • 17
  • 74
  • 111