Jump to content

[SOLVED] simple, yet complicated


DeathStar

Recommended Posts

Hi there.

I made a register page that send the user their password but it seems that the email isnt sent!

Fixed all the errors and still not..

 

 

here is my code:

<?php
session_start();

print "<html>
<head>
<title> Register</title>
</head>
<body bgcolor=black><br />";
require "connect.php";

//code by http://www.phpit.net/category/code/string-regular-expressions/
function valid_email ($email)
{
  // First, we check that there's one @ symbol, and that the lengths are right
  if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email))
   {
    // Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
    return false;
   }

  // Split it into sections to make life easier
  $email_array = explode("@", $email);
  $local_array = explode(".", $email_array[0]);

  for ($i = 0; $i < sizeof($local_array); $i++)
   {
    if (!ereg(
                 "^(([A-Za-z0-9!#$%&#038;'*+/=?^_`{|}~-][A-Za-z0-9!#$%&#038;'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$",
                 $local_array[$i]))
     {
      return false;
     }
   }

  if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1]))
   { // Check if domain is IP. If not, it should be valid domain name
    $domain_array = explode(".", $email_array[1]);

    if (sizeof($domain_array) < 2)
     {
      return false; // Not enough parts to domain
     }

    for ($i = 0; $i < sizeof($domain_array); $i++)
     {
      if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i]))
       {
        return false;
       }
     }
   }

  return true;
}

session_start();
print <<<EOF
<head>
<!-- Begin Main Content -->
EOF;
$IP = ($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];

if (file_exists('ipbans/' . $IP))
{
  die("<b><font color=red size=+1>Your IP has been banned, there is no way around this.</font></b></body></html>");
}

if ($_POST['username'])
{
  if (!valid_email($_POST['email']))
   {
    die("<font color=#66ff00>Sorry, the email is invalid.</font><br />
><a href='register.php'>Back</a>");
   }

  if (strlen($_POST['username']) < 4)
   {
    die("<font color=#66ff00>Sorry, the username you entered is too short.</font></font><br />
><a href='register.php'>Back</a>");
   }

  $q2       = mysql_query("SELECT * FROM users WHERE email='{$_POST['email']}'");
if (mysql_num_rows($q2))
   {
    print "<font color=#66ff00>E-Mail already in use. Choose another.</font><br />
><a href='register.php'>Back</a>";
   }

  else
   {
    $IP           = ($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
     
     /* We ensure that somebody has not signed up from this IP in the last 2 hrs = 2 * 60 * 60 = 7200 secs */
     
     $chk_ip_dup = mysql_query("SELECT * FROM users WHERE ip_signup='{$IP}'");
     if (mysql_num_rows($chk_ip_dup))
     {
          die("<b><font color=red size=+1>Naugthy, naughty! Moderator advised!</font></b></body></html>");
     }
    // define possible characters
    $possible = "0123456789bcdfghjkmnpqrstvwxyz"; 
    
    // set up a counter
    $i = 0; 
    
    // add random characters to $password until $length is reached
    while ($i <  { 

      // pick a random character from the possible ones
      $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
        
      // we don't want this character if it's already in the password
      if (!strstr($userpass, $char)) { 
        $password .= $char;
        $i++;
      }

    }
$md5password = md5($password);
$$usernameconv = $_POST['username'];

    mysql_query("INSERT INTO `users` (`name` , `password` , `email` , `ip_login` , `ip_signup` , `verified` , `verified_code` )
VALUES ('$username', '$md5password', '$email', '127.0.0.1', '$IP', '0', '$md5password')");

    
    $to = $_POST['email']; 
    $headers = "From: norelpy@noreply.com";
    $subject = "Your new MOGSpam registration";
    $email = $_REQUEST['email'] ;
    
    $message = "Welcome to MOGSpam!\n\nYour account has been created as follows:\n";
    $message = $message . " Login: ".$usernameconv ;
    $message = $message . " Password: ".$password ;
    $message = $message . "\n\nIf you did not create this account, please ignore this email. No further action is required"; 
    $message = $message . "\nThis account was registered from IP address ".$IP ;
   
    $sent = mail($to, $subject, $message, $headers) ;
    if($sent)
        {
    	print "<b>Your Signup was processed sucessfully!!<b><br>";
        print "Your password has been sent to ".$_POST['email']."<b><br>";
        print "<a href='login.php'>Login</a>"; 
        }
    else
    	{
    	print "We encountered an error sending your registration email"; 
    	}
   }
}

else
{
print "<form action=register.php method=post>
<center><table width='75%' class='table' cellspacing='1'>

<tr>
<td width='30%'><font color=#66ff00>Username</font></td>
<td width='40%'><input type=text name=username></td>
<td width='30%'></td>
</tr>

<tr>
<td><font color=red>Email</font></td><td><input type=text name=email></td>
<td></td>
</tr>
<tr>
<td colspan=3><center><input type=submit value=Submit>
</form><br /></td></tr></table></center>
<a href='login.php'><font color=red><b>Go Back</b></a></center>";
}

print <<<OUT

</td>
<td class="rgrad"></td>
</tr>
<tr>
<td colspan="3">
<table cellpadding="0" cellspacing="0" border="0" width="100%">
<tr>
<td class="dgradl"> </td>
<td class="dgrad"> </td>
<td class="dgradr"> </td>
</tr>
</table>
</td>
</tr>
</table>
</center>
</body>
</html>
OUT;
?>

Link to comment
Share on other sites

Try commenting parts of it out to narrow down where the problem may be occurring. Is it messing up in the email validation function? The php mail function? I looked over your code and nothing stuck out to me as incorrect syntax or anything. Try to narrow it down and it should make it easier to find out what's wrong.

Link to comment
Share on other sites

This si the mail() function:

   $to = $_POST['email']; 
    $headers = "From: norelpy@noreply.com";
    $subject = "Your new MOGSpam registration";
    $email = $_REQUEST['email'] ;
    
    $message = "Welcome to MOGSpam!\n\nYour account has been created as follows:\n";
    $message = $message . " Login: ".$usernameconv ;
    $message = $message . " Password: ".$password ;
    $message = $message . "\n\nIf you did not create this account, please ignore this email. No further action is required"; 
    $message = $message . "\nThis account was registered from IP address ".$IP ;
   
    $sent = mail($to, $subject, $message, $headers) ;
    if($sent)
        {
    	print "<b>Your Signup was processed sucessfully!!<b><br>";
        print "Your password has been sent to ".$_POST['email']."<b><br>";
        print "<a href='login.php'>Login</a>"; 
        }
    else
    	{
    	print "We encountered an error sending your registration email"; 
    	}
   }
}

 

cant understand whats worng..

Cant momment anything out or else the script will fail.

Link to comment
Share on other sites

From php.net:

 

<?php
$to      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
   'Reply-To: webmaster@example.com' . "\r\n" .
   'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
?> 

 

Your code looks fine to me, as long as $_POST['email'] is a valid email address. Try substituting that part for a string with an actual email address and see if the code works. Also, what is the purpose of this line:

 

$email = $_REQUEST['email'] ;

 

? From what I can tell, you never use the $email variable after making that declaration.

Link to comment
Share on other sites

I have noticed that some internet ISP's are killing my mail() function mail as well.  Something to do with Spam filters that check header info maybe?  I know that some of my mail will go to certain accounts and not to others.  All I can think of is anti-spam filters.  Anyone else having this behavior?

Link to comment
Share on other sites

More than likely it is the spam filters causing your problem. The mail function seems to send, you get a success story, but the email never arrives in the inbox.

 

I have this problem all of the time. Some times more extensive headers allow it to be passed through, sometimes simple headers. It is a real problem, but your web host should be able to check the logs to see what emails were sent or blocked.

Link to comment
Share on other sites

As far as msn goes for blocking emails, you need to use headers to avoid it. The headers I use are:

 

$html = false;
$headers  = "MIME-Version: 1.0" . "\r\n";
if($html)
{
$headers .= "Content-type: text/HTML; charset=ISO-8859-1" . "\r\n";
} else {
$headers .= "Content-Type: text/plain; charset=ISO-8859-1" . "\r\n";
}
$headers .= "From: \"$from_name\" <" . $from . ">" . "\r\n";
$headers .= "Reply-To: $from" . "\r\n";

 

$html is a true/false value. If you're sending html messages, define $html as true.

$from_name is the name shown instead of the email. (ex. John Doe)

$from is the email address which it was sent from. (ex. john@doe.com)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.