Jump to content

mail() in a while statement...


butchnutch

Recommended Posts

Hi everyone.

I am trying to set up a very very simple mailer that pulls emails from a mysql database.  This is what my code looks like:

[code]<?php

mysql_connect("localhost" , "id" , "password");
mysql_select_db("violetpa_test");
$query = "SELECT * FROM customers";
$results = mysql_query($query);

if ($results)
{
while ($customers = mysql_fetch_object($results))
{
$toemail = $customers -> email;
$name = $_POST["name"];
$email = $_POST["email"];
$subject = $_POST["subject"];
$text = $_POST["text"];
@extract($_POST);
$name = stripslashes($name);
$email = stripslashes($email);
$subject = stripslashes($subject);
$text = stripslashes($text);
mail('$toemail',$subject,$text,"From: $name <$email>");
}
}

?>[/code]

The actual mail() script:

[code]$toemail = $customers -> email;
$name = $_POST["name"];
$email = $_POST["email"];
$subject = $_POST["subject"];
$text = $_POST["text"];
@extract($_POST);
$name = stripslashes($name);
$email = stripslashes($email);
$subject = stripslashes($subject);
$text = stripslashes($text);
mail('$toemail',$subject,$text,"From: $name <$email>");[/code]

works just fine if it's not in the while{} statement (or whatever the official nme is), but will not work as the code that is listed first above and I need it to loop through all the emails in the mysql table.

Any ideas?

Thanks in advance,

Butch
Link to comment
https://forums.phpfreaks.com/topic/29187-mail-in-a-while-statement/
Share on other sites

The code could be failing because of the @extract - it may be destroying the POST array. In fact, it doesn't make any sense where you put it as it would have no effect the first time through. Your code i salso VERY inefficient. You do not need to redefine the POST variables every time through the loop - you just need to define them once.

Also note that doing any mass mailing could be against the terms of service for your ISP or hosting company and could get your account terminated.

Try this:

[code]<?php
mysql_connect("localhost" , "id" , "password");
mysql_select_db("violetpa_test");
$query = "SELECT * FROM customers";
$results = mysql_query($query);

if ($results) {

    $name = stripslashes($_POST["name"]);
    $email = stripslashes($_POST["email"]);
    $subject = stripslashes($_POST["subject"]);
    $text = stripslashes($_POST["text"]);

    while ($customers = mysql_fetch_object($results)) {
        $toemail = $customers -> email;
        mail('$toemail',$subject,$text,"From: $name <$email>");
    }
}

?>[/code]
Ok, I tried your code and it still doesn't send anything out.  When I echo the name, email, etc. they all show up properly. So, the info is being passed just fine.  If I take this code and put it out of the while{} brackets then it works just fine.

Any ideas?
Don't use single quotes to surround the variable $toemail. When you do this it tells mail that the user address is the literal string "$toemail"

Try this:
[code]<?php
mail($toemail,$subject,$text,"From: $name <$email>");
?>[/code]

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.