jr. Posted January 29, 2009 Share Posted January 29, 2009 Alright, I have been working on making a forum for my website. I have a lot of it made so far but I am stuck on this mail function. I have never used a mail function before. I am using gofreeserve.com free hosting until I get my site fully coded. Anyways, my website doesn't have any errors when I run this code. I just don't get an email. I was wondering if anyone could help me out? BTW this code is for my password recovery section. getpass.php Code: <?php function getpass($email){ $sql = "SELECT * FROM `users` WHERE `email`='".$email."'"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) == 0){ return "Unknown email address!"; }else { $row = mysql_fetch_assoc($res); if($email == $row['email']){ $rand = rand(100000, 999999); $row['password'] = $rand; $to = $email; $subject = "Password Recovery"; $message = "Testing"; $headers = "From: email@gmail.com"; mail($to,$subject,$message,$headers); $sql2 = "UPDATE `users` SET `password`='".md5($row['password'])."' WHERE `email`='".$email."'"; $res2 = mysql_query($sql2) or die(mysql_error()); if(mail){ echo "Sent!"; }else { echo "Not sent!"; } }else { echo "Unknown email address!"; } } } if(!$_POST['submit']){ echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\" width=\"30%\">\n"; echo "<form method=\"post\" action=\"getpass.php\">\n"; echo "<tr><td width=\"50%\">Registered e-mail </td><td><input type=\"text\" name=\"email\"></td></tr>\n"; echo "<tr><td colspan=\"2\" align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Get Password\"></td></td></tr>\n"; echo "</form></table>"; }else { echo getpass($_POST['email']); } ?> Quote Link to comment Share on other sites More sharing options...
ratcateme Posted January 29, 2009 Share Posted January 29, 2009 code looks alright but i know lost of free hosts block the mail function to stop spammers using there mail servers to send mail so check with yours to see if free accounts can send mail Scott. Quote Link to comment Share on other sites More sharing options...
Errant_Shadow Posted January 29, 2009 Share Posted January 29, 2009 Hmm... instead of: mail($to,$subject,$message,$headers); if(mail){ echo "Sent!"; }else { echo "Not sent!"; } try putting the mail() function in the if() condition, like this: if(mail($to,$subject,$message,$headers)){ echo "Sent!"; } else { echo "Not sent!"; } Not sure it will fix the problem, but it might ratcateme raises a good point. Did you try sending the message to a different e-mail service? Quote Link to comment Share on other sites More sharing options...
jr. Posted January 29, 2009 Author Share Posted January 29, 2009 Still not working, thanks for the suggestion though errant. and ratcateme, it says that gofreeserve allows the mail function, but not smtp email function. but i tried just a basic mail function script but that didn't work either. hmm maby the host is just weird. Quote Link to comment Share on other sites More sharing options...
cwarn23 Posted January 29, 2009 Share Posted January 29, 2009 Still not working, thanks for the suggestion though errant. The only other 2 things that I have experienced in the past are an invalid email address and incorrect php.ini configurations. Both of which you should be able to change with php code. So first is first, I see in your code a copy of the email is saved in the database. Varify that the mysql password update process took place during the loading of the page. If it did then you can eliminate to possibility of it being the email address which leaves one other option in my experience of the mail function. And that is to alter the ini directives that tell php what server processes the mail. You will want an external server such as gmail to process the mail for you since it seems your web host may not support email or haven't got php configured to their email client. Below is the code used to configure the email server and in the code they are set to their defaults: ini_set('SMTP','localhost'); ini_set('smtp_port','25'); ini_set('sendmail_path','/usr/sbin/sendmail -t -i'); //this line may or may not work Below is what you can try setting them to but am unsure weather it will work or not. ini_set('SMTP','smpt.gmail.com'); ini_set('smtp_port','465'); Other than that, you may want to submit a support ticket to your web host. Quote Link to comment Share on other sites More sharing options...
Errant_Shadow Posted January 29, 2009 Share Posted January 29, 2009 Another thing you may try that won't fix it, but might narrow down what exactly your problem is, is to send a mail() function with static values instead of variables. mail("email@gmail.com","Password Recovery","Testing","From: email@gmail.com"); It might not seem likely, but if that works with static values, then at least you know the mail service is working and that the problem is probably in your database results. Otherwise, it really is your mail service. Quote Link to comment Share on other sites More sharing options...
kittrellbj Posted January 29, 2009 Share Posted January 29, 2009 I apologize if this is considered advertising, but I am not affiliated with them in any way. I am doing the same thing that the OP is doing (coding my site on a free one until it's ready). http://www.000webhost.com has a working mail() function (it is working fine in my application), so if you have a few free minutes, you could try singing up with them and testing it out there to see if it's the provider or something in the code. Quote Link to comment Share on other sites More sharing options...
jr. Posted January 30, 2009 Author Share Posted January 30, 2009 Thats a good idea kittrellbj i might try that later. And errant i did try that also and it wasnt working then either. Also gofreeserve hosting does have its own email. I made the email but do I just put myemail@mywebsite.com for the email instead of the email@gmail.com? Could that be the problem, does it have to go though the websites email? Quote Link to comment Share on other sites More sharing options...
gevans Posted January 30, 2009 Share Posted January 30, 2009 Hey, just a quick look; <?php function getpass($email){ $sql = "SELECT * FROM `users` WHERE `email`='".$email."'"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) == 0){ return "Unknown email address!"; }else { $row = mysql_fetch_assoc($res); if($email == $row['email']){ $rand = rand(100000, 999999); $row['password'] = $rand; $to = $email; $subject = "Password Recovery"; $message = "Testing"; $headers = "From: email@gmail.com"; $sql2 = "UPDATE `users` SET `password`='".md5($row['password'])."' WHERE `email`='".$email."'"; $res2 = mysql_query($sql2) or die(mysql_error()); if(mail($to,$subject,$message,$headers)){ echo "Sent!"; }else { echo "Not sent!"; } }else { echo "Unknown email address!"; } } } if(!$_POST['submit']){ echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\" width=\"30%\">\n"; echo "<form method=\"post\" action=\"getpass.php\">\n"; echo "<tr><td width=\"50%\">Registered e-mail </td><td><input type=\"text\" name=\"email\"></td></tr>\n"; echo "<tr><td colspan=\"2\" align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Get Password\"></td></td></tr>\n"; echo "</form></table>"; }else { echo getpass($_POST['email']); } ?> Use the above for you code. And this may sound like a dumb question, but are you entering and email address correctly? Is it your email address? (those sort of things) Quote Link to comment Share on other sites More sharing options...
jr. Posted January 30, 2009 Author Share Posted January 30, 2009 I have already tried doing that if(mail... and it doesnt work. And I have been using $to = "myemail@yahoo.com"; But even if it was the wrong email it would say unknown email address. I will just try a new website. I was wondering, if you put someone elses email in the headers, would it look like they send it or does it say who its from up at the top? Quote Link to comment Share on other sites More sharing options...
gevans Posted January 30, 2009 Share Posted January 30, 2009 When you say $to = "myemail@yahoo.com"; do you mean that literally, or is it the email address that you use @yahoo.com Quote Link to comment Share on other sites More sharing options...
jr. Posted January 30, 2009 Author Share Posted January 30, 2009 haha i am not stupid its the email i use email @yahoo.com. Quote Link to comment Share on other sites More sharing options...
gevans Posted January 30, 2009 Share Posted January 30, 2009 Sorry had to ask, Just for everyones information, Google has already crawled this page. I was trying to look at the server you were using and typed in 'how to send email with gofreeserve' and this very page came up seventh, I'm impressed! Quote Link to comment Share on other sites More sharing options...
work_it_work Posted January 30, 2009 Share Posted January 30, 2009 it happen to me with hosting providers, mail() function was specified in the plan, but couldn't manage to send emails... so i put my own phpmailer on server, just upload it on your account. here is an example converted to your demands, it might work for you <?php function getpass($email){ $sql = "SELECT * FROM `users` WHERE `email`='".$email."'"; $res = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($res) == 0){ return "Unknown email address!"; }else { $row = mysql_fetch_assoc($res); if($email == $row['email']){ $rand = rand(100000, 999999); $row['password'] = $rand; $sql2 = "UPDATE `users` SET `password`='".md5($row['password'])."' WHERE `email`='".$email."'"; $res2 = mysql_query($sql2) or die(mysql_error()); if($res2){ include('phpmailer/class.phpmailer.php'); // change this to point to the phpmailer class $mail = new PHPMailer(); $mail->IsSMTP(); // set mailer to use SMTP $mail->Host = "smtp.server.domain"; // specify main and backup server //$mail->SMTPAuth = true; // turn on SMTP authentication (uncomment if necessary) $mail->Username = "user@host.domain"; // SMTP username (remember they provide you an email which has smtp access) $mail->Password = "your password"; // SMTP password //$mail->SMTPDebug = 2; // uncomment if you need $message = "Testing\n"; //messafe body here $message .= "Testing lost pass\n"; //messafe body here $mail->From = "email@gmail.com"; // change it to from email you wnat $mail->FromName = "LostPass"; // change it to from name you wnat $mail->Subject = "Password Recovery"; // change it to your subject $mail->IsHTML(true); $mail->AddAddress = $email; $mail->Body = $message; $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; if(!$mail->Send()){ echo "<br />Confirmation e-mail cannot be sent. Error: ".$mail->ErrorInfo; } else { echo "A confirmation e-mail has been sent to {$_POST['email']}"; } } else { echo "You application cannot be completed"; } } } } if(!$_POST['submit']){ echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\" width=\"30%\">\n"; echo "<form method=\"post\" action=\"getpass.php\">\n"; echo "<tr><td width=\"50%\">Registered e-mail </td><td><input type=\"text\" name=\"email\"></td></tr>\n"; echo "<tr><td colspan=\"2\" align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Get Password\"></td></td></tr>\n"; echo "</form></table>"; }else { echo getpass($_POST['email']); } ?> Quote Link to comment Share on other sites More sharing options...
jr. Posted January 31, 2009 Author Share Posted January 31, 2009 hmm I have seen phpmailer before, I tried it but i keep getting an error: Warning: fsockopen() [function.fsockopen]: unable to connect to smtp.gofreeserve.com:25 (Connection timed out) in /home/vol3/gofreeserve.com/gofre_2829949/quadlab.co.cc/htdocs/phpmailer/class.smtp.php on line 122 Confirmation e-mail cannot be sent. Error: SMTP Error: Could not connect to SMTP host. Should the $mail->Host = "smtp.gofreeserve.com"; look like that? Quote Link to comment Share on other sites More sharing options...
work_it_work Posted January 31, 2009 Share Posted January 31, 2009 oops, it's turned off... the smtp is of... i guess this is the problem why your form doesn't work either way... (my opinion) get another host Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.