Search code examples
phphtmlemailbuttonmailto

For each loop through mailto button


I'm making a button for my website and i just cant get this to work. What im trying to do is that if a user presses the button all the selected e-mails are put into an array. which is called $FacMail and if u press the button again (Which will change text from select to send) it opens the mail client. The thing is, i want a kind of loop that for each mail address in the array a mail client window is opened, it also pre-fills the subject and actual message. But i have no idea where to start and if its even possible. im using html Mailto. so when i have 3 mail adresses in the array it should open 3 windows (in my case Mozilla Thunderbird).

So is this possible? and if so, how?

    <?php 
    if(isset($_GET['FacMail'])){
    <a href="Mailto:<?=$_GET['FacMail']; ?>?subject=<?=MAIL_SUBJECT?>&BODY=<?=MAIL_BODY?>><button>Send</button></a>
<td width="18" height="18" align="center" background="images/smallbut.gif">
<a href="<?php echo $url2 ?>" target="_self" class="helpbutton" title="<?php echo Back ?>"> &#8635 </a></td>
<?php } 
}else{
?>  &emsp;<input type="submit" name="select_mail" id="select_mail" value="Select Mail" title="Select the mail addresses"/><?php } ?>

This is the actual button. the Form will submit it to another file where the mail addresses are picked from the mysql database but thats irrelevant.


Solution

  • Something like this?

    EDIT: after reading your code, I'm guessing you need another method, this one does not use php. Not needed to load another page to process an array. Not sure if it can be done from server side.

    HTML:

    <form>
        <input class="address" type="checkbox" value="[email protected]" > [email protected]<br>
        <input class="address" type="checkbox" value="[email protected]" > [email protected]<br>
        <input class="address" type="checkbox" value="[email protected]" > [email protected]<br><br>
        <button type="button" id="mail">Open vensters</button>
        <p class="temp-links"></p>
    </form>
    

    jQuery:

    $(document).ready(function() {
        $('.temp-links').hide();
    
        // Add mailto link for each checked box
        $(document).on('change', '.address', function(e) {
            var trigger = $(this);
    
            if(trigger.is(':checked')) {
                $('.temp-links')
                    .append($('<a></a>')
                        .attr('href', 'mailto:'+trigger.val())
                        .text('test')
                        .bind('click', function() {
                            // Needed because mailto links can not be triggered by jQuery...
                             window.location.href = $(this).attr('href');   
                        })
                    );
            }
            else {
                $('.temp-links a[href="mailto:'+trigger.val()+'"]').remove();
            }
    
        });
    
        // On btn click trigger click on all mailto links
        $(document).on('click', '#mail', function(e) {
            e.preventDefault();
            $('.temp-links a').trigger('click');
        });
    });
    

    This uses jQuery. To fill the subject and message you'll have to add attributes to the link tags etc..

    Enjoy!