OK, hours later and much searching yields nothing....

We do newsletters on our website, they are created via a form in the backend. Currently they are generic whatever you enter in the textfield is sent and I was looking to add predefined headers and footers to them that could be edited on-the-fly. Also with that I wanted to add some variables to personalize our newsletters. (eg. Dear <#USERNAME#>...)

So basically Im trying to get the posted data analyzed and have specific "variables" replaced where relevant.

Here is the latest code I have tried....

if($_POST['action'] == "newsletter" && phpa_securepost($_POST)) {


    #//
    if(empty($_POST['subject']) || empty($_POST['content'])) {
        $ERR = $ERR_5014;
    } else {
        $COUNTER = 0;
        switch($_POST['usersfilter']) {
            case 'test':
            $query = "select email, name, nick from PHPAUCTIONXL_users where nick='sabyre'";
            break;
            case 'all':
            $query = "select email, name, nick from PHPAUCTIONXL_users where nletter='1' and birthdate<>'99999999'";
            break;
            case 'active':
            $query = "select email, name, nick from PHPAUCTIONXL_users where nletter='1' AND suspended=0 and birthdate<>'99999999'";
            break;
            case 'admin':
            $query = "select email, name, nick from PHPAUCTIONXL_users where nletter='1' AND suspended=1 and birthdate<>'99999999'";
            break;
            case 'fee':
            $query = "select email, name, nick from PHPAUCTIONXL_users where nletter='1' AND suspended=9 and birthdate<>'99999999'";
            break;
            case 'notconfirmed':
            $query = "select email, name, nick from PHPAUCTIONXL_users where nletter='1' AND suspended=8 and birthdate<>'99999999'";
            break;
            case 'everyone':
            $query = "select email, name, nick from PHPAUCTIONXL_users where birthdate<>'99999999'";
            break;
        }

        $result = mysql_query($query);
        $row = mysql_fetch_array($result);





        //echo $content;        

        while($row = mysql_fetch_array($result)) {

$patterns = array();
$patterns[0] = '/<#s_nick#>/';

$replacements = array();
$replacements[0] = $row['nick'];

        preg_replace($patterns,$replacements,$_POST['content']);

    echo $_POST['content']; 






            if(mail($row['email'],stripslashes($_POST['subject']),stripslashes($_POST['content']),"From:".$SETTINGS['sitename']." <".$SETTINGS['adminmail'].">\n"."Content-Type: text/html; charset=$CHARSET")) {
                $COUNTER++;
            }
        }
        if(!$result) {
            $ERR = $ERR_001;
        } else {
            $ERR = $COUNTER.$MSG_5300;
        }
    }
}

The message variables work with other areas of our site. Like if a new user signs up or someone wins an auction, but the variables are pulled from a file and are only mailed to 1 person at a time. Also we would like the info to be in the TEXTAREA so we can edit it for different newsletters.

Working Example:

$buffer = file($include_path."mail_endauction_youwin_nodutch.".$USERLANG.".inc.php");

$i = 0;

$j = 0;

while($i < count($buffer)){

    if(!ereg("^#(.)*$",$buffer[$i])){

        $skipped_buffer[$j] = $buffer[$i];

        $j++;

    }

    $i++;

}

$message = implode($skipped_buffer,"");

//--Change TAGS with variables content
$message = ereg_replace("<#s_name#>",$Seller['name'],$message);
$message = ereg_replace("<#s_nick#>",$Seller['nick'],$message);

Thanks in advanced for any help with this. I know php well, but this makes me feel like a newb. Maybe i've just been on it to long...

You don't say exactly what your problem is but for one thing you are using mysql_fetch_array twice on your first query (lines 34 and 42) which I am certain will be causing you problems.

Line 42 is a loop. "While the SQL response has an array, email and increase the counter".

This is the backend... See the variable <#s_nick#> in the textarea? Upon clicking the send button that will be passed as $_POST['content']

news

I'm looking to get that post analyzed for <#variables#> and swap them with things like peoples usernames, real names, etc...

The problem is the most I can get it to do is return 'Y', the first letter in the textarea.

I know what the while loop is for but you can't use the mysql_fetch_array twice and allocate it to the variable $row twice, you need to remove line 34 ($row = mysql_fetch_array($result);

With regard to just returning 'Y' - have you checked your fild in the databse (that it is the corrct type etc) and have you tried running your script in PHPMyAdmin to see if you get errors there?

Yeah, I checked all the queries and they are fine.

The "y" actually doesn't come from the database it comes from $_POST['content'] The idea is for it to get not just the 'y', but in this example, "Yo <#s_nick#>" and replace <#s_nick#> with the username of the member the mail is being sent with.

Basically we are trying to mix POST data with SQL data and check for specifics and change where relavent.

Thanks

I got it down to my non understanding of preg_replace vs ereg_replace.

$nickcode = '<#s_nick#>';
echo "$nickcode <br>";
$nick = $row['nick'];
echo $nick;
preg_replace(array($nickcode),$nick,$buffer);
echo "<br> $buffer";

Still returns <#s_nick#> when is should return $nick....

Thanks for the replies!

Here is the working code:

if($_POST['action'] == "newsletter" && phpa_securepost($_POST)) {
    #//
    if(empty($_POST['subject']) || empty($_POST['content'])) {
        $ERR = $ERR_5014;
    } else {
        $COUNTER = 0;
        switch($_POST['usersfilter']) {
            case 'test':
            $query = "select * from PHPAUCTIONXL_users where nick='sabyre'";
            break;
            case 'all':
            $query = "select * from PHPAUCTIONXL_users where nletter='1' and birthdate<>'99999999'";
            break;
            case 'active':
            $query = "select * from PHPAUCTIONXL_users where nletter='1' AND suspended=0 and birthdate<>'99999999'";
            break;
            case 'admin':
            $query = "select * from PHPAUCTIONXL_users where nletter='1' AND suspended=1 and birthdate<>'99999999'";
            break;
            case 'fee':
            $query = "select * from PHPAUCTIONXL_users where nletter='1' AND suspended=9 and birthdate<>'99999999'";
            break;
            case 'notconfirmed':
            $query = "select * from PHPAUCTIONXL_users where nletter='1' AND suspended=8 and birthdate<>'99999999'";
            break;
            case 'everyone':
            $query = "select * from PHPAUCTIONXL_users where birthdate<>'99999999'";
            break;
        }
        $result = mysql_query($query);
        while($row = mysql_fetch_array($result)) {

$buffer = $_POST['content'];

$i = 0;
$j = 0;

while($i < count($buffer)){
if(!ereg("^#(.)*$",$buffer[$i])){
$skipped_buffer[$j] = $buffer[$i];
$j++;
 }
$i++; }

$message = implode($skipped_buffer,"");

$patterns[0] = '#m_nick#';
$patterns[1] = '#m_name#';
$patterns[2] = '#m_id#';
$patterns[3] = '#m_email#';
$patterns[4] = '#m_reg_date#';
$patterns[5] = '#m_lastlogin#';
$patterns[6] = '#m_bids_remaining#';

$replacements[0] = $row['nick'];
$replacements[1] = $row['name'];
$replacements[2] = $row['id'];
$replacements[3] = $row['email'];
$replacements[4] = $row['reg_date'];
$replacements[5] = $row['lastlogin'];
$replacements[6] = $row['bids_remaining'];

$message = str_replace($patterns, $replacements, $buffer);

            if(mail($row["email"],stripslashes($_POST["subject"]),stripslashes($message),"From:".$SETTINGS["sitename"]." <".$SETTINGS['adminmail'].">\n"."Content-Type: text/html; charset=$CHARSET")) {
                $COUNTER++;
            }
        }
        if(!$result) {
            $ERR = $ERR_001;
        } else {
            $ERR = $COUNTER.$MSG_5300;
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.