Okay, you are very close to the finish…
First, let’s make a few minor changes and explain how data from the database is used.
(You wanted to learn, so…)
The section where you pull your subscribers and place the data into arrays to use is really
a waste of processing time. Since the QUERY to the database places all of the data into one
array already and then you want to use that list as your major loop, it really makes no sense
to place them into another two arrays. So, let’s drop those and make the subscriber list your
outside-major loop. So, this:
while($row = mysqli_fetch_array($subscribers)) {
$emails[] = $row[‘email’];
$names[] = $row[‘firstname’]." ".$row[‘lastname’];
}
echo implode($emails);
Would just be the first line only. We will loop thru all of the subscribers and just use the info
where we need it inside the rest of the code. ( The ending brace } would be moved to the bottom. )
As far as how to stuff names into email templates, that can be tricky. There are two ways to handle it.
You can hard code your “salutation” in this procedure by just echo’ing out " Dear " . $row[“name”]; " or
something similar and then actually place the rest of the email after it. Or you can ENCODE your name
inside the template and replace it with the data as needed. to do this, you would place a code such as
" " inside the email template. Then, you would use a string-replace command to
swap that code with the real name. Which way is best. Well, if you have the name used in different
places inside the email template, then the second was is best. If it always is in the same place and
not important, the first way is faster and easier. In the code below I will use the first version.
So, the process is, again, simple. We will just parse thru all of your subscribers which are loaded into
an array by the QUERY. Next, I think I was confused about the number of email templates. In your
first posts, you mentioned many emails with the ID number. Now you are talking about only one of
them. So, I will assume you have one email template to send out to each subscriber. So, that is easy.
We only need one loop then, the subscriber loop. Less code…
Here is a new version for you to test. Note it is smaller than the last one. Sorry for the spacing, it does
seem we are using different editors and I have to redo the indents each time… Anyway, check this one
out and let us know the results…
Note that I took out the extra displays and added some comments. Also, you send one email at a time
not one large email with multiple email addresses. This way, it gets past the spam filters on servers.
[php]
// Send newsletter
if(isset($_GET[‘process’]) && $_GET[‘process’] == “send”) {
// Get email template id and get it from database
$id = $_GET['id'];
$template = mysqli_query($con,"SELECT * FROM tblnewslettermailings WHERE id=".$id." LIMIT 1") or die(mysql_error());
$temp = mysqli_fetch_array($template);
$name = $temp['name'];
$subject = $temp['subject'];
$body = $temp['body'];
// Get subscribers
$subscribers = mysqli_query($con,"SELECT email, firstname, lastname FROM tblnewsletter") or die(mysql_error());
// Set up the parameters that do not change for the email...
$from = "Administrator <[email protected]>";
$headers = 'From: ' . $from . "\r\n" . 'Reply-To: ' . $from . "\r\n" . 'X-Mailer: PHP/' . phpversion();
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// Loop thru all subscribers sending the email
while($row = mysqli_fetch_array($subscribers)) {
$to = $row['email'];
$msgbody = 'Dear ' . $row['firstname'] . " " . $row['lastname'] . ",<br />' . nl2br($body) . '<br />';
mail($to, $subject, $msgbody, $headers);
echo "Message Sent to ".$to."<br />";
}
mysqli_close($con);
}
[/php]
Note also that I moved a lot of the emails set up code to before the loop. No need to keep repeating
that part over and over for all of the subscribers. Just do it once.
Let us know how this version works for you. Should be done… Unless you wanted to move the name
of the subscriber inside your email. Like if you wanted to use a like like this one:
Dear John… … Do not forget, John, that… …
In other words if you have a need to embed the name in more than one place in the letter…
(Another project maybe… Along with converting to an HTML email with graphics and borders, etc…)