Php/mysql

software development

#1

i’m trying to do my own newsletter program. My problem is how do I get to print the $toemail in the newsletter without it attaching the same message over in the while loop.

ex.

$query = mysql_query("SELECT email FROM newsletter", $link);

$message .= "===========================================================================\n";
$message .= "Click the link below to cancle your\n subscription.\n\n";
$message .= "http://www.domain.com/?action=unsubscribe&email=$toemail\n";
$message .= "===========================================================================\n";

while ($i < mysql_num_rows($query)) {
$toemail = mysql_result($query, $i);

$fd = popen($sendmail, "w");
fputs($fd, "To: $toemail\r\n");
fputs($fd, "From: \"MyDomain.com\" <noreply@mydomain.com>\r\n");
fputs($fd, "Subject: $subject\r\n");
fputs($fd, "X-Mailer: PHP Mail\r\n\r\n");
fputs($fd, stripslashes($message));
pclose($fd);

$i++;
}

basically if i leave the $message .= “”; out of the while loop, it doesn’t loop the $messages when sending it in the email, but it doesn’t print the email for $toemail; so its either one or the other, how i can i get it to work the way I want?

I have a mysql database with 1 email per row… I want to send the newsletter to each person, with the option to unsubscribe.


#2

Read http://www.php.net/ documenation on string operators. What you want is something like:

while() { $message = "first line\n"; # $message is "first line\n" $message .= "second line\n"; # $message is now "first line\nsecond line\n" }The first statement is an assignment to clear out whatever $message had in it before. The second is an concatenating assignment to simply append another string to the end of $message.

:cool: Perl / MySQL / HTML+CSS


#3

I already have something in $message before it runs through the loop though…

basically

$message = “some long story”; that is inputted by my form.

then once it reads through the program, I append the footer to the message so I don’t have to keep on re-typing it.

$message .= “===========”;
$message .= “click here to unsubscribe $toemail”;
$message .= "===========’;

then it goes through the while loop

while () {

// then i have it search the database, and keep looping until each person receives the mail.
}


#4

Then use another variable

while() {
$this_message = $message;
$this_message .= “unsubscribe $toemail\n”;
}

:cool: Perl / MySQL / HTML+CSS


#5

wouldn’t the message keep looping, adding the last line with the

say the loop had to go over 30 times - it would add the same line 30 times, wouldn’t it?

$this_message .= “unsubscribe $toemail”;

what i mean by looping is…

say the
$message = “my long newsletter story”;

then the $message went into a function that sends mail to each person in the database. each person in the database has a seperate row.

ex.

email 1
email 2
email 3
email 4

so i have a mysql query that reads the database and just pulls out each person email during the loop… it picks one email out, sends the newsletter, then loops again until each person gets their email.

but in each email, i want it to also list their $email to unsubscribe when clicking the link… the thing is, its adds whatever is in the $message over and over and over again.

so basically the first email that sends is fine,
then the second email has just the $message, but it adds the rest of the email message (email footer) part… and it just keeps adding during the loop. so how can i stop it from looping, but still get all the emails sent, without it adding more email footers in the loop.


#6

OK not sure how more clear I can make this.

[code]$message = “This is line is sent to all subscribers.\n”;

while () {
$toemail = mysql_result($query, $row_number);
$this_message = $message;
$this_message .= “This is line is sent only to $toemail\n”;
echo $this_message;
}[/code]$message is never changed!!! It stays the same.

The only thing that changes is $this_message

The first assignment ( = operator) makes $this_message equal $message exactly.
Then you use concatenating assignments ( .= operator ) to add strings specific to the current iteration.

So you get at the end of each iteration:

loop 1: $this_message == "This is line is sent to all subscribers.\nThis is line is sent only to subscriber 1\n"; loop 2: $this_message == "This is line is sent to all subscribers.\nThis is line is sent only to subscriber 2\n"; loop 3: $this_message == "This is line is sent to all subscribers.\nThis is line is sent only to subscriber 3\n";
Using the assignment (= operator) and an extra variable stops the loop from doing what your original code does.

:cool: Perl / MySQL / HTML CSS


#7

oh yeah… i didn’t even think about it that way…haha

thanks it worked!