Failed to connect to mailserver

  css, email, html, localhost, php

below is my sendmail.php file which sends mail to specified user.

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:xampphtdocsrachanaloginsendmail.php on line 61

i have also changed the php.ini file-

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP=smtp.gmail.com
; http://php.net/smtp-port
smtp_port=25
sendmail_from = [email protected]

i have also changed sendmail.exe file where i have provided username and password.

<?php
//$error=[];
function clean_text($string)
{
     $string = trim($string);
     $string = stripslashes($string);
     $string = htmlspecialchars($string);
     return $string;
}

if(isset($_POST["submit"])) {
    /*if(empty($_POST["from_message"]))
    {
        //$error[] = '<p><label class="text-danger">Please Enter your Email</label></p>';
        echo "Please Enter your Email";
    } else {
        $fromEmail = clean_text($_POST["from_message"]);
        if(!filter_var($fromEmail, FILTER_VALIDATE_EMAIL)) {
            // $error[] = '<p><label class="text-danger">Invalid email format</label></p>';
            echo "Invalid email format";
        }
    }*/

    if(empty($_POST["to_message"])) {
        //$error[]= '<p><label class="text-danger">Please Enter your Email</label></p>';
        echo "Please Enter your Email";
    } else {
        $to = clean_text($_POST["to_message"]);
        if(!filter_var($to, FILTER_VALIDATE_EMAIL)) {
            //$error[] = '<p><label class="text-danger">Invalid email format</label></p>';
            echo "Invalid email format";
        }
    }
    
    if(empty($_POST["subject"])) {
        //$error[] = '<p><label class="text-danger">Subject is required</label></p>';
        echo "Subject is required";
    } else {
        $subject = clean_text($_POST["subject"]);
    }
    if(empty($_POST["compose-textarea"])) {
        // $error[] = '<p><label class="text-danger">Message is required</label></p>';
        echo "Message is required";
    } else {
        $message = clean_text($_POST["compose-textarea"]);
    }
    if(empty($error)) {
        //$headers = 'From: '.$fromEmail.'<'.$fromEmail.'>' . "rn".'Reply-To: '.$fromEmail."rn" . 'X-Mailer: PHP/' . phpversion();
        $result = mail($to, $subject, $message);
        if($result) {
            //echo '<script>alert("Mail sent Successfully")</script>';
            echo "success";
            header('refresh:1;mail_activity.php');
        } else {
            //$error[] = '<label class="text-danger">There is an Error</label>';
            echo "error";
        }
    }
       // else{
         //   echo '<script>alert($error)</script>';
            //header('refresh:1;mail_activity.php');
        //}
    }
?>

mailactivity.php

<!DOCTYPE html>
<html lang="en">
<?php include('db_connection.php');
if(!isset($_SESSION))
{
  session_start();
}
?>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Task Manager | MailBox</title>

  <!-- Google Font: Source Sans Pro -->
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700&display=fallback">
  <!-- Font Awesome -->
  <link rel="stylesheet" href="../plugins/fontawesome-free/css/all.min.css">
  <!-- Theme style -->
  <link rel="stylesheet" href="../dist/css/adminlte.min.css">
  <!-- summernote -->
  <link rel="stylesheet" href="../plugins/summernote/summernote-bs4.min.css">
  <!-- iCheck for checkboxes and radio inputs -->
  <link rel="stylesheet" href="../plugins/icheck-bootstrap/icheck-bootstrap.min.css">
  <!-- Bootstrap Color Picker -->
  <link rel="stylesheet" href="../plugins/bootstrap-colorpicker/css/bootstrap-colorpicker.min.css">
  <!-- Tempusdominus Bootstrap 4 -->
  <link rel="stylesheet" href="../plugins/tempusdominus-bootstrap-4/css/tempusdominus-bootstrap-4.min.css">
  <!-- Select2 -->
  <link rel="stylesheet" href="../plugins/select2/css/select2.min.css">
  <link rel="stylesheet" href="../plugins/select2-bootstrap4-theme/select2-bootstrap4.min.css">
  <!-- Bootstrap4 Duallistbox -->
  <link rel="stylesheet" href="../plugins/bootstrap4-duallistbox/bootstrap-duallistbox.min.css">
  <!-- BS Stepper -->
  <link rel="stylesheet" href="../plugins/bs-stepper/css/bs-stepper.min.css">
  <!-- dropzonejs -->
  <link rel="stylesheet" href="../plugins/dropzone/min/dropzone.min.css">
</head>
<body class="hold-transition sidebar-mini layout-fixed">
<div class="wrapper">
  <!-- Navbar -->
  <nav class="main-header navbar navbar-expand navbar-dark">
    <!-- Left navbar links -->
    <ul class="navbar-nav">
      <li class="nav-item">
        <a class="nav-link" data-widget="pushmenu" href="#" role="button"><i class="fas fa-bars"></i></a>
      </li>
      <li class="nav-item d-none d-sm-inline-block">
        <a href="dashboard.php" class="nav-link">Home</a>
      </li>
    </ul>

    <!-- Right navbar links -->
    <ul class="navbar-nav ml-auto">
      <!-- Navbar Search -->
      <li class="nav-item">
        <a class="nav-link" data-widget="navbar-search" href="#" role="button">
          <i class="fas fa-search"></i>
        </a>
        <div class="navbar-search-block">
          <form class="form-inline">
            <div class="input-group input-group-sm">
              <input class="form-control form-control-navbar" type="search" placeholder="Search" aria-label="Search">
              <div class="input-group-append">
                <button class="btn btn-navbar" type="submit">
                  <i class="fas fa-search"></i>
                </button>
                <button class="btn btn-navbar" type="button" data-widget="navbar-search">
                  <i class="fas fa-times"></i>
                </button>
              </div>
            </div>
          </form>
        </div>
      </li>

      
      <!-- Notifications Dropdown Menu -->
      <li class="nav-item dropdown">
        <a class="nav-link" data-toggle="dropdown" href="#">
          <i class="far fa-bell"></i>
          <span class="badge badge-warning navbar-badge">15</span>
        </a>
        <div class="dropdown-menu dropdown-menu-lg dropdown-menu-right">
          <span class="dropdown-item dropdown-header">15 Notifications</span>
          <div class="dropdown-divider"></div>
          <a href="#" class="dropdown-item">
            <i class="fas fa-envelope mr-2"></i> 4 new messages
            <span class="float-right text-muted text-sm">3 mins</span>
          </a>
          <a href="#" class="dropdown-item dropdown-footer">See All Notifications</a>
        </div>
      </li>
      <li class="nav-item">
        <a class="nav-link" data-widget="fullscreen" href="#" role="button">
          <i class="fas fa-expand-arrows-alt"></i>
        </a>
      </li>
      <li class="nav-item">
      <div class="btn-group">
        <button type="button" class="btn btn-default dropdown-toggle dropdown-icon" data-toggle="dropdown"></button>
        <div class="dropdown-menu">
          <a class="dropdown-item" href="edit_user.php">
          <i class="nav-icon fas fa-edit"></i> Edit User
          </a>
          <div class="dropdown-divider"></div>
          <a class="dropdown-item" href="logout.php">
            <i class="fas fa-sign-out-alt"></i> Logout
          </a>
        </div>
      </div>
      </li>
    </ul>
  </nav>
  <!-- /.navbar -->


  <aside class="main-sidebar sidebar-dark-primary elevation-4">
  <!-- Brand Logo -->
    <a href="" class="brand-link">
    <?php
      $que="SELECT profile_pic from employee WHERE id" .$_SESSION['id'];
      if($result=mysqli_query($conn,$que)){
        $row=mysqli_fetch_array($result);
          if($row['profile_pic']=" "){
            echo '<img src="img/avataar.jpg" alt="Logo" class="brand-image img-circle elevation-3" style="opacity: .8">';
          }
          else{
            $path=$row['profile_pic'];
            echo '<img src="img/$path" alt="Logo" class="brand-image img-circle elevation-3" style="opacity: .8">';
          }
      }
    ?>
     <img src="img/tm-logo.jpg" alt="AdminLTE Logo" class="brand-image img-circle elevation-3" style="opacity: .8">
      <span class="brand-text font-weight-light">Task Manager</span>
    </a>
    <!-- Sidebar -->
    <div class="sidebar">
        <!-- Sidebar user panel (optional) -->
      <div class="user-panel mt-3 pb-3 mb-3 d-flex">
        <div class="image">
          <img src="" class="img-circle elevation-2" alt="User Image">
        </div>
        <div class="info">
          <a href="" class="d-block">
                <?=ucwords($_SESSION['firstname'])," ",ucwords($_SESSION['lastname']) ?>
          <br>
          <small><?=$_SESSION['designation']?></small></a>
        </div>
      </div>
        <!-- Sidebar Menu -->
      <nav class="mt-2">
        <ul class="nav nav-pills nav-sidebar flex-column" data-widget="treeview" role="menu" data-accordion="false">
          <!-- Add icons to the links using the .nav-icon class
               with font-awesome or any other icon font library -->
            <li class="nav-item">
                <a href="dashboard.php" class="nav-link">
                    <i class="nav-icon fas fa-tachometer-alt"></i>
                    <p>Dashboard</p>
                </a>
            </li>
      <li class="nav-item">
        <a href="#" class="nav-link active">
          <p>
            Tasks
            <i class="right fas fa-angle-left"></i>
          </p>
        </a>
        <ul class="nav nav-treeview">
            <li class="nav-item">
                <a href="createtask.php" class="nav-link">
                    <i class="nav-icon far fa-circle text-secondary"></i>
                    <p>Create Task</p>
                </a>
            </li>
            <li class="nav-item">
            <a href="" class="nav-link">
              <i class="nav-icon fas fa-copy"></i>
              <p>
                View Tasks
                <i class="fas fa-angle-left right"></i>
                <span class="badge badge-info right">4</span>
              </p>
            </a>
            <ul class="nav nav-treeview">
              <li class="nav-item">
                <a href="completed_task.php" class="nav-link active">
                  <i class="nav-icon far fa-circle text-danger"></i>
                  <p>Completed Tasks</p>
                </a>
                      <li class="nav-item">
                          <a href="datewise_task.php" class="nav-link">
                              <i class="nav-icon far fa-circle text-warning"></i>
                              <p>Date-Wise Tasks</p>
                          </a>
                      </li>
                <li class="nav-item">
                    <a href="prioritywise_task.php" class="nav-link">
                        <i class="nav-icon far fa-circle text-info"></i>
                            <p>Priority-Wise Tasks</p>
                    </a>
                </li>
                </li>
                <li class="nav-item">
                <a href="pending_task.php" class="nav-link active">
                  <i class="nav-icon far fa-circle text-primary"></i>
                  <p>Pending Tasks</p>
                </a>
                </li>
      </ul>
            </li>
        </ul>
    <li class="nav-item">
            <a href="#" class="nav-link">
              <i class="nav-icon far fa-envelope"></i>
              <p>
                Mailbox
              </p>
            </a>
    </li>
        </ul>
      </nav>
    </div>
</aside>
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h1>Compose</h1>
          </div>
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="#">Home</a></li>
              <li class="breadcrumb-item active">Compose</li>
            </ol>
          </div>
        </div>
      </div><!-- /.container-fluid -->
    </section>
  <form action="sendmail.php" method="POST">
    <div class="col-md-9">
            <div class="card card-primary card-outline">
              <div class="card-header">
                <h3 class="card-title">Compose New Message</h3>
              </div>
              <!-- /.card-header -->
              <div class="card-body">
                <!--<div class="form-group">
                  <input class="form-control" placeholder="From:" id="from_message" name="from_message">
                </div>-->
                <div class="form-group">
                  <input class="form-control" placeholder="To:" id="to_message" name="to_message">
                </div>
                <div class="form-group">
                  <input class="form-control" placeholder="Subject:" id="subject" name="subject">
                </div>
                <div class="form-group">
                    <textarea id="compose-textarea" name="compose-textarea" class="form-control" style="height: 300px">
                      
                    </textarea>
                </div>
              <!-- /.card-body -->
              <div class="card-footer">
                <div class="float-right">
                <input type="button" value="Clear" name="btnclear" id="btnclear" class="btn btn-primary btn-md" onClick="reset_field()">
                <button type="submit" name="submit" id="submit" class="btn btn-primary"><i class="far fa-envelope" onClick="sendmail.php"></i> Send</button>
              <!-- /.card-footer -->
            </div>
            <!-- /.card -->
          </div>
          <!-- /.col -->
    </form>
        </div>
        <!-- /.row -->
      </div><!-- /.container-fluid -->
    </section>
    <!-- /.content -->
  </div>
</div> <!-- ./wrapper -->
  <!-- /.content-wrapper -->
  <!-- /.content-wrapper -->
<footer class="main-footer">
    <div class="float-right d-none d-sm-block">
    </div>
    <strong><a href="dashboard.php">Task Manager</a></strong>
  </footer>

  <!-- Control Sidebar -->
  <aside class="control-sidebar control-sidebar-dark">
    <!-- Control sidebar content goes here -->
  </aside>
  <!-- /.control-sidebar -->



<!-- jQuery -->
<script src="../plugins/jquery/jquery.min.js"></script>
<!-- Bootstrap 4 -->
<script src="../plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- AdminLTE App -->
<script src="../dist/js/adminlte.min.js"></script>
<!-- Summernote -->
<script src="../plugins/summernote/summernote-bs4.min.js"></script>
<!-- AdminLTE for demo purposes -->
<script src="../dist/js/demo.js"></script>
<!-- Page specific script -->
<script>
         function reset_field() {
           // document.getElementById("from_message").reset();
            document.getElementById("to_message").reset();
            document.getElementById("subject").reset();
            document.getElementById("compose-textarea").reset();
         }
      </script>
<script>
  $.widget.bridge('uibutton', $.ui.button)
</script>
<script>
  $(function () {
    //Add text editor
    $('#compose-textarea').summernote()
  })
</script>
<!-- Select2 -->
<script src="../plugins/select2/js/select2.full.min.js"></script>
<!-- bs-custom-file-input -->
<script src="../plugins/bs-custom-file-input/bs-custom-file-input.min.js"></script>
<!-- Bootstrap4 Duallistbox -->
<script src="../plugins/bootstrap4-duallistbox/jquery.bootstrap-duallistbox.min.js"></script>
<!-- InputMask -->
<script src="../plugins/moment/moment.min.js"></script>
<script src="../plugins/inputmask/jquery.inputmask.min.js"></script>
<!-- date-range-picker -->
<script src="../plugins/daterangepicker/daterangepicker.js"></script>
<!-- bootstrap color picker -->
<script src="../plugins/bootstrap-colorpicker/js/bootstrap-colorpicker.min.js"></script>
<!-- Tempusdominus Bootstrap 4 -->
<script src="../plugins/tempusdominus-bootstrap-4/js/tempusdominus-bootstrap-4.min.js"></script>
<!-- Bootstrap Switch -->
<script src="../plugins/bootstrap-switch/js/bootstrap-switch.min.js"></script>
<!-- BS-Stepper -->
<script src="../plugins/bs-stepper/js/bs-stepper.min.js"></script>
<!-- dropzonejs -->
<script src="../plugins/dropzone/min/dropzone.min.js"></script>
</body>
</html>

Source: Ask PHP

LEAVE A COMMENT