2017-02-18 46 views
0
不工作

沒有被執行下面的功能:郵件()內功能

function sendCode(){ 
    $our_mail = '[email protected]' 
    $to = $email; 
    $msg = "Hi $first_name, \n\nYour email has been used to sign up with us. To activate your account \nuse activation code:\n$unique_id\n\nThis message was sent to $email at your request."; 
    $subject = "$unique_id is your account activation code."; 
    mail($to,$subject,$msg,'$from:'.$our_mail); 
} 

if($result){ 
    header('Location:activate.php'); //redirect 
    sendCode(); 
} 

同時,這是工作的罰款:

if($result){ 
    header('Location:activate.php'); //redirect 
    $our_mail = '[email protected]' 
    $to = $email; 
    $msg = "Hi $first_name, \n\nYour email has been used to sign up with us. To activate your account \nuse activation code:\n$unique_id\n\nThis message was sent to $email at your request."; 
    $subject = "$unique_id is your account activation code."; 
    mail($to,$subject,$msg,'$from:'.$our_mail); 
} 

任何人都可以解釋,爲什麼?

+0

你的代碼應該觸發注意的幾個問題。我建議您在開發框中啓用完整的錯誤報告。這樣PHP本身會告訴你有關缺失的變量。幾乎每次我給出這樣的建議時,我知道我會被忽略,但這仍然是一個很好的建議。 –

+0

謝謝隊友。我已經解決了這個問題,但它可以是有用的。謝謝。 –

回答

1

在一個函數變量範圍內將是本地的,所以你需要聲明變量爲全局變量或傳遞參數。也不要rediretion後發送郵件

試試這個:

function sendCode($email){ 
$our_mail='[email protected]' 
$to=$email; 
$msg= "Hi $first_name, \n\nYour email has been used to sign up with us. To activate your account \nuse activation code:\n$unique_id\n\nThis message was sent to $email at your request."; 
$subject="$unique_id is your account activation code."; 
mail($to,$subject,$msg,'$from:'.$our_mail); 
} 

if($result){ 

sendCode($email); 
header('Location:activate.php'); //redirect 
} 

OR

function sendCode(){ 
    global $email,$unique_id,$first_name; //<----- make variables global 
    $our_mail='[email protected]' 
    $to=$email; 
    $msg= "Hi $first_name, \n\nYour email has been used to sign up with us. To activate your account \nuse activation code:\n$unique_id\n\nThis message was sent to $email at your request."; 
    $subject="$unique_id is your account activation code."; 
    mail($to,$subject,$msg,'$from:'.$our_mail); 
    } 

    if($result){ 

    sendCode(); 
    header('Location:activate.php'); //redirect 

    } 
+0

這沒有奏效。 $ email是一個全局變量。 –

+0

評論'header('Location:activate.php');'然後檢查 –

+0

兩者都不起作用。 –