1 Replies - 596 Views - Last Post: 05 October 2014 - 07:45 AM Rate Topic: -----

#1 cairdin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 03-October 14

How Can I Use onClick To Send Background CFMAIL Message?

Posted 04 October 2014 - 11:33 AM

I am attempting to have an email alert ( via <CFMAIL> ) sent to me in the background (not visible to the user) whenever a user downloads a document from my webpage. I have tried the following using an onclick event handler. The code doesn't barf at all, and the document downloads fine, but the email message doesn't get fired. It should be a relatively simple thing to do, but I am still pretty much a novice at this and may be missing something really obvious , but I am still pretty much a novice at this and may be missing something really obvious. I would really appreciate any help. Thanks!

This is the code I am trying to use to call the template as the user clicks to download the document. It obviously isn't right, but gives the idea of what I am attempting to do.

<a href="doc_directory/doc_file.pdf" onclick="doc_download_alert.cfm";>Click here to download document</a>


This is the code for the called template that is supposed to fire off the <CFMAIL> message in the background:

<!doctype html public "-//W3C//dtd html 3.2 Final//EN">
<html>
<head>
</head>
<body>

<cfprocessingdirective suppresswhitespace="no">
<cfmail
type="html"
from="my website"						
to="my_email_address@email.com"
subject="download alert">
<p>ALERT: document downloaded from my page
</cfmail>
</cfprocessingdirective>

</body>
</html>

This post has been edited by Craig328: 05 October 2014 - 07:16 AM
Reason for edit:: Used the proper code tag for the post


Is This A Good Question/Topic? 0
  • +

Replies To: How Can I Use onClick To Send Background CFMAIL Message?

#2 Craig328  Icon User is offline

  • I make this look good
  • member icon

Reputation: 1947
  • View blog
  • Posts: 3,488
  • Joined: 13-January 08

Re: How Can I Use onClick To Send Background CFMAIL Message?

Posted 05 October 2014 - 07:45 AM

Welcome to DIC cairdin! So, two part answer coming up.

Part 1: why this isn't working
The issue lies with your link tag:
<a href="doc_directory/doc_file.pdf" onclick="doc_download_alert.cfm";>Click here to download document</a>

What you're essentially trying to do is make a single click do two things. That won't work unless the click goes to a Javascript function that does those two things for you. Clicking on an anchor link IS the onclick event and in the example of your code, it sends the user to your PDF file. The onclick event handler never fires which is why your email process never runs.

That said, your problem (as is the case in many situations) can be solved in more than one way. I'll present one way but there are others equally as worthy.

Part 2: one solution
I'll preface this by saying that here at DIC, we tend to offer guidance rather than a code solution for you. This can vary depending upon how you ask the question, whether you've tried something yourself, etc. Since you're a novice, I'd like to guide you to a solution rather than give it to you outright because this is a handy learning opportunity.

So, my suggested solution involves you using the anchor link to direct the user to a page that does the email processing as well as prompting them to download the PDF. In addition, you can pass the document name they want to download to this new page so that your email can not only tell you they downloaded a document but which one it was they downloaded (could be handy to know). Incidentally, in much the same way as you can know which document they selected to download so you can email yourself an alert, you could also run a database insert to add that document identifier to a table along with the date and time and, if you're tracking such, even the user's ID and other pertinent info (but we won't go into that...just know that it's an option).

So, consider this for your link to send them to this document download page:
<cfset variables.docToDownload = "doc_file.pdf">
<a href="doc_download_alert.cfm?doc=#variables.docToDownload#">Click here to download document</a>

So, what we've done here is make the document they're wanting to download a variable that you pass to the doc_download_alert.cfm page as a URL variable. So, on the doc_download_alert.cfm page you'd need to handle the incoming URL variable, email yourself the alert and prompt them to download the document:
<cfparam name="url.doc" default="">
<cfif Trim(url.doc) EQ "">
   <!--- They somehow got here without a document name to download.  This is where you handle that situation (ie. pop up a message telling them they lack a document and then send them back) --->
   <cfabort>
</cfif>
<!doctype html public "-//W3C//dtd html 3.2 Final//EN">
<html>
<head>
</head>
<body>

<cfprocessingdirective suppresswhitespace="no">
<cfmail
type="html"
from="my website"						
to="my_email_address@email.com"
subject="download alert">
<p>ALERT: document (#url.doc#) downloaded from my page
</cfmail>
</cfprocessingdirective>

<!--- Code to prompt user to download the document goes here --->

</body>
</html>


So, what you're seeing here is the altered .cfm to handle the incoming document name (in the form of URL.doc). This is only a slight change from what you had in that it sets the default variable you're expecting (the CFPARAM part), does a check to make sure something got passed in (the CFIF, Trim() and CFABORT parts) and then it included the file requested in your email (the #url.doc# part in the email body).

That comprises the notice to you. The last part goes in the comment area to prompt the user to download the document. I'll direct you to an example of how to do that here. You'll want to familiarize yourself with the following tags though as part of that:
CFHEADER
CFCONTENT

Anyway, that's it. A small redesign of the workflow and a familiarity with some CF tags and functions gets you to where you want to be.

Good luck!

This post has been edited by Craig328: 07 October 2014 - 09:48 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1