views:

86

answers:

2

I'm using ColdFusion and I need to automatically send an email when a page loads. I'm using this as a notification for the owner of the site so they know when to check the database for a new entry.

After the form used to collect the data is submitted, a confirmation page loads. When this page loads I need to send the email.

+6  A: 

Here is how I have sent email using ColdFusion

<cfmail to="[email protected]" from="[email protected] (From Name)" subject="Email Subject">
    <cfoutput>
Hello,

Here is the body of the email.

kthxbai
    </cfoutput>
</cfmail>
mr.moses
An immaculate use of kthxbai
Ben Doom
A: 

What version of CF are you using? If it's MX6 or beyond, check your Application.cfc component. (And if you don't have one, make one for the site.) The Application component has a method (a cffunction) called "onRequestStart" which runs as soon as a page is loaded. Put your cfmail tag in the body of this function. Note: Put the cfmail tag inside a cftry tag. That way, even if something goes wrong with your email, the page will still show up for the user. In that case, you can write the error to a log file for your use. You might do something like this:

<cffunction name="onRequestStart" returnType="boolean">
    <cftry>
        <cfmail to="siteowner" from="sitemonitor" subject="user accessed page">
            ...body of email...
        </cfmail>
        <cfcatch>
            <cfsavecontent variable="errormsg">
                <cfdump var="#cfcatch#">
            </cfsavecontent>
            <cflog log="Application" type="error" text="Send mail on page load failed with error message: #errormsg#">
        </cfcatch>
    </cftry>
 </cffunction>

See if something like that works.

Later: So I just realized you were asking to send the email just on certain page loads. You can wrap the whole ... in an "if" block:

<cfif getFileFromPath(getBaseTemplatePath()) is "myConfirmationPage.cfm">
    <cftry>....
    </cftry>
</cfif>

or like that.

Maybe, instead of sending out an email with every confirmation page, it would be easier to have a scheduled email going out every night with a summary of daily activity? It depends on how much activity you expect to get.

Matt Gutting