发送随机数给PHP发送电子邮件

时间:2015-11-02 07:07:30

标签: php email random

我正在尝试向电子邮件发送一个随机数,但是无法这是我迄今为止能够提出的代码 请尽可能帮助

sendmail.php

$to = $_POST['email'];
$header ='From:admin@domain.com';
$subject ='Verification Code';  

    if(empty($to))
        {
            echo "Email is Empty";
        }
        else
        {
            if (mail($to,$subject,$message,$header)){

             echo 'Check Your Email FOr verfication code';
            }
            else{
                echo 'Failed';
            } 
        }

index.php

<form action = "register.php" method="POST">

<p>Username</p>
<input type="text" name="username" maxlength="40" value='<?php if(isset($username)) echo $username ?>'"><br>

<p>New Password</p>
<input type="password" name="password"> 

 <p>email</p>
<input type="text" name="email" maxlength="40" value='<?php if(isset($email)) echo $email ?>'"> <br><br>
<input type="submit" value="Register"> <br><br>
</form>

3 个答案:

答案 0 :(得分:2)

格式( index.php

<form action="sendmail.php" method="post">
    <lable>email</lable>>
    <input type="text" name="email" maxlength="40">
    <br>

    <input type="submit" value="Register"> 

</form>

在sendmail.php

$rand= rand(10, 20)// random number generator
$to = $_POST['email'];
$header ='From:admin@domain.com';
$subject ='Verification Code';

$message = "Your Random number is";
$message .= $rand;
$message .= "Thank you-Admin";

if(empty($to))
{
    echo "Email is Empty";
}
else
{
    if (mail($to,$subject,$message,$header)){

     echo 'Check Your Email FOr verfication code';
    }
    else{
        echo 'Failed';
    } 
}

答案 1 :(得分:1)

您忘记在第一行末尾添加分号

$rand= mt_rand(100000, 999999)

应该是

$rand= mt_rand(100000, 999999);

答案 2 :(得分:0)

我认为您使用的托管服务不接受使用mail()函数发送。可能出于安全原因,阻止类似病毒的PHP脚本从您的帐户发送垃圾邮件 因此,请创建admin@domain.com电子邮件帐户并使用PHPMailer发送您的电子邮件:

require __DIR__.'/phpmailer/PHPMailerAutoload.php';

$mail = new PHPMailer;
$mail->isSMTP();                    
$mail->Host = 'smtp.hostname.com';  
$mail->SMTPAuth = true;             
$mail->Username = 'no-reply@domain.com';                 
$mail->Password = 'secret';                           
$mail->SMTPSecure = 'tls';
$mail->Port = 587;    
$mail->setFrom('admin@domain.com');
$mail->addAddress($to);
$mail->isHTML(true);   
$mail->Subject = $subject;
$mail->Body    = $body;

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}