mySQL database php form

Hello,

I am trying to set up an email newsletter for my website. When the user enters their email address it gets saved in my database. Then the user will receive an email to confirm the subscription.

The code I have here is mostly from a tutorial, so it is clean and easy to read. I have made it so it does connect and save data to my mySQL database. I just can not figure out how to get the PHP code to automatically send the user an email confirmation of their subscription to my site.

Please help! I will reward! and btw I have put *** as my password so not to be hacked…soo that is not why this is not working :wink:

Thank You!!

Here is my index.php…

[php]

include_once ‘inc/php/config.php’;
include_once ‘inc/php/functions.php’;

//setup some variables/arrays
$action = array();
$action[‘result’] = null;

$text = array();

//check if the form has been submitted
if(isset($_POST[‘signup’])){

//cleanup the variables
//prevent mysql injection
$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);
$email = mysql_real_escape_string($_POST['email']);

//quick/simple validation
if(empty($username)){ $action['result'] = 'error'; array_push($text,'You forgot your username'); }
if(empty($password)){ $action['result'] = 'error'; array_push($text,'You forgot your password'); }
if(empty($email)){ $action['result'] = 'error'; array_push($text,'You forgot your email'); }

if($action['result'] != 'error'){
			
	$password = md5($password);	
		
	//add to the database
	$add = mysql_query("INSERT INTO `users` VALUES(NULL,'$username','$password','$email',0)");
	
	if($add){
		
		//get the new user id
		$userid = mysql_insert_id();
		
		//create a random key
		$key = $username . $email . date('mY');
		$key = md5($key);
		
		//add confirm row
		$confirm = mysql_query("INSERT INTO `confirm` VALUES(NULL,'$userid','$key','$email')");	
		
		if($confirm){
		
			//include the swift class
			include_once 'http://ryanafonk.com/imag-design/source/inc/php/swift/swift_required.php';
		
			//put info into an array to send to the function
			$info = array(
				'username' => $username,
				'email' => $email,
				'key' => $key);
		
			//send the email
			if(send_email($info)){
							
				//email sent
				$action['result'] = 'success';
				array_push($text,'Thanks for signing up. Please check your email for confirmation!');
			
			}else{
				
				$action['result'] = 'error';
				array_push($text,'Could not send confirm email');
			
			}
		
		}else{
			
			$action['result'] = 'error';
			array_push($text,'Confirm row was not added to the database. Reason: ' . mysql_error());
			
		}
		
	}else{
	
		$action['result'] = 'error';
		array_push($text,'User could not be added to the database. Reason: ' . mysql_error());
	
	}

}

$action['text'] = $text;

}

?>

<?php include 'inc/elements/header.php'; ?> <?= show_errors($action); ?>
<fieldset>

	<ul>
		<li>
			<label for="username">Username:</label>
			<input type="text" name="username" />
		</li>
		<li>
			<label for="password">Password:</label>
			<input type="password" name="password" />
		</li>
		<li>
			<label for="email">Email:</label>
			<input type="text" name="email" />	
		</li>
		<li>
			<input type="submit" value="Signup Now" class="large blue button" name="signup" />			
		</li>
	</ul>
	
</fieldset>

[/php]

First of all sorry about my english,

You are using external server to send mail, check if this web are available and show this documentation.
include_once ‘http://ryanafonk.com/imag-design/source/inc/php/swift/swift_required.php’;

If you has some mail server, I recomended you to use “mail()”, native php function.
Otherwise i recommended to install someone, if your SO are unix you can install sendmail at repositories.

Sponsor our Newsletter | Privacy Policy | Terms of Service