Jump to content

Mail Function


jr.

Recommended Posts

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 protected]";

 

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']);

 

}

 

 

?>

Link to comment
https://forums.phpfreaks.com/topic/142923-mail-function/
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/142923-mail-function/#findComment-749362
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/142923-mail-function/#findComment-749415
Share on other sites

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 protected]","Password Recovery","Testing","From: [email protected]");

 

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.

Link to comment
https://forums.phpfreaks.com/topic/142923-mail-function/#findComment-749427
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/142923-mail-function/#findComment-749458
Share on other sites

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 [email protected] for the email instead of the [email protected]? Could that be the problem, does it have to go though the websites email?

Link to comment
https://forums.phpfreaks.com/topic/142923-mail-function/#findComment-750226
Share on other sites

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 protected]";

		$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)

Link to comment
https://forums.phpfreaks.com/topic/142923-mail-function/#findComment-750232
Share on other sites

I have already tried doing that if(mail... and it doesnt work. And I have been using $to = "[email protected]";

 

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?

Link to comment
https://forums.phpfreaks.com/topic/142923-mail-function/#findComment-750256
Share on other sites

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 = "[email protected]";     // 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 protected]";          // 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']);
            }
               
            ?> 

 

 

Link to comment
https://forums.phpfreaks.com/topic/142923-mail-function/#findComment-750518
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/142923-mail-function/#findComment-751036
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.