Jump to content

using mail() function for selected emails


netfrugal

Recommended Posts

I have a list of emails and I want to email only selected people by clicking on a checkbox next to their record.

So I created a nice table that shows all records and a checkbox next to each record with the recordID as the value.

But, I am not sure where to go from here.  The next page has the mail() function, but I don't know what to do next.

[B]here's the code on page 1:[/B]




[COLOR=DarkOrange]<form name="form1" method="post" action="mail-to-some.php">[/COLOR]
          [COLOR=DarkGreen] <tr>
            <td class="nicetableheader">REQUESTOR</td>
<td class="nicetableheader">NAME</td>
<td class="nicetableheader">checkbox</td>
          </tr>[/COLOR]
[COLOR=DarkRed]<?
while ($row = mysql_fetch_array($sql)) {
$recID = $row['recID'];
$name = $row['name'];
?>[/COLOR]
              <tr>
[COLOR=DarkGreen]<td class="nicetablerow">[/COLOR][COLOR=DarkRed]<?php echo $name; ?>[/COLOR][COLOR=DarkGreen]</td>
                <td class="nicetablerow" align="center">[/COLOR][COLOR=DarkOrange]<input name="sendto" type="checkbox" value="[/COLOR][COLOR=DarkRed]<? echo $recID; ?>[/COLOR]" />[COLOR=DarkGreen]</td>
              </tr>[/COLOR]
[COLOR=DarkRed]<? } ?> [/COLOR]

          [COLOR=DarkGreen]<tr>
            <td colspan="16" align="right">[/COLOR][COLOR=DarkOrange]<input class="textbox"  type="submit" name="Submit" value="Submit">[/COLOR] [COLOR=DarkGreen]</td>
          </tr> [/COLOR]
[COLOR=DarkOrange]</form> [/COLOR]



[B]And here's the attempted code on the mail-to-some.php.  I am having alot of problems about how to make the query from the db - depending on what records were chosen from the previous page.[/B]


include('includes/configure.php');


[COLOR=DarkRed]$sql = mysql_query("[/COLOR]  [B]?[/B]  [COLOR=DarkRed] "); [/COLOR]


[COLOR=DarkRed]while ($row = mysql_fetch_array($sql)) {
$email = $row['email'];  [/COLOR]


[COLOR=DarkGreen]$to = $email;
$mailheaders = "Attention!\n";
$Subject = "My subject";
$msg = "My message\r\n";

mail($to, $Subject, $msg, "From: IT Help Desk", $mailheaders); [/COLOR]

[COLOR=DarkRed]}
[/COLOR]



If I am completely off my rocker on how this is performed correctly, I won't be surprised. but any help will be appreciated!

Link to comment
https://forums.phpfreaks.com/topic/28997-using-mail-function-for-selected-emails/
Share on other sites

Here is something that will do it all on one page
[code]<?php
// Connect to Database here

if(isset($_POST['Submit'])){
$sendto = array();
foreach($_POST as $key => $val){
  if(substr($key, 0, 6) == "sendto"){
    $sendto[]=$val;
  }
}
$ids = implode(", ", $sendto);
$sql = "SELECT email FROM tablename WHERE id IN ($ids)";
$emails = array();
  $res = mysql_query($sql) or die (mysql_error());
    while($r = mysql_fetch_assoc($res)){
    $emails[]=$r['email'];
    }
$bcc = implode(", ", $emails);
$to = "";
$subject = "Subject Here";
$headers = "From: your e-mail here\r\n";
$headers .= "BCC: $bcc\r\n";

//leave the next 2 lines alone
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

$message = 'Message Here';

$mail = mail($to,$subject,$message,$headers);
if(!$mail){
echo "could not send E-mails";
} else {
echo "emails sent!!";
}
}else {
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method=POST>';
    <tr>
      <td class="nicetableheader">REQUESTOR</td>
      <td class="nicetableheader">NAME</td>
      <td class="nicetableheader">checkbox</td>
    </tr>
<?php
$sql = "SELECT * FROM tablename";
$res = mysql_query($sql) or die (mysql_error());
$i=0;
while($row = mysql_fetch_assoc($res)){
$recID = $row['recID'];
$name = $row['name'];
?>
    <tr>
        <td class="nicetablerow"><?php echo $name; ?></td>
        <td class="nicetablerow" align="center"><input name="sendto<?=$i?>" type="checkbox" value="<? echo $recID; ?>" /></td>
    </tr>
<?php
$i++;
}
?>
    <tr>
      <td colspan="16" align="right"><input class="textbox"  type="submit" name="Submit" value="Submit">  </td>
    </tr>
</form>
<?php
}
?>[/code]

Just fill in your table and field names appropriately

Ray
I suggest making the checkboxes an array, with a name of name="mail[]" or something like that, and the value="[email protected]".

then for you php do something like this:

[CODE]<?php
foreach($_POST['mail'] as $email){
    //your vars go here, and get put into the mail below:
    mail($email,$subject,$message,$headers);
}
?>[/CODE]

Archived

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

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