Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

SChaput

macrumors regular
Original poster
Jul 2, 2008
153
0
United States
I have just recently discovered the wonderful world of functions to make my life easier. I am trying to create a function that sends an email to certain people from a database.
Code:
function sendEmail($email,$random)
{
//mysql query here
 


while($row = mysql_fetch_array($result))
{
    $id = $row['id'];
    $random = $row['random'];
	$email = $row['email'];



// subject
$subject = 

// message
$message = "";


mail($email, $subject, $message);
}


}

I call the function in my website like this.
Code:
  sendEmail($email,$random);
I feel the error lies in where i call the function, it just doesn't look right to me.

Any help is appreciated. Thanks!
 
You aren't assigning anything to $subject, you just have an equal sign with nothing after it. That creates syntax issues. Make sure to view the PHP error logs, which should reveal these problems. Also not sure why you're sending the $email and $random variables to the function, when you just recreate them inside the function.
 
I think he's using $email and $random to perform some kind of SQL query to return a specific user using the email and random field supplied.

I agree though, you don't end $subject or even assign it a value.

If you could provide the actual error message that is being displayed it may be easier to tell if there is a problem somewhere else.
 
I have just recently discovered the wonderful world of functions to make my life easier. I am trying to create a function that sends an email to certain people from a database.

<snip code>

I feel the error lies in where i call the function, it just doesn't look right to me.

Any help is appreciated. Thanks!

You didn't ask a question!
 
PHP:
function sendEmail($email,$random)
{

//mysql query here
// I wish you would of posted this but w/e. Just make sure to add or die(mysql_error); at the end....

do {
$id = $row['id'];
$random = $row['random'];
$email = $row['email'];

// subject
$subject = ""; // I have no clue what you have here...

// message
$message = ""; // And again, no clue.

mail($email, $subject, $message);

} while($row = mysql_fetch_array($result));
}
return TRUE;
}

Tried revising. I think you just missed a semicolon on your while statement. Plus you implemented a do while loop wrong.
 
Sorry for the delay in response. I got the code working, it was an error before calling the function. For those interested here is the final, <b>working</b> code.
PHP:
function sendEmail($email,$random)
{

 //Database connection
	
 $query  = "SELECT * FROM hmr WHERE email ='$email' AND random = '$random' and verified=0 LIMIT 1 ";
$result = mysql_query($query);


while($row = mysql_fetch_array($result))
{
    $id = $row['id'];
    $random = $row['random'];
	$email = $row['emaul'];



// subject
$subject = 'Verification Needed @ I Hate My Roommate';

// message
$message = "This is an automated message from IHateMyRoommate.net.

According to our records someone has used this email address, $email, and added a post at our website.  If this was not you, please delete this email.

However, if you posted the story click the following link to verify your posting on our site, letting us know you are not spam.

Thank you.  We appreciate it.

http://www.ihatemyroommate.net/verify.php?id=$id&random=$random

- The I Hate My Roommate Team.
";

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";


$headers = "From: verification@ihatemyroommate.net \r\n" . 
           "Reply-To: no-reply@ihatemyroommate.net \r\n" . 
           "X-Mailer: PHP/" . phpversion(); 

mail($email, $subject, $message, $headers);
}

}

This function is called using, sendEmail($email, $random);

Thanks.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.