2013-11-28 34 views
1

我是PHP编码新手。我创建了一个Customer表单,然后是发送电子邮件功能。当客户提交数据(包括电子邮件和密码)时,他应该收到带有用户ID(与输入的电子邮件ID相同)的确认电子邮件并输入密码。
此时,用我现在的代码,当客户提交表单时,数据库会被更新,但是没有电子邮件发送给用户。任何人都可以指出需要更改哪些内容以确保发送电子邮件功能正常工作。邮件发送功能没有从.php调用客户表单

<!DOCTYPE html> 
<html> 
    <head> 
     <title>Registration Desk</title> 

    </head> 
    <body> 
     <div class="form"> 
      <form id="contactform" action="reg_submit.php" method="post"> 
       <p class="contact"><label for="name">Name</label></p> 
       <input id="name" name="name" placeholder="First and last name" required="" tabindex="1" type="text"> 

       <p class="contact"><label for="add">Address</label></p> 
       <textarea id="add" name="add" style="width:85%; height:60%; margin-top:1%" required=""></textarea> 

       <fieldset> 
        <label>Birthday</label> 
        <input type="date" name="dob" value="yyyy-mm-dd" required=""> 
       </fieldset> 



       <label>I am</label>&nbsp;&nbsp;&nbsp;&nbsp;<br><br> 


       <input type="radio" name="sex" value="male" >Male &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
       <input type="radio" name="sex" value="female">Female 




       <p class="contact"><label for="email">Email</label></p> 
       <input id="email" name="email" class="field" placeholder="Please enter a valid emailid" required="" type="email"> 


       <p class="contact"><label for="password">Create a password</label></p> 
       <input type="password" id="password" name="password" required=""> 
       <p class="contact"><label for="repassword">Confirm your password</label></p> 
       <input type="password" id="repassword" name="repassword" required=""> 


       <br><br> 

       <p class="contact"><label for="phone">Mobile phone</label></p> 
       <input id="phone" name="phone" placeholder="phone number" required="" type="text"> <br> 
       <input class="buttom" name="submit" id="submit" tabindex="5" value="Sign me up!" type="submit">  
      </form> 
     </div>  
     </div> 

    </body> 
</html> 
<?php 
error_reporting(0); 
$connect=mysqli_connect("localhost","wbtecqoj_saltee","webuildtec1","wbtecqoj_salteegroup"); 
if(mysqli_connect_errno()) 
{ 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 


if($_POST["password"]!= $_POST["repassword"]) 
{ 
?> 
    <script type="text/javascript"> 
    alert("password miss matched!!!."); 
    history.back(); 


    </script> 

<?php 

} 
if($_POST["name"] && $_POST["add"] && $_POST["phone"] && $_POST["email"] && $_POST["password"] && $_POST["dob"] ) 
{ 
    $insert="INSERT INTO `db`.`new_user` (`id`, `name`, `add`, `contact`, `email`, `pass`, `dob`, `gender`,`code`) VALUES (NULL, '$_POST[name]', '$_POST[add]', '$_POST[phone]', '$_POST[email]', '$_POST[password]', '$_POST[dob]', '$_POST[sex]', 'SAL1000000')"; 

    if(!mysqli_query($connect,$insert)) 
    { 
     echo die('Error: ' . mysqli_error($connect)); 
    } 
    else 
    { 

     $to=$_POST['email']; 
     $from= "[email protected]"; 
     $subject="Welcome to xyz Group"; 


     $message="Welcome " . $_POST['name'] . "Dear Customer, 
Your Card Number is " .$Ccode.$cookieId. " User id -" . $_POST['email'] . "Your Password -" . $_POST['password']. 
"Thanking You, 
Customer Care,"; 
     $header = "From" .$from; 

     if(isset($_POST['btnSend'])) 
     { 
      $res = mail($to,$subject,$message,$header); 
      if($res) 
      { 
       echo 'Message send'; 
      } 
      else 
      { 
       echo 'msg not send'; 
      } 
     } 

}} 
mysqli_close($connect); 
?> 
+0

你不能从本地主机发送邮件...如果你想从本地发送邮件,那么你必须使用PHP邮件程序 –

+0

Web服务器和邮件服务器是同一台机器吗? – rawdog

+0

我使用Bigrock作为我的虚拟主机,这是网站所说的 - 关于SMTP服务器设置 - http://manage.bigrock.com/kb/servlet/KBServlet/faq1311.html - 希望这有助于。 – user3045396

回答

2

你检查

if(isset($_POST['btnSend'])) 

但表单不包含名为btnSend输入,你应该能够删除if语句完全

3

删除

if(isset($_POST['btnSend'])) 
    { 
} 
+0

谢谢王子,那确实是一个错字错误。我已经删除了相同的,但我得到错误-'msg不发送':( – user3045396

+0

使用您的域名smtp服务器名称,而不是本地主机 –

0

u必须下载这不得不文件 PHP mailer

class.phpmailer.phpclass.smtp.php然后加在所有具有如下所示的PHP代码相同的目录文件的PHP邮件库..

<?PHP 
require_once('class.phpmailer.php'); 

if('POST' == $_SERVER['REQUEST_METHOD']) { 
     // process the form here 

    $subject = "This is subject testing1"; 
    $mail = new PHPMailer();  
    $mail->IsSMTP(); 
    $mail->IsHTML(true); // send as HTML  
    $mail->SMTPAuth = true;     // enable SMTP authentication 
    $mail->SMTPSecure = "ssl";     // sets the prefix to the servier 
    $mail->Host  = "smtp.gmail.com";  // sets GMAIL as the SMTP server 
    $mail->Port  = 465;     // set the SMTP port 
    //$mail->Username = "gmailusername";     // GMAIL username 
    //$mail->Password = "password";           // GMAIL password 
    $mail->From  = 'email id'; 
    $mail->FromName = "FrontName"; 
    $mail->Subject = $subject; 
    $mail->Body  = "this is html body";   //HTML Body 
    $emailId='email id'; 
    $emails = explode(",",$emailId); 
    foreach($emails as $email) 
      $mail->AddAddress($email); 

    if($mail->Send()){ 
     echo "Message sent successfully"; 
    }else{ 
     echo "Mailer Error: " . $mail->ErrorInfo; 
    } 
} 
    ?> 
<form action="" method="post"> 
    <input type="submit" name='button1'value="sent mail" /> 
</form> 
+0

@谢谢Mayur - 我做了更改 - 但得到同样的错误顺便说一句,我已经提到服务器名称为“本地主机”,因为我的网站主机提到它的具体情况。任何其他建议? – user3045396

0

我建议你使用PHPMailer从 http://sourceforge.net/projects/phpmailer/

使用PHPMailer类,您可以指定传出SMTP服务器并正确进行身份验证。这里是我如何使用它的一个例子:

<?php 
require("class.phpmailer.php"); 

class Mailer 
{ 
    function send($email, $subject, $body, $attachment1) 
    { 
     $mail = new PHPMailer(); 

     $mail->IsSMTP();         // send via SMTP 
     $mail->Host  = "mail.xyz.com"; // your SMTP servers 
     $mail->SMTPAuth = true;  // turn on SMTP authentication 
     $mail->Username = "authname"; // SMTP username 
     $mail->Password = "authpassword"; // SMTP password 

     $mail->From  = "[email protected]"; 
     $mail->FromName = "[email protected]"; 
     $mail->AddAddress($email); 

     $mail->WordWrap = 50;        // set word wrap 
     if ($attachment1 != "") { 
      $mail->AddAttachment($attachment1);  // attachment1 
     } 
     $mail->IsHTML(false);        // send as HTML 

     $mail->Subject = $subject; 
     $mail->Body  = $body; 

     if(!$mail->Send()) 
     { 
      return -1; 
     } 
     else 
     { 
      return 0; 
     } 
    } 
} 
?> 

希望帮助!

+0

@ rawdog - 感谢您的建议 - 我现在要试试这个并分享这个反馈。干杯! – user3045396

+0

这两个答案与删除'if(isset($ _ POST ['btnSend']))'也是正确的!我的建议是让发送过程更加稳定和安全。 – rawdog