logo

MMBase tips

Sending mail

tip #5 / Wed 25 Nov 2009 / André / 6 comments

The mmbase email application enables you to send email with for example a contactform or you can even build a mailinglist with it. You can save the mails after they have been send, or discard them.

After you have installed the 'mmbase-email.jar' in your webapp's '/WEB-INF/lib/' directory, you need to configure Apache Tomcat or another prefered webapplication server. Following the configuration instructions in the README with the application this basically boils down to enabling a mail Resource for your webapplication's context. For example:

<Context path="" docBase="ROOT" debug="0">
<!-- there is normally more stuff here about your webapp -->
<Resource name="mail/Session" auth="Container"
type="javax.mail.Session"
mail.smtp.host="smtp.someplace.org" /> </Context>

After that sending mail is quite easy.

<%-- pagelogin to enable visitors to create email nodes --%>
<mm:cloud username="foo" password="bar" method="pagelogon">

<%-- create an email node --%>
<mm:createnode id="new_email_node" type="email">
<mm:setfield name="from">you@someplace.org</mm:setfield>;
<mm:setfield name="replyto">you@someplace.org</mm:setfield>;
<mm:setfield name="to">me@somewhere.net</mm:setfield>;
<mm:setfield name="subject">Just saying hi</mm:setfield>
<mm:setfield name="body">Hi there! How are you.</mm:setfield>
</mm:createnode>

<%-- the function 'mail' shoots it off into the world --%>
<mm:node referid="new_email_node">
<mm:functioncontainer>
<mm:param name="type">oneshotkeep</mm:param>
<mm:function name="mail" />
</mm:functioncontainer>
</mm:node>

<%-- let the visitor know what happened with it --%>
<mm:node referid="mailtje">
<mm:field name="mailstatus">
<mm:compare value="1">
<h2>Thanks. Your mail has been send.</h4>
</mm:compare>
<mm:compare value="2">
<h2>Some error occured!</h4>
</mm:compare>
</mm:field>
</mm:node>
</mm:cloud>
The 'type' function parameter is 'oneshotkeep' which sends email and stores it as an email node in mmbase. If you don't want to start collecting email from your site you can use the nodefunction with the parameter 'type' at 'oneshot'.
<mm:node referid="new_email_node">
<mm:functioncontainer>
<mm:param name="type">oneshot</mm:param>
<mm:function name="mail" />
</mm:functioncontainer>
</mm:node>

That's it. Other features of the email application include sending multipart email like mail with attachments or html.

6 comments

Hi André,
Really like the tips and the description you give with them.
I am using the stuff you described above.
But after a test I get &quot;Operation 'write' on 1395810 was NOT permitted to anonymous&quot;.
Why does this happen? Is this because the email node is stored by anonymous? I don't really understand what you do in the uppermost part. In which you add some stuff to the Tomcat Configuration.
Can you help me with this. Or is it just a case of adding more rights to anonymous?
regards Aschwin

di 18 mei 2010 / aschwin versteegden

You could give anonymous more rights, but it would perhaps be safer to only give the page more rights.

So, don't use <mm:cloud>

but
<mm:cloud method="delegate" authenticate="class">

I think a default configuration would provide an administrator cloud then. Otherwise it can be configured in config/security/classauthentication.xml.

di 18 mei 2010 / mihxil → http://www.meeuw.org

Thanks a lot. Easiest for me is to just to use the pagelogon attribute on the <mm:cloud /> tag. Like:

<mm:cloud username="foo" password="bar" method="pagelogon"> ... </mm:cloud>

User 'foo' (or another basic user) is in a default mmbase setup normally allowed to create nodes in the body of the cloud tag in this example.

And Tomcat needs to know where to deliver mail. You would probably also need the JavaMail API. More information is in on Tomcat's website:
http://tomcat.apache.org/tomcat-5.5-doc/jndi-resources-howto.html

di 18 mei 2010 / André → http://www.toly.nl

quote:
"And Tomcat needs to know where to deliver mail. You would probably also need the JavaMail API. More information is in on Tomcat's website:
http://tomcat.apache.org/tomcat-5.5-doc/jndi-resources-howto.html"

Couldn't you use sendmail.xml to set the mail properties? I have no access to Tomcat installation?
Nice to see your reactions......:-)

di 18 mei 2010 / aschwin versteegden

Thanks again and I believe you're right. You probably can still use the configuration of the email application in 'config/modules/sendmail.xml' in stead of JNDI Resources, but I haven't used it in a while. See the commented parts in http://scm.mmbase.org/view/mmbase/trunk/applications/email/src/main/config/modules/sendmail.xml?view=markup

di 18 mei 2010 / André → http://www.toly.nl

finally.....
I modified the sendmail.xml (WEB-INF\config\modules\)
looks a bit like this :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC "-//MMBase//DTD module config 1.0//EN" "http://www.mmbase.org/dtd/module_1_0.dtd">;
<module maintainer="mmbase.org" version="0">
<status>active</status>
<classfile>org.mmbase.applications.email.SendMail</classfile>
<properties>
<property name="mailhost">mailservxxx.xx.nl</property>
</properties>
</module>
Then in the jsp I used "<mm:cloud username="foo" password="bar" method="pagelogon">" and after that it worked like a breeze .
I will keep visiting your site. I found it trough Twitter, although I think Twitter is overrated :-)
thx again

di 18 mei 2010 / aschwin versteegden

RSS feed

Tweet this article

Most mentioned links:

MMBase documentation
MMBase API
MMBase SVN
Bugtracker
MMBase Taglib reference
The reference for all tags mm