Reputation: 45
First my code:
<?php
if(isset($betreff) && isset($text)){
$query= "SELECT email, anrede, vorname, nachname FROM newsletter";
$result= mysql_query ($query);
while ($row = mysql_fetch_array($result)) {
$anrede= $row['anrede'];
$vorname= $row['vorname'];
$nachname= $row['nachname'];
$email= $row['email'];
$text = str_replace('[A]', $anrede, $text);
$text = str_replace('[V]', $vorname, $text);
$text = str_replace('[N]', $nachname, $text);
$text = str_replace('[E]', $email, $text);
$body=$text;
strip_tags($text, '<br><b><img><a>');
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Commeatus-IO <[email protected]>';
mail($email, $betreff, $body, $headers);
echo 'Email wurde gesendet an: ' . $email. '<br>';
}
?>
Now this code should Email to every user in the database with the certain email name surname, if I insert in the text [V]
(for surname) or [E]
for Email. but if i do this, it shows in every email the same surname and name. Why?
Thanks
Upvotes: 1
Views: 48
Reputation: 300
In the first iteration [A], [V], [N], [E] are replaced and they does not exist in the text anymore, and thus can't be replaced.
Save the text in a new variable and in each iteration do this as following:
<?php
if(isset($betreff) && isset($text)){
$body_text = $text; //Add this
$query= "SELECT email, anrede, vorname, nachname FROM newsletter";
$result= mysql_query ($query);
while ($row = mysql_fetch_array($result)) {
$anrede= $row['anrede'];
$vorname= $row['vorname'];
$nachname= $row['nachname'];
$email= $row['email'];
$body_text = str_replace('[A]', $anrede, $body_text);
$body_text = str_replace('[V]', $vorname, $body_text);
$body_text = str_replace('[N]', $nachname, $body_text);
$body_text = str_replace('[E]', $email, $body_text);
strip_tags($body_text, '<br><b><img><a>');
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Commeatus-IO <[email protected]>';
mail($email, $betreff, $body_text, $headers);
echo 'Email wurde gesendet an: ' . $email. '<br>';
}
}
?>
Upvotes: 0
Reputation: 10327
This is because you are overwriting the text containing the [A][N][V][E]
placeholders, and the subsequent iterations can't find them anymore.
Feed your $body
variable from $text first, this way you won't overwrite your template.
You had also missing a closing curly brace at the end of your loop.
Your code should look like this:
<?php
if(isset($betreff) && isset($text)){
$query= "SELECT email, anrede, vorname, nachname FROM newsletter";
$result= mysql_query ($query);
while ($row = mysql_fetch_array($result)) {
$anrede= $row['anrede'];
$vorname= $row['vorname'];
$nachname= $row['nachname'];
$email= $row['email'];
// feed $body with your template ($text)
$body = $text;
// use $body as the working copy (replace the placeholders in $body)
$body = str_replace('[A]', $anrede, $body);
$body = str_replace('[V]', $vorname, $body);
$body = str_replace('[N]', $nachname, $body);
$body = str_replace('[E]', $email, $body);
// I assume you wanted to strip tags in the e-mail text, not in the template
strip_tags($body, '<br><b><img><a>');
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: Commeatus-IO <[email protected]>';
mail($email, $betreff, $body, $headers);
echo 'Email wurde gesendet an: ' . $email. '<br>';
}
}
?>
Upvotes: 2