0

I am trying to send email from localhost, using xampp. With this script belowe I am getting this error: 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() in C:\xampp\htdocs\testsubscribe\index.php on line 69

<?php

// Salt for hashing confirmation keys
$salt = 'yoursecretstring12#11;.-_.21';

//$url = 'localhost/testsubscribe/index.php';
$url = 'http://localhost/testsubscribe/index.php';
$fromEmail = 'emial@mail.com';

$dbHost = 'localhost';
$dbUser = 'root';
$dbPass = '';
$dbDatabase = 'subscribe';

mysql_connect($dbHost, $dbUser, $dbPass);
mysql_select_db($dbDatabase);

$ip = $_SERVER["REMOTE_ADDR"];

if ( isset( $_GET['key'] ) && isset( $_GET['email'] ) ) {

  // If we have 'email' and 'key' parameters, we are handling an opt-in click

  $email = mysql_real_escape_string( $_GET['email'] );

  // Check if key matches hash of email and salt combination and if email is really an email

  if ( sha1( $email.$salt ) == $_GET['key'] && filter_var($email, FILTER_VALIDATE_EMAIL) ) {

    // Check if entry already exists

    $checkDupes = mysql_query( "SELECT COUNT(*) as cnt FROM emails WHERE email = '$email';" ); 
    $result = mysql_fetch_assoc($checkDupes);

    if ($result['cnt'] < 1) {

        // Fresh email, insert into db along with remote ip and timestamp

      mysql_query( "INSERT INTO emails (email, ip, timestamp) VALUES ( '$email', $ip, NOW() );" );
      die('Subscription confirmed!');

    } else {

      die('Email already exists in database');

    }

  } else {

    die('Key mismatch or invalid email!');  

  } 

} else if ( isset( $_POST['email'] ) && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ) {

  // Form submission, send confirmation email    

  $email = $_POST['email'];
  $key = sha1( $email.$salt );

  $link = $url . '?email=' . $email . '&key=' . $key;

  $mailSubject = 'Please confirm your subscription';
  $mailTo = $email;
  $mailBody = 'Please confirm your subscription by clicking <a href="$link">this link</a>'; 
  //$headers = 'From: ' . $fromEmail . "\r\n";
  $headers = "From: $fromEmail\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n";
  mail( $mailTo, $mailSubject, $mailBody, $headers );

} else {

  // Present form and show error if needed

  if ( isset( $_POST['email'] ) ) {
    echo "Ivalid email submitted!<br />";
  } 

  echo '
  <form method="post" action="'.$url.'">
    Email: <input type="text" name="email" /><br />
    <input type="submit" value="Submit" />
  </form>
  ';

}

Here is part of my php.ini :

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP=localhost
; http://php.net/smtp-port
smtp_port=25

; For Win32 only.
; http://php.net/sendmail-from
;sendmail_from = me@example.com


; For Unix only.  You may supply arguments as well (default: "sendmail -t -i").
; http://php.net/sendmail-path
;sendmail_path =

; Force the addition of the specified parameters to be passed as extra parameters
; to the sendmail binary. These parameters will always replace the value of
; the 5th parameter to mail().
;mail.force_extra_parameters =

; Add X-PHP-Originating-Script: that will include uid of the script followed by the filename
mail.add_x_header=On

; The path to a log file that will log all mail() calls. Log entries include
; the full path of the script, line number, To address and headers.
;mail.log =
; Log mail to syslog (Event Log on Windows).
;mail.log = syslog

And here is table in my db:

id  |  email | ip | timestamp
  • Windows does not have a mail server. Suggest you either install one like hMailServer or use a library like phpMailer – RiggsFolly Jul 15 '17 at 16:11
  • @rafalmp It is possible, but that question is quite old, for like seven years. I wasnt able to solve my problem using these advices. He is talking about windows 2003. I was thinking maybe someone has newer answer that will help me resolve my problem. – Milica Pavlovic Jul 15 '17 at 16:15

0 Answers0