2015-11-02 13 views
0

我試圖一個隨機數發送到電子郵件,但不能這是我已經能夠拿出這麼遠 請幫助,如果可能的發送隨機數到電子郵件PHP

sendmail的代碼。 PHP

$to = $_POST['email']; 
$header ='From:[email protected]'; 
$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> 
+0

你面臨什麼問題? –

+0

電子郵件將不會發送 – Kunz

+0

它說失敗; – Kunz

回答

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:[email protected]'; 
$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'; 
    } 
} 
+0

現在錯誤是電子郵件是空的 – Kunz

+0

oopz sory。添加此形式'方法=「帖子」' –

+0

我編輯了我的問題與適當的細節,我仍然收到一個錯誤的電子郵件爲空 – Kunz

0

我認爲你正在使用不接受使用的mail()函數發送的託管服務。也許出於安全原因,防止像病毒一樣的PHP腳本從您的帳戶發送垃圾郵件。
因此,創建[email protected]電子郵件帳戶,並使用PHPMailer發送電子郵件:

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

$mail = new PHPMailer; 
$mail->isSMTP();      
$mail->Host = 'smtp.hostname.com'; 
$mail->SMTPAuth = true;    
$mail->Username = '[email protected]';     
$mail->Password = 'secret';       
$mail->SMTPSecure = 'tls'; 
$mail->Port = 587;  
$mail->setFrom('[email protected]'); 
$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'; 
} 
+0

它我能夠通過指定的東西發送電子郵件直接 – Kunz

+0

你知道,我在共享主機太有一天,郵件網站()停止工作了。解決方案是使用phpmailer,試試看,至少會看到$ mail-> ErrorInfo(: – num8er

1

你忘了在末尾添加分號第一行

$rand= mt_rand(100000, 999999) 

應該是

$rand= mt_rand(100000, 999999);