Anne Buena
Anne Buena

Reputation: 25

Email Sending Localhost

I'm wondering if it is possible to send an email from a localhost? I've tried something but it didn't worked and I thought maybe it wont work in a localhost. Here's the code:

           // Send the email:
            $message = " To activate your account, please click on this link:\n\n";
            $message .= WEBSITE_URL . '/activate.php?email=' . urlencode($Email) . "&key=$activation";
            mail($Email, 'Registration Confirmation', $message, 'From: [email protected]');

            // Flush the buffered output.


            // Finish the page:
            echo '<div class="success">Thank you forregistering! A confirmation email has been sent to '.$Email.' Please click on the Activation Link to Activate your account </div>';

And here's the connection:

        /*Define constant to connect to database */
        DEFINE('DATABASE_USER', 'root');
        DEFINE('DATABASE_PASSWORD', 'buena');
        DEFINE('DATABASE_HOST', 'localhost');
        DEFINE('DATABASE_NAME', 'forum');
        /*Default time zone ,to be able to send mail */
        date_default_timezone_set('UTC');

        /*You might not need this */
        ini_set('SMTP', "mail.myt.mu"); // Overide The Default Php.ini settings for sending mail


        //This is the address that will appear coming from ( Sender )
        define('EMAIL', '[email protected]');

        /*Define the root url where the script will be found such as http://website.com or http://website.com/Folder/ */
        DEFINE('WEBSITE_URL', 'http://localhost');


       // Make the connection:
      $dbc = @mysqli_connect(DATABASE_HOST, DATABASE_USER, DATABASE_PASSWORD,
DATABASE_NAME);

       if (!$dbc) {
trigger_error('Could not connect to MySQL: ' . mysqli_connect_error());
       }

Is there anything wrong? Or missing?

Upvotes: 1

Views: 3469

Answers (3)

Brian Kiremu
Brian Kiremu

Reputation: 4484

Your code is fine, the problem is the mechanism through which PHP sends the email. In windows there is no out-of-the-box solution for PHP's mail() function to use to send the email contrast to *NIX systems which come shipped with mostly sendmail.

In any case it is possible to implement such a mechanism in Windows to allow the mail() function to work seamlessly. As quick as it is to do s, I won't go into how to do it here. Please refer to this clear step-by-step post and you'll be up and running in no time.

Upvotes: 1

user2162192
user2162192

Reputation: 151

You will needs a SMTP server for php mail to use. For the mail functions to be available, PHP requires an installed and working email system. The program to be used is defined by the configuration settings in the php.ini file. PHP is usually packaged with PEAR Mail Package. Search this site for PEAR Mail. For tons of info on it.

Upvotes: 0

Beniamin Szabo
Beniamin Szabo

Reputation: 1929

To send emails from localhost you need a smtp server on your local machine. Which i think is not necessary because there is a small program that listens for emails sent from the localhost, catches them and opens them in your default email client.

Here is a link

I strongly recomand this program. I also use it and it's free :)

Upvotes: 1

Related Questions