PHP Mail() if requirement met

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jeffyyy
Forum Newbie
Posts: 17
Joined: Sat Apr 24, 2010 9:06 pm

PHP Mail() if requirement met

Post 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?
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP Mail() if requirement met

Post 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' } ?>
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
jeffyyy
Forum Newbie
Posts: 17
Joined: Sat Apr 24, 2010 9:06 pm

Re: PHP Mail() if requirement met

Post 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]
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP Mail() if requirement met

Post 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'.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
jeffyyy
Forum Newbie
Posts: 17
Joined: Sat Apr 24, 2010 9:06 pm

Re: PHP Mail() if requirement met

Post 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'; } ?>
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: PHP Mail() if requirement met

Post 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.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
jeffyyy
Forum Newbie
Posts: 17
Joined: Sat Apr 24, 2010 9:06 pm

Re: PHP Mail() if requirement met

Post 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!
jeffyyy
Forum Newbie
Posts: 17
Joined: Sat Apr 24, 2010 9:06 pm

Re: PHP Mail() if requirement met

Post 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!
Post Reply