Page 1 of 1

PHP Mail() if requirement met

Posted: Thu Apr 29, 2010 7:42 pm
by jeffyyy
I would like to have a script that looks up a value in a database and if an entry is found, it sends a message to the email address on file.

Here is what I have so far

Code: Select all

<?php
require_once('../constants.php');
mysql_select_db ("text_tag");
?>
<?php
$sql="INSERT INTO messages (name, tag, message)
VALUES
('$_POST[name]','$_POST[tag]', '$_POST[message]')";

if (!mysql_query($sql))
  {
  die('Error: ' . mysql_error());
  }
  ?>
"Your message has been sent"
<?php
$term = $_POST['tag'];
$user = mysql_query("select * from users where tag like '$term'");

while ($row = mysql_fetch_array($user)){
?>
<?php
	}
?>

That works on its own and if I use an echo statement, it shows me the email address I want it to send to, but I keep getting errors. Where should I put the mail section in order to send it the email address on file for the user selected from the $user variable?

Re: PHP Mail() if requirement met

Posted: Fri Apr 30, 2010 2:25 am
by social_experiment
If you find the email address with mysql_fetch_array(), you should equate it to a variable if you want to you use it outside the while loop.

Code: Select all

<? $query  = mysql_query("SELECT email_address FROM user WHERE tag LIKE '".mysql_real_escape_string($term)."'");
while ($array = mysql_fetch_array($query)) {
 $mail_address = $array['email_address'];
}
//after the query is where you should place the mail
//define all the variables you want to use
$mail = mail($to, $subject, $message, $headers);
if ($mail) { echo 'Message sent'; }
else { echo 'Message not sent' } ?>

Re: PHP Mail() if requirement met

Posted: Fri Apr 30, 2010 7:04 pm
by jeffyyy
Ok, this is now what I have, the script runs with no errors but I never receive the email address. What do I do now?

Code: Select all

<? $query  =  mysql_query("SELECT email FROM users WHERE tag LIKE '".mysql_real_escape_string($term)."'");
while ($array = mysql_fetch_array($query)) {
 $mail_address = $array['email_address'];
}
//after the query is where you should place the mail
//define all the variables you want to use
$to = '.$mail_address';
$subject = 'Someone has sent a message to your tag';
$message = 'test';
$mail = mail($to, $subject, $message, $headers);
if ($mail) { echo 'Message sent'; }
else { echo 'Message not sent'; } ?>[/sytntax=php]

Re: PHP Mail() if requirement met

Posted: Sat May 01, 2010 4:37 am
by social_experiment

Code: Select all

$query = mysql_query("SELECT email FROM users WHERE tag LIKE '".mysql_real_escape_string($term)."'");
while ($array = mysql_fetch_array($query)) {
$mail_address = $array['email_address']; ?>
If you select the 'email' field, you must call '$array['email''] and equate it to '$mail_address'.

Re: PHP Mail() if requirement met

Posted: Sat May 01, 2010 8:10 am
by jeffyyy
OK, now this is what I have and I am getting the same result...what am I doing wrong???

Code: Select all

<? $query  =  mysql_query("SELECT email FROM users WHERE tag LIKE '".mysql_real_escape_string($term)."'");
while ($array = mysql_fetch_array($query)) {
 $mail_address = $array['email'];
}
//after the query is where you should place the mail
//define all the variables you want to use
$to = '.$mail_address';
$subject = 'Someone has sent a message to your tag';
$message = 'test';
$mail = mail($to, $subject, $message, $headers);
if ($mail) { echo 'Message sent'; }
else { echo 'Message not sent'; } ?>

Re: PHP Mail() if requirement met

Posted: Sat May 01, 2010 9:41 am
by social_experiment
Do you receive any errors from the script when you execute it? I copied your code an i got this error message:
"From:" header missing in scriptname on line 17
Try the following sample of code

Code: Select all

<?php $count_query = mysql_query("SELECT COUNT(id) FROM users WHERE tag LIKE '".mysql_real_escape_string($term)."'");
 $rows = mysql_fetch_array($count_query);
 
 if ($rows[0] != 0) {
 	$query = mysql_query("SELECT email FROM users WHERE tag LIKE '".mysql_real_escape_string($term)."'");
	while ($array = mysql_fetch_array($query)) {
		$mail_address = $array['email'];
	}
	$to = $mail_address;
	$subject = 'Someone has sent a message to your tag';
	$message = 'test';
	$headers = 'From: example@example.com';
	
	$mail = mail($to, $subject, $message, $headers);
	if ($mail) { echo 'Message sent'; }
	//edit for production
	else {
		echo $mail_address;
	}
 }
else {
 // Display alternate message informing visitor or whoever
 // that there was no email address matching the tag
} ?>
This code does the following :
1. Counts the rows matching your query. Im going on the assumption that you have one (unique) email address per tag.
2. If there is a match ($rows[0] == 1, again based on unique email premise) the query is performed where the email address is retrieved from the table and assigned to the variable '$mail_address'.
3. All the required variables for the 'mail()' function is assigned and then an attempt is made to send the email.
4. If it sends, good. If not it will echo the email address. This is just to see what email address is retrieved from the table, if any. If the script works, change it. Note i also added edit for production.
5. The final 'else' section you should use to indicate that no match for the tag was found, or you could remove it.

Hope this helps. If it still doesn't work please paste any errors you receive :)

Note - if your PRIMARY KEY is not 'id' please change the 'id' in the COUNT() function to the same value as your primary key.

Re: PHP Mail() if requirement met

Posted: Sat May 01, 2010 10:19 am
by jeffyyy
I am not getting any error messages, and I am also not getting the email sent, so I don't know what is going on. Here is the code for the entire page:

Code: Select all

<?php
require_once('../constants.php');
mysql_select_db ("text_tag");
?>
<?php
$sql="INSERT INTO messages (name, tag, message)
VALUES
('$_POST[name]','$_POST[tag]', '$_POST[message]')";

if (!mysql_query($sql))
  {
  die('Error: ' . mysql_error());
  }
  ?>
  <head><link rel="stylesheet" href="style.css" type="text/css" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
	<script type="text/javascript" src="/fancybox/jquery.fancybox-1.3.1.pack.js"></script>
    <script type="text/javascript" src="/fancybox/jquery.easing-1.3.pack.js"></script>
    <script type="text/javascript" src="/fancybox/jquery.mousewheel-3.0.2.pack.js"></script>
<script type="text/javascript">
	$(document).ready(function() {

	/* This is basic - uses default settings */
	
	$("a#single_image").fancybox();
	
	/* Using custom settings */
	
	$("a#inline").fancybox({
		'hideOnContentClick': false
	});

	/* Apply fancybox to multiple items */
	
	$("a.group").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	false
	});
	
});
</script>
    <link rel="stylesheet" href="/fancybox/jquery.fancybox-1.3.1.css" type="text/css" media="screen" />
</head>
<body>
<div id="header">
    	<img style="display:block;margin:auto;" src="img/logo.png" width="300" height="75" />
        <div id="login">Login | Register</div>
    	<div id="nav">
        <ul>
        <li class="active"><a href="index.php">Home</a></li>
        <li><a href="about.html">About</a></li>
        <li><a href="support.html">Support</a></li>
        <li><a href="mailto:feedback@tagtexter.com">Contact</a></li>
        </ul>
    	</div>
	</div>
    <div style="height:200px;"></div>
<div align="center" id="content">
Your message has been sent. Please <a href="index.php">click here</a> to return to our homepage.
</div>
<?php $count_query = mysql_query("SELECT COUNT(username) FROM users WHERE tag LIKE '".mysql_real_escape_string($term)."'");
 $rows = mysql_fetch_array($count_query);
 
 if ($rows[0] != 0) {
        $query = mysql_query("SELECT email FROM users WHERE tag LIKE '".mysql_real_escape_string($term)."'");
        while ($array = mysql_fetch_array($query)) {
                $mail_address = $array['email'];
        }
        $to = $mail_address;
        $subject = 'Someone has sent a message to your tag';
        $message = 'test';
        $headers = 'From: example@example.com';
       
        $mail = mail($to, $subject, $message, $headers);
        if ($mail) { echo 'Message sent'; }
        //edit for production
        else {
                echo $mail_address;
        }
 }
else {
 // Display alternate message informing visitor or whoever
 // that there was no email address matching the tag
} ?>
I have put

Code: Select all

<?php ini_set('display_errors', 1); ?> 
at the top of the page to hopefully show any errors, but I am getting nothing.

And thanks for your help, i'm sure i've done something dumb and the answer is staring me in the face!

Re: PHP Mail() if requirement met

Posted: Sat May 01, 2010 10:23 am
by jeffyyy
Wow, like I said, I was doing something dumb! After looking through it again, I found that I forgot to define the variable $term and that would not work. So I fixed it, and it works! Thanks for all your help!