[英]Mail() Not Working inside Function
以下功能未執行:
function sendCode(){
$our_mail = 'username@gmail.com'
$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 = 'username@gmail.com'
$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);
}
誰能解釋為什么?
在函數中,變量作用域將是局部的,因此您需要將變量聲明為全局變量或使用參數傳遞。 發送郵件后也要重新分配
嘗試這個:
function sendCode($email){
$our_mail='username@gmail.com'
$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
}
要么
function sendCode(){
global $email,$unique_id,$first_name; //<----- make variables global
$our_mail='username@gmail.com'
$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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.