How to email to mulitple people from a CF grid selection

Discussion in 'Programming' started by harters, Apr 14, 2010.

  1. #1
    Hello everyone. As the name suggests i am trying to figure out in CF9 over MySQL how to send multiple emails based on selected records from a grid.

    I have a table called sstblemailnewmembers
    this has fields:
    fldemailid (Int + Auto Incr)
    fldemail (TEXT)
    fldsent (DATE)
    CHECKED (SET 'true','false' DEFAULT false)

    I then have a cf query in my page called emailnewmembers.cfm


    <cfquery name="emailnewmembers" datasource="SAPLIVE">
    SELECT fldemailID, fldemail, fldsent, CHECKED
    FROM sstblemailnewmembers
    </cfquery>
    So far so good (i think)

    I then have a form defined:


    <cfform action="AdminTransfer.cfm"
    width="1170"
    method="post"
    enctype="multipart/form-data"
    format="Flash"
    preloader="true"
    style="backgroundAlpha: 0;"
    wmode="transparent"
    timeout="3000"
    name="adminhome"
    onload="loadAS();">

    the onload="loadAS();" bit is used so I can select multiple records using this script which is the next bit to be defined:


    <cfformitem type="script">
    function loadAS(){
    _root.myGrid.multipleSelection = true;
    }
    </cfformitem>
    and finally on this page we have the grid itself and the submit button as below:


    <cfgrid name="myGrid"
    selectmode="edit"
    query="emailnewmembers"
    rowheaders="no">
    <cfgridcolumn name="CHECKED" header="Select" type="boolean">
    <cfgridcolumn name="fldemailID" header="Email ID">
    <cfgridcolumn name="fldemail" header="Email Addrerss">
    <cfgridcolumn name="fldsent" header="Date Sent">
    </cfgrid>
    <cfinput type="submit"
    name="sendemailnewmembers"
    value="Send Emails"
    style="#buttonStyle#">
    So far so good i guess?

    The submit button calls the action page defined in the form tag.
    This is what I have so far defined in the action page "admintransfer.cfm"...


    <cfif isdefined("FORM.endemailnewmembers")>
    <cfloop index = "Counter" from = "1" to =
    #arraylen(Form.emails.CHECKED)#>

    <cfif Form.emails.CHECKED[counter] is "true">
    <cfquery name="emailnewmembers"
    datasource="SAPLIVE">
    UPDATE sstblemailnewmembers
    SET
    fldsent = NOW()
    </cfquery>

    <cfmail
    to = "#Form.fldemail#"
    from = "myemailaddress@mydomain.com"
    subject = "Welcome New Member">
    Dear New Member...
    </cfmail>
    </cfif>
    </cfloop>
    </cfif>
    So, if I select 3 records an email is getting sent 3 times to the last email address of the last record I select.

    I am assuming there is something wrong with the loop and probably a better way to do this but I am quite new and this is the best I can come up with so if anyone cares to point me in the right direction I would be enormously gratful indeed
     
    harters, Apr 14, 2010 IP