提交表单后发送欢迎电子邮件

时间:2014-10-15 09:11:16

标签: php forms email

这是我的sign_up.php代码,我希望用户在点击提交按钮后立即收到欢迎电子邮件,我搜索了很多论坛,但他们没有给我我需要的东西。

用户成功注册,详细信息存储在数据库中,但我还想添加一个欢迎邮件功能,以便我在提交表单后立即将详细信息发送到电子邮件

 <?php
 //We check if the form has been sent
 if(isset($_POST['username'], $_POST['password'], $_POST['passverif'], $_POST['email'],     $_POST['avatar']) and $_POST['username']!='')
 {
//We remove slashes depending on the configuration
if(get_magic_quotes_gpc())
{
    $_POST['username'] = stripslashes($_POST['username']);
    $_POST['password'] = stripslashes($_POST['password']);
    $_POST['passverif'] = stripslashes($_POST['passverif']);
    $_POST['email'] = stripslashes($_POST['email']);
    $_POST['avatar'] = stripslashes($_POST['avatar']);
    $_POST['mobile'] = stripslashes($_POST['mobile']);
}
//We check if the two passwords are identical
if($_POST['password']==$_POST['passverif'])
{
    //We check if the password has 6 or more characters
    if(strlen($_POST['password'])>=6)
    {
        //We check if the email form is valid
        if(preg_match('#^(([a-z0-9!\#$%&\\\'*+/=?^_`{|}~-]+\.?)*[a-z0-9!\#$%&\\\'*+/=?^_`{|}~-]+)@(([a-z0-9-_]+\.?)*[a-z0-9-_]+)\.[a-z]{2,}$#i',$_POST['email']))
        {
            //We protect the variables
            $username = mysql_real_escape_string($_POST['username']);
            $password = mysql_real_escape_string($_POST['password']);
            $email = mysql_real_escape_string($_POST['email']);
            $avatar = mysql_real_escape_string($_POST['avatar']);
            $mobile = mysql_real_escape_string($_POST['mobile']);
            //We check if there is no other user using the same username
            $dn = mysql_num_rows(mysql_query('select id from users where username="'.$username.'"'));
            if($dn==0)
            {
                //We count the number of users to give an ID to this one
                $dn2 = mysql_num_rows(mysql_query('select id from users'));
                $id = $dn2+1;
                //We save the informations to the databse
                if(mysql_query('insert into users(id, username, password, email, avatar, mobile, signup_date) values ('.$id.', "'.$username.'", "'.$password.'", "'.$email.'", "'.$avatar.'", "'.$mobile.'", "'.time().'")'))
                {
                    //We dont display the form
                    $form = false;

                    //mail function

                    //mail end
 ?>

 <div class="message">Your Registration was successful. Please login below<br />
 <a href="connexion.php">Log in</a></div>
 <?php
                }
                else
                {
                    //Otherwise, we say that an error occured
                    $form = true;
                    $message = 'An error occurred while signing up.';
                }
            }
            else
            {
                //Otherwise, we say the username is not available
                $form = true;
                $message = 'The username you want to use is not available, please choose another one.';
            }
        }
        else
        {
            //Otherwise, we say the email is not valid
            $form = true;
            $message = 'The email you entered is not valid.';
        }
    }
    else
    {
        //Otherwise, we say the password is too short
        $form = true;
        $message = 'Your password must contain at least 6 characters.';
    }
     }
     else
     {
    //Otherwise, we say the passwords are not identical
    $form = true;
    $message = 'The passwords you entered are not identical.';
}
 }
 else
 {
 $form = true;
 }
 if($form)
 {
//We display a message if necessary
     if(isset($message))
     {
    echo '<div class="message">'.$message.'</div>';
}
//We display the form
 ?>
 <div class="content">
 <?php include('adverts.php'); ?>
 <br />
  <h1 style="color:#666;">New User Registration</h1>

 <table class="message">
 <form action="sign_up.php" method="post" class="message">
 <tr>
 <td>Username</td><td><input type="text" name="username" value="<?php      if(isset($_POST['username'])){echo htmlentities($_POST['username'], ENT_QUOTES, 'UTF-8');}      ?>" /></td>
 </tr>
 <tr>
 <td>Password<span class="small">(6 characters min.)</span></td><td><input     type="password" name="password" /></td>
 </tr>
 <tr>
 <td>Password<span class="small">(verification)</span></td><td><input type="password"     name="passverif" /></td>
 </tr>
 <tr>
 <td>Email</td><td><input type="text" name="email" value="<?php      if(isset($_POST['email'])){echo htmlentities($_POST['email'], ENT_QUOTES, 'UTF-8');} ?>" />     </td>
 </tr>
 <tr>
 <td>Gender<span class="small">(optional)</span></td><td><input type="text" name="avatar" value="<?php if(isset($_POST['avatar'])){echo htmlentities($_POST['avatar'], ENT_QUOTES, 'UTF-8');} ?>" /></td>
 </tr>
 <tr><td>Mobille</td><td><input type="text" name="mobile" value="<?php           if(isset($_POST['mobile'])){echo htmlentities($_POST['mobile'], ENT_QUOTES, 'UTF-8');} ?>" /></td></tr>
 <tr>
 <td></td>
 <td><input type="submit" value="Sign up" /></td>
 </tr>

 </form>
 </table>

 </div>

 <?php
 }
 ?>

5 个答案:

答案 0 :(得分:1)

成功提交表单后,您需要为您的邮件功能编写代码

<?php 
  if(isset($_POST['submit'])){
    $to = $_POST['email']; 
    $subject = $_POST['name'];
    $message = $_POST['message'];
    $from = "test@testcom";
    $headers = "From:" . $from;
     if(mail($to,$subject,$message,$headers))
     {
       echo "Mail Sent.";
     }
     else
     {
       echo "Something went wrong"; 
     }
  }
?>

答案 1 :(得分:0)

发送邮件的功能是mail()

mail($email,'Subject','Message_body'); ...

之后添加此if(mysql_query('insert into users

http://www.w3schools.com/php/php_mail.asp

答案 2 :(得分:0)

只需使用邮件功能并发送邮件,如下所示

<?php
    $result = mysql_query('insert into users(id, username, password, email, avatar, mobile, signup_date) values ('.$id.', "'.$username.'", "'.$password.'", "'.$email.'", "'.$avatar.'", "'.$mobile.'", "'.time().'")');
    if($result){
        //We dont display the form
        $form = false;
        //mail function
        mail("mail_address@mail.com",'Subject','Message_body');
        //mail end
 ?>
<div class="message">Your Registration was successful. Please login below<br />
        <a href="connexion.php">Log in</a></div>
 <?php
    }
?>

答案 3 :(得分:0)

试试用户,管理员会收到邮件

 <?php
     //We check if the form has been sent
     if(isset($_POST['username'], $_POST['password'], $_POST['passverif'], $_POST['email'],     $_POST['avatar']) and $_POST['username']!='')
     {
    //We remove slashes depending on the configuration
    if(get_magic_quotes_gpc())
    {
        $_POST['username'] = stripslashes($_POST['username']);
        $_POST['password'] = stripslashes($_POST['password']);
        $_POST['passverif'] = stripslashes($_POST['passverif']);
        $_POST['email'] = stripslashes($_POST['email']);
        $_POST['avatar'] = stripslashes($_POST['avatar']);
        $_POST['mobile'] = stripslashes($_POST['mobile']);
    }
    //We check if the two passwords are identical
    if($_POST['password']==$_POST['passverif'])
    {
        //We check if the password has 6 or more characters
        if(strlen($_POST['password'])>=6)
        {
            //We check if the email form is valid
            if(preg_match('#^(([a-z0-9!\#$%&\\\'*+/=?^_`{|}~-]+\.?)*[a-z0-9!\#$%&\\\'*+/=?^_`{|}~-]+)@(([a-z0-9-_]+\.?)*[a-z0-9-_]+)\.[a-z]{2,}$#i',$_POST['email']))
            {
                //We protect the variables
                $username = mysql_real_escape_string($_POST['username']);
                $password = mysql_real_escape_string($_POST['password']);
                $email = mysql_real_escape_string($_POST['email']);
                $avatar = mysql_real_escape_string($_POST['avatar']);
                $mobile = mysql_real_escape_string($_POST['mobile']);
                //We check if there is no other user using the same username
                $dn = mysql_num_rows(mysql_query('select id from users where username="'.$username.'"'));
                if($dn==0)
                {
                    //We count the number of users to give an ID to this one
                    $dn2 = mysql_num_rows(mysql_query('select id from users'));
                    $id = $dn2+1;
                    //We save the informations to the databse
                    if(mysql_query('insert into users(id, username, password, email, avatar, mobile, signup_date) values ('.$id.', "'.$username.'", "'.$password.'", "'.$email.'", "'.$avatar.'", "'.$mobile.'", "'.time().'")'))
                    {
                        //We dont display the form
                        $form = false;

                        //mail function

                        //mail end
                        $to = "$email";
    $subject = "Welcome to";
    $message = " Hi $username,<br /><br />
    Thank you for signing up with us.<br />

    Thanks <br />";
    // Always set content-type when sending HTML email
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
    // More headers
    $headers .= 'From: <test@gmail.com>' . "\r\n";
    $mail=mail($to,$subject,$message,$headers);
    if($mail)   
    {
    $to = "admin@gmail.com";

    $subject = "Following Customer Signed Up";
    $message = " $username,Customer is signed up with us,<br /><br />
    Customer Details:<br />First Name:$firstname<br/>Last Name:$lastname<br/>Email:$email<br/>
    Phone:$phone<br/>Zip Code:$zip<br/>
    Thanks <br />";
    // Always set content-type when sending HTML email
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
    // More headers
    $headers .= 'From: <'.$email.'>' . "\r\n";
    $mail=mail($to,$subject,$message,$headers);
    }
     ?>

     <div class="message">Your Registration was successful. Please login below<br />
     <a href="connexion.php">Log in</a></div>
     <?php
                    }
                    else
                    {
                        //Otherwise, we say that an error occured
                        $form = true;
                        $message = 'An error occurred while signing up.';
                    }
                }
                else
                {
                    //Otherwise, we say the username is not available
                    $form = true;
                    $message = 'The username you want to use is not available, please choose another one.';
                }
            }
            else
            {
                //Otherwise, we say the email is not valid
                $form = true;
                $message = 'The email you entered is not valid.';
            }
        }
        else
        {
            //Otherwise, we say the password is too short
            $form = true;
            $message = 'Your password must contain at least 6 characters.';
        }
         }
         else
         {
        //Otherwise, we say the passwords are not identical
        $form = true;
        $message = 'The passwords you entered are not identical.';
    }
     }
     else
     {
     $form = true;
     }
     if($form)
     {
    //We display a message if necessary
         if(isset($message))
         {
        echo '<div class="message">'.$message.'</div>';
    }
    //We display the form
     ?>
     <div class="content">
     <?php include('adverts.php'); ?>
     <br />
      <h1 style="color:#666;">New User Registration</h1>

     <table class="message">
     <form action="sign_up.php" method="post" class="message">
     <tr>
     <td>Username</td><td><input type="text" name="username" value="<?php      if(isset($_POST['username'])){echo htmlentities($_POST['username'], ENT_QUOTES, 'UTF-8');}      ?>" /></td>
     </tr>
     <tr>
     <td>Password<span class="small">(6 characters min.)</span></td><td><input     type="password" name="password" /></td>
     </tr>
     <tr>
     <td>Password<span class="small">(verification)</span></td><td><input type="password"     name="passverif" /></td>
     </tr>
     <tr>
     <td>Email</td><td><input type="text" name="email" value="<?php      if(isset($_POST['email'])){echo htmlentities($_POST['email'], ENT_QUOTES, 'UTF-8');} ?>" />     </td>
     </tr>
     <tr>
     <td>Gender<span class="small">(optional)</span></td><td><input type="text" name="avatar" value="<?php if(isset($_POST['avatar'])){echo htmlentities($_POST['avatar'], ENT_QUOTES, 'UTF-8');} ?>" /></td>
     </tr>
     <tr><td>Mobille</td><td><input type="text" name="mobile" value="<?php           if(isset($_POST['mobile'])){echo htmlentities($_POST['mobile'], ENT_QUOTES, 'UTF-8');} ?>" /></td></tr>
     <tr>
     <td></td>
     <td><input type="submit" value="Sign up" /></td>
     </tr>

     </form>
     </table>

     </div>

     <?php
     }
     ?>

答案 4 :(得分:0)

谢谢大家的帮助。

我已经成功添加了一个简单的邮件功能,你们所有人都提供了帮助,这就是我所做的:

我添加了

mail($email,'Subject','Message_body');

    if(mysql_query('insert into users(id, username, password, email, avatar, signup_date) values ('.$id.', "'.$username.'", "'.$password.'", "'.$email.'", "'.$avatar.'", "'.time().'")'))
    {
//We dont display the form
$form = false; 

就像这样:

mail("$email",'Welcome To Naijabloom','Dear user, <br /> Welcome to Naijabloom.com, you will start receiving mails from us to keep you updated. Remember to be active in our forums and invite your friends here. Thanks. <br /> Naijanloom Team.', 'info@naijabloom.com');

它对我有用,谢谢