Here's how I do it. It's cheesy but it works. Hope it points you in the right direction.
I create a report with a 'send email' option buttons based on a database query. I enclose the report in <FORM> tags.
I get email address from the database with this query:
SELECT lastname,
firstname,
if(email<>'',concat('<input type=checkbox checked=true name=emailarray[] value=',email,'> Send email'),'<BR>')
FROM person
I include these buttons:
<input type=submit class=button name=email value="Send Email to Checked Addresses">
<input type=button class=button name=uncheck value="Uncheck All" onclick="docheckbox(this)"></center>';}
and include this JAVASCRIPT code on the page:
//JAVASCRIPT!!!
function docheckbox(button){
y=document.forms[0].elements;
xxx=0;
while(y[xxx]){if((y[xxx].type=='checkbox')) {
y[xxx].checked=!y[xxx].checked;
}
xxx++;
}
if(button.value=='Check All'){
button.value=='Uncheck All';
}
else{
button.value='Check All';
}
}
</script>
The database query looks like this:
When the submit button is clicked, the report form POSTs an array of email addresses to a send email page.
The send email page is based on this code:
function emailtoarray($emailarray){
$emailarray=array_unique($emailarray);
buildheader();
echo "<h2>Send Email</h2>
<center><table style='width:50%;' border=1><tr><td class=list >
To These Addresses</td></tr><tr><td>";
foreach ($emailarray as $key=>$value){
echo " $value,";
}
echo "</td></tr></table>";
echo "<form method=post name=emailform action=index.php?action=sendemailtoarray>\n";
foreach ($emailarray as $key=>$value){
echo "<input type=hidden name=emailarray[$key] value=$value>\n";
}
$id=useridfromcookie();
$from=oneresult("SELECT email
FROM person
WHERE id =$id");
echo "<center><table border=1 cellpadding=6 ><tr><td><table border=1><tr><td class=list>Subject</td>
<td><input class=text name=subject size=50 value=''></td></tr>
<tr><td class=list>From</td>
<td><input class=text name=from size=50 value='$from'></td></tr>
<tr><td class=list>Email text</td>
<td><textarea".' style="text-align:left" class="text" name=message rows="9"
cols="43" >Dear $firstname:
We appreciate your business.'."</textarea>
</td></tr>
</table></td><td >
\$firstname=First Name<BR>
\$lasttname=Last Name<BR>
</td></tr><tr><td colspan=2 style='text-align:center'><input class=button class=buton type=submit name=confirm value='send email'>
<input TYPE=button class=button VALUE='Cancel'onClick='window.location=\"index.php\"'>
</td></tr></table></center>";
echo "</form>";
buildfooter();
}
Now that I have an array of email address, a subject, message, and from value, I send them to the email making function, which sends an individual, personalized email to each address on the array.
function sendemailtoarray($emailarray, $subject, $message, $from){
$origmessage=stripslashes($message);
foreach($emailarray as $value){
$firstname= oneresult("SELECT firstname from person WHERE email='$value'");
$lastname= oneresult("SELECT lastname from person WHERE email='$value'");
$message= str_replace('$firstname',"$firstname",$origmessage);
$message= str_replace('$lastname',"$lastname",$message);
mail($value, $subject, $message,
"From: $from\r\n" ."X-Mailer: PHP/" . phpversion());
}
indexpage("Sent emails as requested");
}