Jump to content

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.

[code=php:0]
$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())";
if(!mysqli_query($con,$sql)){ 
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.";
}
[/code]

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

Thanks in advance!

Entity ;)
Link to comment
https://forums.phpfreaks.com/topic/14187-newsletter-script/
Share on other sites

That's odd.

Using the test script:
[code=php:0]
<?php
$username = 'Will';
$string = 'Please help {name}.';
$string = str_replace("{name}",$username,$string);
echo $string;
?>
[/code]

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.
Link to comment
https://forums.phpfreaks.com/topic/14187-newsletter-script/#findComment-55608
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.