1
votes
 <?php

  if (isset($_REQUEST['email']))  {


  $admin_email = "[email protected]";
  $email = $_REQUEST['email'];
  $subject = $_REQUEST['subject'];
   $comment = $_REQUEST['comment'];


   ini_set("SMTP","localhost");
   ini_set("smtp_port","25");
   ini_set("sendmail_from",$email);

   mail($admin_email, "$subject", $comment, "From:" . $email);
    echo "Thank you for contacting us!";
   }


   else  {
  ?>

  <form method="post">
   Email: <input name="email" type="text" /><br />
   Subject: <input name="subject" type="text" /><br />
   Message:<br />
  <textarea name="comment" rows="15" cols="40"></textarea><br />
  <input type="submit" value="Submit" />
  </form>

  <?php
  }
 ?>

error

mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()**

1
Do you have a mail server running on your machine? - Jirka Hrazdil
use phpmailer for smtp - Aswad Shaik
i have no any mail server in my machine - Bhargav Chudasama
You just gave yourself the explanation why it does not work. Thx @jiri-hrazdil! - dr0i

1 Answers

-2
votes

try this it useful to you!

<?php
include "PHPMailer_5.2.4/class.phpmailer.php";

  if (isset($_REQUEST['smtp']))  {
    $email=$_POST['email'];
$mail        = new PHPMailer();

$mail->IsSMTP(); 
$mail->SMTPDebug = 1; 
$mail->SMTPAuth = true; 
$mail->SMTPSecure = 'ssl'; 
$mail->Host = "smtp.gmail.com";
$mail->Port = 465; 
$mail->IsHTML(true);
$mail->Username = "[email protected]";
$mail->Password = "password";
    $mail->AddAddress($email, 'ashu');
    $mail->Subject = "SMTP Receivced";
    $mail->Body    = "<b>Succesfully SMTP Receivced</b>";
    $text = 'Text version of email';
    $html = '<html><body>HTML version of email</body></html>';
    $file = 'index.php';
    $crlf = "\n";
    $hdrs = array(
        'email' => '[email protected]',
        'Subject' => 'Test subject message',
        'comment' => 'Test comment',
    );
    if ($mail->send($hdrs))
    //if (mail($subject,$message, $headers))
        {
        echo "<script> alert('Thank you for contacting us!');window.location = '';</script>";
    } else {
        echo "Mailed Error: " . $mail->ErrorInfo;
    }

  }
  ?>

  <form method="post">
   Email: <input name="email" type="text" /><br />
   Subject: <input name="subject" type="text" /><br />
   Message:<br />
  <textarea name="comment" rows="15" cols="40"></textarea><br />
  <input type="submit" name="smtp" value="Submit" />
  </form>

enter image description here