How to send email from model

53 views
Skip to first unread message

Cathy Shapiro

unread,
Sep 13, 2010, 11:53:43 AM9/13/10
to ColdFusion on Wheels
Hey you guys,

I saw a previous discussion on the group that said you can't send an
email from the model.
I get an error when I try to use sendEmail() from a function inside
the model.

In the documentation for Object Callbacks though, it mentions
something about sending an email from the model.

So does anyone know how I can send an email from the model?

thanks,
Cathy

raulriera

unread,
Sep 13, 2010, 3:52:32 PM9/13/10
to ColdFusion on Wheels
Hey Cathy,

None of the controller specific functions are available in the model,
you should place that sendMail() in your controller for now :(

Cathy Shapiro

unread,
Sep 13, 2010, 5:12:41 PM9/13/10
to cfwh...@googlegroups.com
Hey Raul,

Thanks... am trying that with no luck. Maybe I'm just too tired, and it seems really simple... but I'm missing it!
Here's what I'm trying:

In model, I'm doing a callback in the init method:
<cfset beforeCreate("checkdates")>

Then in the checkdates function in the model I'm trying to send the email from the controller:
<cffunction name="checkdates">   
<cfset this.reconfirmemail()>
</cffunction>

Then in my controller, I have
<cffunction name="reconfirmemail" >
    <cfset sendEmail(to="ca...@flashprodesign.com",from="m...@me.com",
        subject="Reconfirmation Date Change",template="datechangeemail" )>
    </cffunction>

But I get an error that checkdates is not found in the model.
How do I call something in the controller from the model?


thanks,
Cathy

--
You received this message because you are subscribed to the Google Groups "ColdFusion on Wheels" group.
To post to this group, send email to cfwh...@googlegroups.com.
To unsubscribe from this group, send email to cfwheels+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cfwheels?hl=en.




--
Flash Pro Design
4646 Poplar, Suite 517
Memphis, TN 38117

Phone: (901) 767-8767
Fax: (901) 685-9054

http://www.flashprodesign.com

Per Djurner

unread,
Sep 13, 2010, 5:20:35 PM9/13/10
to cfwh...@googlegroups.com
You can't / shouldn't call the controller from the model.
For now you simply have to recode your app so that the controller is
responsible for sending the email (on its own).

Per Djurner

unread,
Sep 13, 2010, 5:21:02 PM9/13/10
to cfwh...@googlegroups.com
The documentation is incorrect by the way.
We'll get that changed.

Per Djurner

unread,
Sep 13, 2010, 5:23:49 PM9/13/10
to cfwh...@googlegroups.com
One more suggestion for you :)

What you can do is schedule the email to be sent from the callback
instead of actually sending it.
Just put it in a temporary table or something and set up a scheduled
task that sends emails every minute.

Cathy Shapiro

unread,
Sep 13, 2010, 6:46:31 PM9/13/10
to cfwh...@googlegroups.com
Thanks Per...
I guess I really don't understand the MVC architecture well enough and should read more about it.
Shamefully, I did try using a <cfmail> tag in the model and that works.  ;)

Per Djurner

unread,
Sep 14, 2010, 2:24:15 AM9/14/10
to cfwh...@googlegroups.com
Oh, the shame!!
;)

Andy Bellenie

unread,
Sep 14, 2010, 7:31:48 AM9/14/10
to cfwh...@googlegroups.com
Why isn't sendMail() a global function?

Per Djurner

unread,
Sep 14, 2010, 7:43:38 AM9/14/10
to cfwh...@googlegroups.com
Because an email is just a different type of view and should therefore
be handled by the controller.
We could make it a global function anyway though but that would mean a
lot of the underlying functions would have to be made global as well
(functions related to including view templates etc).
This would "pollute" the model with a bunch of (mostly) unrelated functions.

Cathy Shapiro

unread,
Sep 14, 2010, 11:13:54 AM9/14/10
to cfwh...@googlegroups.com
Hey Per,

Searching around online, it looks like you can send an email from the model in Rails.
http://www.robbyonrails.com/articles/2009/11/16/sending-email-controllers-versus-models
It also looks like this is one of those issues that everyone feels very strongly for or against. (see the comments on his blog entry)

I'm not sure that I agree that an email is a view. But I do think programming is sometimes more an art than a science and is therefore open to lots of interpretation.
I see there is an open ticket on it: http://code.google.com/p/cfwheels/issues/detail?id=394&q=email&colspec=ID%20Type%20Status%20Severity%20Milestone%20Owner%20Summary
 
What about this being a plugin? Would love to hear what others think about it. ;)
Cathy
Reply all
Reply to author
Forward
0 new messages