邮件与PHP无法正常工作

时间:2016-06-29 03:51:47

标签: php email phpmailer

这是我表格的代码。

<form method="post" action="mailer.php" id="contactfrm">
        <div class="col-sm-4">
          <div class="form-group">
            <label for="name">Name</label>
            <input type="text" class="form-control" name="name" id="name" placeholder="Enter name" title="Please enter your name (at least 2 characters)">
          </div>
          <div class="form-group">
            <label for="email">Email</label>
            <input type="email" class="form-control" name="email" id="email" placeholder="Enter email" title="Please enter a valid email address">
          </div>
        </div>
        <div class="col-sm-4">
          <div class="form-group">
            <label for="comments">Message</label>
            <textarea name="message" class="form-control" id="comments" cols="3" rows="5" placeholder="Enter your message…" title="Please enter your message (at least 10 characters)"></textarea>
          </div>
          <button name="submit" type="submit" class="btn btn-lg btn-primary" id="submit">Submit</button>
          <div class="result"></div>
        </div>
      </form>

我的 mailer.php

<?php
$replyemail="my email"; 
$name = $_POST["name"];
$email = $_POST["email"];
$thesubject = "Project With Me Query";
$themessage = $_POST["message"];
$success_sent_msg='<p align="center"><strong>&nbsp;</strong></p>
                   <p align="center"><strong>Your message has been successfully sent to My Email<br>
                   </strong> and I will reply as soon as possible.</p>
                   <p align="center">A copy of your query has been sent to you.</p>
                   <p align="center">Thank you for contacting Me.</p>';

$replymessage = "Hi $name

Thank you for your email.

We will endeavour to reply to you shortly.

Please DO NOT reply to this email.

Below is a copy of the message you submitted:
--------------------------------------------------
Subject: $thesubject
Query:
$themessage
--------------------------------------------------

Thank you";

$themessage = "name: $name \nQuery: $themessage";
mail("$replyemail",
     "$thesubject",
     "$themessage",
     "From: $email\nReply-To: $email");
mail("$email",
     "Receipt: $thesubject",
     "$replymessage",
     "From: $replyemail\nReply-To: $replyemail");
echo $success_sent_msg;

echo '<script>setTimeout(function(){location.href="index.php"} , 5000);   </script>';
?>

我无法弄清楚我做错了什么。 每当我填写显示的成功消息的信息时。但我没有收到任何有关该信息的电子邮件。

有人可以修复此现有代码或为我提供新的邮件代码吗?

1 个答案:

答案 0 :(得分:1)

您的表单

<form method="post" action="1.php" id="contactfrm">
        <div class="col-sm-4">
          <div class="form-group">
            <label for="name">Name</label>
            <input type="text" class="form-control" name="name" id="name" placeholder="Enter name" title="Please enter your name (at least 2 characters)">
          </div>
          <div class="form-group">
            <label for="email">Email</label>
            <input type="email" class="form-control" name="email" id="email" placeholder="Enter email" title="Please enter a valid email address">
          </div>
        </div>
        <div class="col-sm-4">
          <div class="form-group">
            <label for="comments">Message</label>
            <textarea name="message" class="form-control" id="comments" cols="3" rows="5" placeholder="Enter your message…" title="Please enter your message (at least 10 characters)"></textarea>
          </div>
          <button name="submit" type="submit" class="btn btn-lg btn-primary" id="submit">Submit</button>
          <div class="result"></div>
        </div>
      </form>

您的php代码与smtp

<?php
if(isset($_POST["submit"])){
$replyemail="my email"; 
$name = $_POST["name"];
$email = $_POST["email"];
$thesubject = "Project With Me Query";
$themessage = $_POST["message"];
$success_sent_msg='<p align="center"><strong>&nbsp;</strong></p>
                   <p align="center"><strong>Your message has been successfully sent to My Email<br>
                   </strong> and I will reply as soon as possible.</p>
                   <p align="center">A copy of your query has been sent to you.</p>
                   <p align="center">Thank you for contacting Me.</p>';

$replymessage = "Hi $name

Thank you for your email.

We will endeavour to reply to you shortly.

Please DO NOT reply to this email.

Below is a copy of the message you submitted:
--------------------------------------------------
Subject: $thesubject
Query:
$themessage
--------------------------------------------------

Thank you";

$themessage = "name: $name \nQuery: $themessage";
include "PHPMailer_5.2.4/class.phpmailer.php"; 
$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 = "yourusername@gmail.com";
$mail->Password = "yourgmailpassword";
$mail->AddReplyTo($replymessage, "Reply name");
$mail->AddAddress($email,'ashu');
$mail->Subject = "SMTP Receivced";
$mail->Body = "<b>Succesfully SMTP Receivced</b>";

$mail->MsgHTML($success_sent_msg);
$text = 'Text version of email';
$html = '<html><body>HTML version of email</body></html>';
$file = 'index.php';
$crlf = "\n";
$hdrs = array(
              'From'    => 'you@yourdomain.com',
              'Subject' => 'Test mime message'
              );
if($mail->send($hdrs))
 {
echo "<script> alert('Successfully Mailed');window.location = '';</script>";
}
else{
echo "Mailed Error: " . $mail->ErrorInfo;
}
}

//echo '<script>setTimeout(function(){location.href="pra-2.php"} , 5000);   </script>';
?>

enter image description here