Jump to content


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


Newsletter Script

Recommended Posts

Hey! I've recently decided to make my site a newsletter because it just seems cool. Anyway I've encountered a small problem.

Well I've have people register an e-mail under a choosen user to be more specific about things. But when I go to send an e-mail it gives the username of the very first user, not the user specific to that e-mail and user combination. I'm using a str_replace() function so that when it finds {username} or {email} it will replace those with that users email and username in both the subject and message. Like so

$message = str_replace("{username}", "$user", "$message");
$message = str_replace("{email}", $email", "$message");
And the same for $subject just replace $message with $subject ;)

Now I've tried a few things, the last one was to reset those variables

mail(//all the mail stuff here ^_^);
$user ="";
$email = "";

(this is all in a while loop, mind you!)

but still it uses the very first user that the e-mails are allowed to be sent to! (Where recieve = 1 ;))

Now this is the code used to send the newsletter.

$do = $_GET['version'];
if((isset($do))&&($do=="do")) {

$message = $_POST['message'];
$subject = $_POST['subject'];
$from = $_POST['from'];
// Write News Letter to database
$sql="INSERT INTO past_newsletters(content,subject,written) VALUES('$message','$subject',NOW())";
die('Error: ' . mysqli_error());

$result99 = mysqli_query($con, "SELECT user, email FROM newsletter WHERE recieve='1' AND black='0'");
$count = 0;

while ($row99 = mysqli_fetch_array($result99)) {
$user = $row99['user'];
$email = $row99['email'];
$message = str_replace("{username}", $user, $message);
$message = str_replace("{email}", $email, $message);
$message = stripslashes($message);

$subject = str_replace("{username}", $user, $subject);
$subject = str_replace("{email}", $email, $subject);
$subject = stripslashes($subject);
mail("$email", "$subject", "$message", "From: Entitys Arena <$from>");
$email = "";
$user = "";
$count = $count + 1;
echo "Emails sent sucessfully.<br />Sent to: <b>$count</b> users.";

If there is [b]anyone[/b] who can help with this I will be majorly greatful!

Thanks in advance!

Entity ;)

Share this post

Link to post
Share on other sites
That's odd.

Using the test script:
$username = 'Will';
$string = 'Please help {name}.';
$string = str_replace("{name}",$username,$string);
echo $string;

Works fine for me.  It sounds like there is either an issue with the POSTed values, incorrect comparison (i.e. mistakenly entering {name} rather than {username}, or the rows being pulled back.  Do a var_dump($_POST['message']) and tell me what it says.

Share this post

Link to post
Share on other sites
Done it now, thanks for the help! Was helpful as heck!

Share this post

Link to post
Share on other sites


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.