Skip to main content
onniec77729686
Participant
January 13, 2019
Answered

Multiple Email recipients with check boxes

  • January 13, 2019
  • 4 replies
  • 795 views

I have a list of recipients with email links. Can a check box be used to select specific names and have those addresses populate in the To: field on a blank email?

    This topic has been closed for replies.
    Correct answer osgood_

    onniec77729686  wrote

    But sadly, I don't know how to get there. :-(

    You would need to use some server-side language to process the information, which IF you ARE dealing with form information you should be doing anyway.

    It's really quite easy. The below example is in it's simplest form. You loop through ALL the ticked checkboxes and send the email message to their designated email addresses, collected from the forms input/checkbox fields.

    <?php

    if(isset($_POST['submit'])) {

    foreach ($_POST['email'] as $email) {

    mail($email, 'Subject','Message Goes Here', "From: Whoever");

    }

    }

    ?>

    <h1>Send Email</h1>

    <form name="send_email" action="index.php" method="post">

    <p><input type="checkbox" name="email[]" value="email@address_1.com">email@address_1.com</p>

    <p><input type="checkbox" name="email[]" value="email@address_2.com">email@address_2.com</p>

    <input type="submit" name="submit" value="Submit" />

    </form>

    Breaking the above down (you should set the form 'action' field to the page which processes the script. I very often tend to keep the php script in the same file as the form itself, right at the top of the file, before anything else:

    If you keep the script in the same page as the form you don't want the script to run when the page loads so you surround the script in a php 'if' query, testing to see if the form submit button has been clicked. If it has the script runs.

    if(isset($_POST['submit'])) {

    }

    You then use a 'foreach' loop to loop through all the form checkboxes that have been checked:

    foreach ($_POST['email'] as $email) {

    }

    Then mail your message to the specific emails using the php mail function:

    mail($email, 'Subject','Message Goes Here', "From: Whoever");

    You collect the email form field values into an array when the checkbox/s have been checked so the script can loop through them. That is why you have set the name field to 'email[]' - the brackets designates to store in an array of information:

    <p><input type="checkbox" name="email[]" value="email@address_1.com">email@address_1.com</p>

    That might help, a bit, maybe.

    There are other more complicated workflows like using ajax to smooth the process of page reload but at this stage I think this will be enough for you to try and get your head around.

    It looks more complex than it really is and it goes without saying you should always use validation on your forms. This is just a bare-bones example of the process without the all singing all dancing necessities in place. You can choose as many or as little of those as you like.

    4 replies

    Participant
    June 7, 2019

    This makes no sense to me.

    Nancy OShea
    Community Expert
    Community Expert
    January 13, 2019

    onniec77729686  wrote

    I have a list of recipients with email links. Can a check box be used to select specific names and have those addresses populate in the To: field on a blank email?

    This sounds like a job for a dedicated e-mail client like MS Outlook rather than Dreamweaver.  Compose a message.  Use your address book of intended recipients.

    Nancy O'Shea— Product User & Community Expert
    onniec77729686
    Participant
    January 13, 2019

    When an email link is selected that recipient's address automatically shows in the email "To:" field. I have a roster of members where I would like someone to be able to select more than one name, have all the names show in the "To:" field, compose the email message and then send to all addresses at once. Like a group email. But sadly, I don't know how to get there. :-(

    Thanks

    osgood_Correct answer
    Legend
    January 13, 2019

    onniec77729686  wrote

    But sadly, I don't know how to get there. :-(

    You would need to use some server-side language to process the information, which IF you ARE dealing with form information you should be doing anyway.

    It's really quite easy. The below example is in it's simplest form. You loop through ALL the ticked checkboxes and send the email message to their designated email addresses, collected from the forms input/checkbox fields.

    <?php

    if(isset($_POST['submit'])) {

    foreach ($_POST['email'] as $email) {

    mail($email, 'Subject','Message Goes Here', "From: Whoever");

    }

    }

    ?>

    <h1>Send Email</h1>

    <form name="send_email" action="index.php" method="post">

    <p><input type="checkbox" name="email[]" value="email@address_1.com">email@address_1.com</p>

    <p><input type="checkbox" name="email[]" value="email@address_2.com">email@address_2.com</p>

    <input type="submit" name="submit" value="Submit" />

    </form>

    Breaking the above down (you should set the form 'action' field to the page which processes the script. I very often tend to keep the php script in the same file as the form itself, right at the top of the file, before anything else:

    If you keep the script in the same page as the form you don't want the script to run when the page loads so you surround the script in a php 'if' query, testing to see if the form submit button has been clicked. If it has the script runs.

    if(isset($_POST['submit'])) {

    }

    You then use a 'foreach' loop to loop through all the form checkboxes that have been checked:

    foreach ($_POST['email'] as $email) {

    }

    Then mail your message to the specific emails using the php mail function:

    mail($email, 'Subject','Message Goes Here', "From: Whoever");

    You collect the email form field values into an array when the checkbox/s have been checked so the script can loop through them. That is why you have set the name field to 'email[]' - the brackets designates to store in an array of information:

    <p><input type="checkbox" name="email[]" value="email@address_1.com">email@address_1.com</p>

    That might help, a bit, maybe.

    There are other more complicated workflows like using ajax to smooth the process of page reload but at this stage I think this will be enough for you to try and get your head around.

    It looks more complex than it really is and it goes without saying you should always use validation on your forms. This is just a bare-bones example of the process without the all singing all dancing necessities in place. You can choose as many or as little of those as you like.

    onniec77729686
    Participant
    January 13, 2019

    Thanks for your help!

    BenPleysier
    Community Expert
    Community Expert
    January 13, 2019

    Yes, but it will need some coding. So that we can answer the question more specifically, what are you aiming to do? When you say 'To: field on a blank email', what exactly do you mean?

    Wappler is the DMXzone-made Dreamweaver replacement and includes the best of their powerful extensions, as well as much more!