I'm using cfmail tag to send out emails to the user. Additional thing I need to fulfill is to delete the records after sending them to the users. Is there any solution available to observe the cfmail task is whether completed or not. For example like a callback to indicate that the task was done.
-
I'm not sure I understand what you're trying to delete. Are you looping over a query for email data? – TomDillinger Aug 11 '15 at 05:21
-
Yes, I'm looping over a query for email data.what I'm trying to delete after that is the schedule data to send out emails. – zawhtut Aug 11 '15 at 05:24
-
To verify that mail has been sent, you can read your mailsent log. – Dan Bracuk Aug 11 '15 at 09:33
2 Answers
With emails there are no guarantees that the email has been delivered.
If the cfmail tag executed without error there will be a record in the cfml spool directory. After the server hands the mail over to your SMTP server coldfusion removes the email from the spool directory.
NOTE: If your server is fast you will never see the mail in the spool folder.
You could try and build a solution that inspects the mail logs, but even that gives you no guarantee that the email has been delivered.
In most cases knowing that cfmail executed is enough.

- 921
- 7
- 9
<cfquery name="emailStuff">
SELECT * FROM mytable
</cfquery>
<cfloop query="emailStuff">
<cfmail>
cfmail contents/properties
</cfmail>
<cfquery>
DELETE FROM mytable WHERE ID=emailStuff.ID
</cfquery>
</cfloop>
Not sure if the above is what you're looking for. But this is a rough example of some code that will delete your DB entry after the mail has been sent.
Also cfmail has properties for failto
and debug
.
If an email fails it will send the failure email to whomever you specify in failto
, or you can use debug
to log failed emails.
see: cfmail docs
UPDATE: You could also move the delete query to outside the loop so that it only needs to delete from the database in one swift motion.
<cfloop query="emailStuff">
<cfmail>
cfmail contents/properties
</cfmail>
</cfloop>
<cfquery>
DELETE FROM mytable
WHERE ID IN (<cfqueryparam list="yes" value="#emailStuff.ID#" />)
</cfquery>

- 194
- 7
-
-
Look for a way to run only one delete query instead of having it in a loop. – Dan Bracuk Aug 11 '15 at 11:50
-
I had to use cflock with timeout to control the race condition occurred. It was solved and thanks everyone. – zawhtut Aug 11 '15 at 12:29