sphinx Posted November 18, 2011 Share Posted November 18, 2011 Hello, What i'm trying to do is generate a random email like: [email protected] Why isn't this working: <?php function genRandomString() { $length = 10; $characters = ’0123456789abcdefghijklmnopqrstuvwxyz’; $string = ”; for ($p = 0; $p < $length; $p++) { $string .= $characters[mt_rand(0, strlen($characters))]; } } $to = "[email protected]"; $subject = "Important"; $message = "Cheap goods available, user"; $from = "[email protected]" $headers = "From:" . $from; mail($to,$subject,$message,$headers); echo "Mail Sent."; ?> Many thanks Link to comment https://forums.phpfreaks.com/topic/251383-problem/ Share on other sites More sharing options...
phily245 Posted November 18, 2011 Share Posted November 18, 2011 You need this: <?php $from = "[email protected]" ?> to be this: <?php $from = $string."@website.com"; ?> Link to comment https://forums.phpfreaks.com/topic/251383-problem/#findComment-1289352 Share on other sites More sharing options...
xyph Posted November 18, 2011 Share Posted November 18, 2011 http://php.net/manual/en/language.variables.scope.php Link to comment https://forums.phpfreaks.com/topic/251383-problem/#findComment-1289354 Share on other sites More sharing options...
sphinx Posted November 18, 2011 Author Share Posted November 18, 2011 ok thanks, I put it to this: $from = $string."@website.com"; Other info: mail("$email", "$subject", $message, "From:" . $from); $headers = "From:" . $from; When I tested it, the email 'from' field displays as: @website.com Any ideas? Thanks Link to comment https://forums.phpfreaks.com/topic/251383-problem/#findComment-1289369 Share on other sites More sharing options...
sphinx Posted November 18, 2011 Author Share Posted November 18, 2011 bump Link to comment https://forums.phpfreaks.com/topic/251383-problem/#findComment-1289416 Share on other sites More sharing options...
PFMaBiSmAd Posted November 18, 2011 Share Posted November 18, 2011 A) You are not even calling your function. B) As xyph posted, you have a variable scope problem. The $string variable you set inside your function does not exist outside the function. You need to return the result that your function produces. C) What phily245 posted are two different syntaxes that produce the exact same result and don't have anything to do with your problem. D) You should have php's error reporting set to E_ALL and display_errors set to ON so that php will help you by displaying all the errors it detects. You will save a TON of time. You would be getting an undefined error message at the $string variable in your main code, alerting you to the fact that it has not been set (see items A and B in this list.) Link to comment https://forums.phpfreaks.com/topic/251383-problem/#findComment-1289418 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.