mail()函数ini_set()smtp服务器localhost不起作用

时间:2017-01-23 09:37:37

标签: php html smtp

 <?php

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


  $admin_email = "example@gmail.com";
  $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
  }
 ?>

错误

  

mail():无法连接到&#34; localhost&#34;的邮件服务器端口25,验证您的&#34; SMTP&#34;和&#34; smtp_port&#34;在php.ini中设置或使用ini_set()**

1 个答案:

答案 0 :(得分:-1)

试试这对你有用!

<?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 = "userid@gmail.com";
$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' => 'example@gmail.com',
        '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