Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6

Thread: PHP Mail Issue

  1. #1
    New Coder
    Join Date
    May 2010
    Posts
    22
    Thanks
    9
    Thanked 0 Times in 0 Posts

    PHP Mail Issue

    Having an issue with PHP mail at the moment
    I'm trying to get a mail to send for registration email verification purposes.
    Here is a code that works when in an isolated file e.g. email.php :

    Code:
    <?php
    
    $hash = md5( rand(0,1000) );
    $hash = mysql_escape_string($hash);
    $rusername = "hello";
    $correctpassword = "password";
    $correctemail = "test@googlemail.co.uk"; <-- (Changed for security reasons, but works either way)  
    
    $to = $correctemail; 
    $subject = "Verification Email";    
    $message = "Thanks for signing up!
    http://website.co.uk/registration/verify.php?email='.$correctemail.'&hash='.$hash.'"; 
    
    $from = "admin@test.co.uk";                 
    $headers = "From:" . $from;
    mail($to, $subject, $message, $headers); // Send our email  	
    echo "mail sent";
     ?>
    The username and password variables are not predefined in the proper version they are taken from the form information which i check to ensure is not empty, furthermore the fields have validation to ensure they are correct i.e correct email format.

    When i put it in an if statement the email just doesn't send, i know that the query works because the information is inserted into the database but the email just doesn't send :S:

    Code:
    if (empty($rusername) or empty($correctpassword) or empty($correctemail) or empty($rcompany) or empty($rwebsite) or empty($rtown) or empty($rpcode) or empty($correctnumber))
    {
    	echo "<br />";
    	echo "Check Error Codes, Registration Failed";
    	echo "<input type='button' value='Edit Details' action='action' onclick='history.go(-1);' ></a>";
    }
    else
    {
    
    $sql="INSERT INTO members (Username, Password, Company_Name, Website, Email, Town, Post_Code, Telephone, hash)  
    VALUES ('$rusername', '$correctpassword', '$rcompany', '$rwebsite', '$correctemail', '$rtown', '$rpcode', '$correctnumber', '$hash')";
    
    $to = $correctemail; 
    $subject = "Verification Email";  
    $message = "Thanks for signing up! 
        http://test.co.uk/registration/verify.php?email='.$correctemail.'&hash='.$hash.'"; 
        $from = "admin@test.co.uk";                 
        $headers = "From:" . $from;
        mail($to, $subject, $message, $headers); // Send our email  	
    	echo "mail sent";
    headers); 
    
    $result=mysqli_query($con2, $sql);
    }
    Last edited by GStatus; 08-10-2013 at 07:45 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Post an accurate script. The one you have here has syntactical errors in it.
    Mail will return a boolean. If its true, than the sendmail has accepted it. If its false, it has not been accepted. Make use of the return result of the mail call to see if its queued. If it comes up true, the problem isn't your PHP, its the sendmail or receiving end. If its false, you can use the error_get_last() to try and determine the cause of the error.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    GStatus (08-13-2013)

  • #3
    New Coder
    Join Date
    May 2010
    Posts
    22
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Full script, used --- to block out password and database names for security reasons

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta name="keywords" content="">
    <meta name="description" content="">
    <title>Registration Form</title>
    <link rel="stylesheet" type="text/css" href="../reset-fonts-grids.css">
    <link rel="stylesheet" type="text/css" href="../base.css">
    </head>
    <body>
    <div id="container">
    	<div id="header">
    	
    	</div>
    	<div id="navigation">
    		<ul>
    			<li><a href="#">Home</a></li>
    			<li><a href="#">About</a></li>
    			<li><a href="#">Services</a></li>
    			<li><a href="#">Contact us</a></li>
    		</ul>
    	</div>
    	<div id="content-container">
    		<div id="content">
    	<h2>Registration</h2>
        
    <?php
    
    $host="---"; // Host name
    $username="---"; // Mysql username
    $password="---"; // Mysql password
    $db_name="---"; // Database name
    $tbl_name="--"; // Table name
    
    // Connect to server and select databse.
    $con = mysqli_connect("---", "---", "---") or die("cannot connect");
    mysqli_connect("---", "---", "---", "---") or die("cannot connect");
    mysqli_select_db($con, "datamysq")or die("cannot select DB");
    
    $con2 = mysqli_connect("---", "---", "---", "---") or die("cannot connect");
    
    
    // Registration details sent from form
    
    if (isset($_POST['Name']) && isset($_POST['Email']) && isset($_POST['Password']) && isset($_POST['Cpassword']) && isset($_POST['Cname']) && isset($_POST['Website']) && isset($_POST['Pcode']) && isset($_POST['Town']) && isset($_POST['Tnum']))
    {
    	
    $rusername= $_POST['Name'];
    
    $usercheck = mysqli_query($con2, "SELECT * FROM members WHERE username='$rusername'");
    
    if(mysqli_num_rows($usercheck) !=0)
    {
    echo "Error 1.5: Username already exists";
    }
    if (empty($rusername))
    		{ 
    		echo "Error 1: Please enter a username";
    		echo "<br />";
    		}
    		
    $remail= $_POST['Email'];
    
    if (empty($remail))
    		{ 
    		echo "Error 2: Please enter a password";
    		echo "<br />";
    		}
    		else
    			{
        if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $remail)){  
           echo "Error 2.5: The email you have entered is invalid, please try again.";  
        }
    	else
    	{
    		$correctemail= $remail;
    		}
    			}
    			
    	
    $rpassword= $_POST['Password'];
    $rcpassword= $_POST['Cpassword'];
    		if (empty($rpassword))
    		{ 
    		echo "Error 3: Please enter a password";
    		echo "<br />";
    		}
    		else
    			{
    			if ($rpassword == $rcpassword)
    				{
    					$salt="---";
    					$salt .=$rpassword;
    					$rpassword=$salt;
    					$correctpassword = md5($rpassword);
    				}
    				else
    				{ 
    						echo "Error 3.5: Passwords Do Not Match";
    						echo "<br />";
    				}
    			}
    			
    $rcompany= $_POST['Cname'];
    if (empty($rcompany))
    		{ 
    		echo "Error 4: Please enter a Company Name";
    		echo "<br />";
    		}
    				
    $rwebsite= $_POST['Website'];
    if (empty($rwebsite))
    		{ 
    		echo "Error 5: Please enter a Website";
    		echo "<br />";
    		}
    
    $rtown= $_POST['Town'];
    	if (empty($rtown))
    		{ 
    		echo "Error 6: Please enter a Town";
    		echo "<br />";
    		}
    $rpcode = $_POST['Pcode'];
    $rnumber= $_POST['Tnum'];
    	if (empty($rnumber))
    		{ 
    		echo "Error 7: Please enter a Telephone number";
    		echo "<br />";
    		}
    		else
    			{
    			if (strlen($rnumber) < 11)
    				{
    					echo "Error 7.5: The Telephone number is not 11 digits";
    					echo "<br />";
    				}
    				else
    				{
    					$correctnumber = $rnumber;
    				}
    			}
    		}
    		echo "<h3> Submitted Information </h3>";
    		echo "User Name: `$rusername`";
    		echo "<br />";
    		echo "Password: `$correctpassword`";
    		echo "<br />";
    		echo "Email: `$correctemail`";
    		echo "<br />";
    		echo "Company Name: `$rcompany`";
    		echo "<br />";
    		echo "Website URL: `$rwebsite`";
    		echo "<br />";
    		echo "Town: `$rtown`";
    		echo "<br />";
    		echo "Post Code: `$rpcode`";
    		echo "<br />";
    		echo "Telephone Number: `$correctnumber`";
    		
    			
    // To protect MySQL injection (more detail about MySQL injection)
    
    $rusername = stripslashes($rusername);
    $correctpassword = stripslashes($correctpassword);
    $correctemail = stripslashes($correctemail);
    $rcompany = stripslashes($rcompany);
    $rwebsite = stripslashes($rwebsite);
    $rtown = stripslashes($rtown);
    $rpcode = stripslashes($rpcode);
    $correctnumber = stripslashes($correctnumber);
    
    $rusername = mysqli_real_escape_string($con2, $rusername);
    $correctpassword = mysqli_real_escape_string($con2, $correctpassword);
    $correctemail = mysqli_real_escape_string($con2, $correctemail);
    $rcompany = mysqli_real_escape_string($con2, $rcompany);
    $rwebsite = mysqli_real_escape_string($con2, $rwebsite);
    $rtown = mysqli_real_escape_string($con2, $rtown);
    $rpcode = mysqli_real_escape_string($con2, $rpcode);
    $correctnumber = mysqli_real_escape_string($con2, $correctnumber);
    $hash = md5( rand(0,1000) );
    $hash = mysql_escape_string($hash);
    
    if (empty($rusername) or empty($correctpassword) or empty($correctemail) or empty($rcompany) or empty($rwebsite) or empty($rtown) or empty($rpcode) or empty($correctnumber))
    {
    	echo "<br />";
    	echo "Check Error Codes, Registration Failed";
    	echo "<input type='button' value='Edit Details' action='action' onclick='history.go(-1);' ></a>";
    }
    else
    {
    
    $sql="INSERT INTO members (Username, Password, Company_Name, Website, Email, Town, Post_Code, Telephone, hash)  
    VALUES ('$rusername', '$correctpassword', '$rcompany', '$rwebsite', '$correctemail', '$rtown', '$rpcode', '$correctnumber', '$hash')";
    
    $to = $correctemail; 
    $subject = Verification Email";   
    $message = "Thanks for signing up! 
        http://test.co.uk/registration/verify.php?email='.$correctemail.'&hash='.$hash.'"; // Our message above including the link  
        $from = "admin@test.co.uk";                 
        $headers = "From:" . $from;
        mail($to, $subject, $message, $headers); // Send our email  	
    	echo "mail sent";
    
    $result=mysqli_query($con2, $sql);
    }
    if($result){
    echo "<br />";
    echo "Registration Successful";
    echo "<br />";
    echo "Your account has been made, <br /> please verify it by clicking the activation link that has been send to your email.";  
    echo "<br />";
    echo "<br />";
    echo "<a href='../login/main_login.php'><input type='button' value='Click Here to Login'/></a>";
    echo "<a href='../account/myaccount.php'><input type='button' value='Click Here to access your account'/></a>";
    echo "<a href='../login/logout.php'><input type='button' value='Click Here to Log Out'/></a>";
    }
    
    
    ?>
    
    
    </div>
    		<div id="aside">
    			<h3>
    				Advertising Space
    			</h3>
    			<p>
    				Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan.
    			</p>
    		</div>
    		<div id="footer">
    			Copyright © Site name, 20XX
    		</div>
    	</div>
    </div>
    </body>
    </html>

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    This one's a bit better.
    PHP Code:
        mail($to$subject$message$headers); // Send our email      
        
    echo "mail sent"
    Start here. Block the mail() call into an if branch and go from there. If it turns out false, that's an indication that it has not queued. If it turns out successful, than it has successfully queued which means your problem is outside of the PHP code.

    We know for sure you have syntactical errors as well:
    PHP Code:
    $to $correctemail
    $subject Verification Email";   
    $message = "
    Thanks for signing up
        
    http://test.co.uk/registration/verify.php?email='.$correctemail.'&hash='.$hash.'"; // Our message above including the link 
    $subject is assigned a string, but it doesn't have a starting delimiter. You'll get a parser error here.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    GStatus (08-13-2013)

  • #5
    New Coder
    Join Date
    May 2010
    Posts
    22
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Thanks, fixed!

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by GStatus View Post
    Thanks, fixed!
    Awesome, what did the problem end up being?
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •