Sharat S Nair
Sharat S Nair

Reputation: 3

Getting error while trying to send mail in php

I am getting the error message as Failed to connect to mailserver at &quot,localhost&quot, port 25,...

Contents of my php.ini file are

SMTP = localhost
smtp_port = 25

I used the following code

mail("[email protected]","test","msg","from [email protected]");

Upvotes: 0

Views: 244

Answers (2)

Steward Godwin Jornsen
Steward Godwin Jornsen

Reputation: 1179

You can't send outbound mails from localhost. To test the mail function, install mercury mail. It should come with xampp. Create emails for the localhost domains like steward@localhost. YOu could use aliases. Do your testing with that sending mails from one inbox to the other. You'd need a licensed version of mercury mail to send outbound messages.

Another option is to run your test on a remote server. Make sure the senders email is recognised by the sending server. Like you cannot be sending a gmail message using those settings you are displaying.

If sending from gmail is your objective, stackoverflow is full of answers already for how to send mails with gmail, even with codeigniter. Need some?

Here is what you ask for: Sending mail using gmail:

   require_once "Mail.php";

    $from = "<from.gmail.com>";
    $to = "<to.yahoo.com>";
    $subject = "Hi!";
    $body = "Hi,\n\nHow are you?";

    $host = "ssl://smtp.gmail.com";
    $port = "465";
    $username = "[email protected]";  //<> give errors
    $password = "password";

    $headers = array ('From' => $from,
      'To' => $to,
      'Subject' => $subject);
    $smtp = Mail::factory('smtp',
      array ('host' => $host,
        'port' => $port,
        'auth' => true,
        'username' => $username,
        'password' => $password));

    $mail = $smtp->send($to, $headers, $body);

    if (PEAR::isError($mail)) {
      echo("<p>" . $mail->getMessage() . "</p>");
     } else {
      echo("<p>Message successfully sent!</p>");
     }

?>  <!-- end of php tag-->

Get info here: https://stackoverflow.com/a/2748837/827224

You could even do more if you are using codeigniter or any PHP email class like PHPMailer search email classes in phpclasses.org. An example is: http://www.phpclasses.org/blog/package/9/post/1-Sending-email-using-SMTP-servers-of-Gmail-Hotmail-or-Yahoo-with-PHP.html

Finally, here is a class you can use:

http://www.phpclasses.org/package/7834-PHP-Send-e-mail-messages-Gmail-users-via-SMTP.html

Upvotes: 2

Husman
Husman

Reputation: 6909

Its clearly evident that there is no mail server running on localhost on port 25 (or a firewall is blocking it). Get and install a mailserver (there are a number of free ones for windows/linux/mac - just make sure your ISP allows it) and your script will run just fine.

I use this for testing which is quite nice: http://smtp4dev.codeplex.com/ Its a fake email server, that intercepts mail and dumps them for you to inspect and test on localhost.

Upvotes: 1

Related Questions