Sending an Email Message...

Discussion in 'Programming' started by JLG2, Oct 14, 2008.

  1. #1
    I have a ColdFusion form setup that basically allows someone to select a name from a list (provide by a database); and input the date, their name, and position. This information is submitted to the database. I would like an email to be sent out every time someone submits this form. Basically notifying someone that the form had been filled out. I know this involves <cfmail>. I just haven't used it before.
    Here is my form code:
    <cfquery name="rsGoldStar" datasource="HRTraining">
    SELECT *
    FROM dbo.tblEmployees
    WHERE Active = 1
    ORDER BY LastName ASC</cfquery>

    .
    .
    .
    <form action="thanksGS.cfm" id="form1" name="form1" method="POST">
    <table width="636" border="0">
    <tr>
    <td><h2><strong>Referred by: </strong></h2></td>
    <td><select name="EmployeeID">
    <cfoutput query="rsGoldStar">
    <option value="#rsGoldStar.EmployeeID#">#rsGoldStar.LastName#, #rsGoldStar.FirstName#</option>
    </cfoutput>
    </select>
    <strong><span class="style3 style2">(Required)</span></strong></td>
    </tr>
    <tr>
    <td width="188"><h2>Date:</h2></td>
    <td width="438"><input name="GoldCardDate" type="text" id="GoldCardDate" size="10" maxlength="10" />
    <strong>(Example: 01/01/2001) </strong></td>
    </tr>
    <tr>
    <td><h2>Applicant Name: </h2></td>
    <td><input name="ApplicantName" type="text" id="ApplicantName" size="40" maxlength="50" /></td>
    </tr>
    <tr>
    <td><h2>Position Applying For: </h2></td>
    <td><input name="PositionApplyingFor" type="text" id="PositionApplyingFor" size="50" maxlength="75" /></td>
    </tr>
    </table>
    <h2>&nbsp;</h2>
    <p><input type="submit" name="Submit" value="submit" />
    <label>
    <input name="Reset" type="reset" id="Reset" value="Reset" />
    </label>
    <label></label>
    </p>
    </form>


    Here is my Action page (thanksGS.cfm) code:

    <cftry>


    <cfquery name="insertRow" datasource="HRTraining">
    INSERT INTO tblGoldStars (EmployeeID, GoldCardDate, ApplicantName, PositionApplyingFor)
    VALUES (<cfqueryparam value = "#form.EmployeeID#" cfsqltype="cf_sql_integer" maxlength="4">, <cfqueryparam value="#form.GoldCardDate#" cfsqltype="cf_sql_date" maxlength="10">, <cfqueryparam value="#form.ApplicantName#" cfsqltype="cf_sql_varchar" maxlength="50">, <cfqueryparam value="#form.PositionApplyingFor#" cfsqltype="cf_sql_varchar" maxlength="75">)
    </cfquery>




    <!--- If you sign up for a twice ERROR--->
    <cfcatch type="database">
    <cfif cfcatch.Message CONTAINS " ">
    </cfif>
    <cfrethrow />
    <cfabort>
    </cfcatch>
    </cftry>


    Any help on how or where to plug in the <cfmail> tag would be greatly appreciated.
     
    JLG2, Oct 14, 2008 IP
  2. robhustle

    robhustle Peon

    Messages:
    98
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    For something simple, why not put the cfmail tag after the try catch block in the thanksGS.cfm file? You can check if an error occurred, and if not, fire off the email.

    When I do this myself, I usually add my outgoing emails to a queue, then have a cron job send the emails off a bit later. I can then filter the messages and control what gets out better.

    If you just have a cfmail tag after a form submission, chances are it will get hijacked at some point.

    $0.02
     
    robhustle, Oct 14, 2008 IP
  3. JLG2

    JLG2 Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thank you for your help with this issue.
     
    JLG2, Oct 15, 2008 IP
  4. robhustle

    robhustle Peon

    Messages:
    98
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Some code to start you.


    Step 1: Adding to the queue

    After the try catch block in thanksGS.cfm:

    <!--- Add the email to the queue --->
    <cfquery name="qAddEmailtoQueue" datasource="#application.dsn#">
    insert into emailQueue
    (recipient,sender,subject,message,link,referer,ip,useragent,sent_status)
    VALUES
    (
    // add your values here //
    )
    </cfquery>


    This will add an item to the queue... you can do filtering here to prevent hijacking or to detect duplicate submissions. You can also implement a rate limiter, or disqualify people if they don't have a cookie... prevent abuse of your system. At least it won't send out emails immediately.


    Step 2: Processing the queue

    You can add further checks here to the stuff you send out before it goes out. Anyway, basic processing, just pull something from the queue and try to send it.

    <!--- This will get a certain number of unsent items to be sent --->
    <cfquery name="qGetMail" datasource="#application.dsn#" maxrows="1">
    select * from emailQueue where sent_status = 0
    </cfquery>

    <cfloop query="qGetMail">
    <cfmail to="#qGetMail.recipient#your@email.address"
    subject="#qGetMail.subject#">

    Add whatever you want to send here, including the link and message.
    #qGetMail.link#
    #qGetMail.message#
    </cfmail>

    <cfquery name="qUpdateMessage" datasource="#application.dsn#">
    update emailQueue
    set sent_status = 1
    where id = #qGetMail.id#
    </cfquery>
    </cfloop>

    Add try catch blocks and cfqueryparams to make it more robust. If it errors out, you can flag it on the database too by updating the sent_status.

    Step 3: Schedule Processing

    Execute this command to schedule your task. If you saved the code for step 2 as 'mailerdaemon.cfm', then you do this:

    <cfschedule action="UPDATE" task="email_queue" operation="HTTPRequest" url="http://your.url.here/mailerdaemon.cfm" startdate="1/1/99" starttime="12:00 AM" interval="3600" requesttimeout="600" resolveurl="No" publish="No">

    You can change the interval and other stuff to have it check whenever you want. Or, you can skip the automation and just send out the emails by manually hitting the mailerdaemon.cfm.

    Not the most robust solution in the world, but it works just fine for smaller sites.
     
    robhustle, Oct 16, 2008 IP