用PHP发送邮件的问题

时间:2015-08-18 06:12:09

标签: javascript php

我是php新手。我创建了一个样本表来通过邮件发送数据但是当我点击按钮时它会重定向到默认页面,当我刷新页面时,我收到邮件而不点击按钮。请帮助我解决我的问题。

这是我的代码:

 <div>
    <b> Hello <?php echo $_POST["name"]; ?>!</b><br>
    <b>Email    :</b> <?php echo $_POST["email"]; ?>.<br>
    <b>Gender   :</b> <?php echo $_POST["gender"]; ?>.<br>
 <b>Birthday    :</b>
  <?php
     $day = $_POST['day'];
     $month = $_POST['month'];
     $year = $_POST['year'];
     $date = $day."-".$month."-".$year;
     $my_date = date('d-m-Y', strtotime($date));
     echo $my_date;
    ?>
</div>

<form action="<?php echo $this->getPostActionUrl() ?>" method="post" id="vaccination-form">
  <div>
<table border="1" style="width:100%">

  <tr>
   <th id= "sno" style="font-family: sans-serif; font-size: 100%; font-weight: bold;" class="bg-color" width="5%">S.No</th>
   <th id= "vaccine" style="font-family: sans-serif; font-size: 100%; font-weight: bold;" class="bg-color center" width="32%">Vaccine</th>      
   <th id="decsription" style="font-family: sans-serif; font-size: 100%; font-weight: bold;">Description</th>
<th id="duedate" style="font-family: sans-serif; font-size: 100%; font-weight: bold;" class ="bg-color" width="15%">Due Date</th>

 </tr>
 <tr>
<td>1</td>
<td><a href="http://www.labwise.in/devel/hepatitis-b/" style="color: rgb(0,255,0)"><font color="#0000FF">Hepatitis B</font></a>
</td>       
<td>

        <b><span> At birth: </span></b><br></br>
        <span>For infants born to hepatitis B surface antigen (HBsAg)-positive mothers, administer HepB vaccine and 0.5 mL of hepatitis B immune globulin (HBIG) within 12 hours of birth. These infants should be tested for HBsAg and antibody to HBsAg (anti-HBs) 1 to 2 months after completion of the HepB series at age 9 through 18 months (preferably at the next well-child visit).</span>

     <div>
  <?php

      $date=date_create("now");
      $bdate=date_create($my_date);

      //echo date_format($dueDate,'d-m-Y');

        if($date >= $bdate)
        {
            $dueDate=$bdate->modify('+6 week');
            echo date_format($dueDate,"d-m-Y");
        }
        else
        {
            $bdate->modify('+2 week');
            echo date_format($bdate,'d-m-Y');
        }
   ?>

</div>


</td>
</tr> 
</table>



<div>
      <button type="submit" title="<?php echo $this->__('Email Me') ?>" value="submit "class="button"><span><span><?php echo $this->__('Email Me')?></span></span></button>

</div> 
</div>
</form>

<script type="text/javascript">
//<![CDATA[
    var dataForm = new VarienForm('vaccination-form', true);
//]]>
</script>
<script type="text/javascript">
 $.ajax({ 
        type: "POST", 
        url: "sendmail.php", 
        success: function(response) {
            alert(response);
  }
});
</script>

sendmail.php

<?php

        if (isset($_POST["submit"]))
        { 
            $email=$_POST['email'];
            $name=$_POST['name'];
            $to=$email;
            $subject= "New Application";
            $message= "Name: ".$name;
            $headers= "From: admin@labwise.in" . "\r\n" ;
            mail($to, $subject, $message, $headers);
        }

?> 

2 个答案:

答案 0 :(得分:0)

在这里,我添加了有关您需要的所有文件。

您的第一个文件

    <div>
        <b> Hello <?php echo $_POST["name"]; ?>!</b><br>
        <b>Email    :</b> <?php echo $_POST["email"]; ?>.<br>
        <b>Gender   :</b> <?php echo $_POST["gender"]; ?>.<br>
     <b>Birthday    :</b>
      <?php
         $day = $_POST['day'];
         $month = $_POST['month'];
         $year = $_POST['year'];
         $date = $day."-".$month."-".$year;
         $my_date = date('d-m-Y', strtotime($date));
         echo $my_date;
        ?>
    </div>
    <form action="sendmail.php" method="post" id="vaccination-form">
        <div>
            <table border="1" style="width:100%">
                <tr>
                    <th id= "sno" style="font-family: sans-serif; font-size: 100%; font-weight: bold;" class="bg-color" width="5%">S.No</th>
                    <th id= "vaccine" style="font-family: sans-serif; font-size: 100%; font-weight: bold;" class="bg-color center" width="32%">Vaccine</th>      
                    <th id="decsription" style="font-family: sans-serif; font-size: 100%; font-weight: bold;">Description</th>
                    <th id="duedate" style="font-family: sans-serif; font-size: 100%; font-weight: bold;" class ="bg-color" width="15%">Due Date</th>
                </tr>
                <tr>
                    <td>1</td>
                    <td><a href="http://www.labwise.in/devel/hepatitis-b/" style="color: rgb(0,255,0)"><font color="#0000FF">Hepatitis B</font></a></td>       
                    <td><b><span> At birth: </span></b><br></br>
                    <span>For infants born to hepatitis B surface antigen (HBsAg)-positive mothers, administer HepB vaccine and 0.5 mL of hepatitis B immune globulin (HBIG) within 12 hours of birth. These infants should be tested for HBsAg and antibody to HBsAg (anti-HBs) 1 to 2 months after completion of the HepB series at age 9 through 18 months (preferably at the next well-child visit).</span></td>
                </tr> 
            </table>
            <div>
                    <input type="hidden"  name="email" value="<?php echo $_POST["email"]; ?>">
                    <input type="hidden"  name="name" value="<?php echo $_POST["name"]; ?>">
                    <input type="hidden"  name="dueDate" value="<?php echo date_format($bdate,'d-m-Y'); ?>">
                    <button type="submit" name="submit" title="<?php echo $this->__('Email Me') ?>" value="submit "class="button"><span><span><?php echo $this->__('Email Me')?></span></span></button>
            </div> 
        </div>
    </form>

<script type="text/javascript">
//<![CDATA[
    var dataForm = new VarienForm('vaccination-form', true);
//]]>
</script>

现在是您的sendmail.php

<?php
        $email=$_POST['email'];
        $name=$_POST['name'];
        $dudate=$_POST['dueDate'];//do what you want to do with due date
        $to=$email;
        $subject= "New Application";
        $message= "Name: ".$name;
        $headers= "From: admin@labwise.in" . "\r\n" ;
        mail($to, $subject, $message, $headers);
        echo 'sucess';
?> 

只需替换文件中的两个代码即可。

试试这个,让我知道发生了什么......

检查邮件()功能

$to         = '===YOUR to Address==';
$uid        = md5(uniqid(time()));
$header     = "From: ".'==NAME=='." <".'==EMAIL ADDRESS=='.">\r\n";
$header    .= "MIME-Version: 1.0\r\n";
$header    .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header    .= "This is a multi-part message in MIME format.\r\n";
$header    .= "--".$uid."\r\n";
$header    .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header    .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header    .= "Hi    \r\n\r\n";
$header    .= "=====MESSAGE 1===== \n\n";
$header    .= "=====MESSAGE 2=====  \r\n\r\n";
$sub        = "====SUBJECT OF MAIL====";
mail($to, $sub, "" , $header);

在这种方法中,您可以在电子邮件中添加更多消息..它也很简单..

答案 1 :(得分:0)

<?php
if (isset($_POST["submit"]))
  {
        $email=$_POST['email'];
        $name=$_POST['name'];
        $to=$email;
        $subject= "New Application";
        $message= "Name: ".$name;
        $headers= "From: admin@labwise.in" . "\r\n" .
        mail($to, $subject, $message, $headers);
        unset($_POST["submit"]);
    }
        ?> 

设置条件以检查单击提交按钮的位置,然后取消设置以备将来使用