Search code examples
emailcoldfusioncoldfusion-11

Deleting email addresses where email wasn't sent


I personally can't see a way to do this, but I'm hoping there could be a clever hack to make it work.

We have to send out regular emails to customers, many of who's emails expire and give us a mail delivery failure. To keep the database accurate and clean, I'd like to delete their record from the database when an email fails to be sent to them.

So currently it goes a little something like this:

<cfloop query="rsCustomers">
<cftry>

<cfmail to="rsCustomers.Email" from="us@ourcompany.com" failto="fails@ourcompany.com" subject="Whatever">
[email text here]
</cfmail>

<cfcatch type="any">
<!--- just to skip this record when it fails to send --->
</cfcatch>
</cftry>

</cfloop>

I could run a query within the catch area to delete records when there was an error in the record (e.g. malformed email address). But nothing happens when the mail is undeliverable. I get a failure notice to my inbox but these can be in the hundreds so its difficult to manually go through and delete from the database.

Is there anyway to "catch" the failed to send emails and then feed them back in to a query to delete from the database?


Solution

  • Someone once did a presentation on this at a function I attended. The approach was:

    • when sending mail, use a special email address in the failto attribute.
    • write a .cfm file that uses the cfpop tag to read mail sent to this address. (This may have been before the cfexchange tag was available)
    • add code that reads Delivery Failure Notfications and extracts the email addresses.
    • do something about those addresses

    The presenter ran this file as a scheduled job.