Main Content

Magnolia Community Forums: Get help with Magnolia: Use Magnolia Mail Module in Custom Form


  • jmende
    jmende
    Full name: Jonas Mende
    Posts: 4
    Last post: Feb 7, 2014 1:44:30 PM
    Registered on: Feb 1, 2013
    Use Magnolia Mail Module in Custom Form
    #1 by jmende on Feb 1, 2013 2:59:16 PM

    Hi,

    I am currently working on a project using Magnolia CE. I do not use any STK Features.
    I created a simple Form and would like to know how to make use of the Magnolia Mail module in order to submit form entries via Mail.
    I know that there is a Form Module as well, but since I do not use STK and this module depends on STK, I would rather like to know how to use the mail module itself. Does anybody know how to achieve this goal?

    Many thanks in advance,

    Jonas

  • will
    will
    Full name: Will Scheidegger
    Posts: 424
    Last post: May 31, 2017 11:30:01 AM
    Re: Use Magnolia Mail Module in Custom Form
    #2 by will on Feb 1, 2013 3:02:20 PM

    I _think_ you can use the form paragraph of the form module without having stk configured. Maybe you'll need to have STK installed, but that's not hurting anyone.

    -will

  • runger
    runger
    Full name: Richard Unger
    Posts: 571
    Last post: Feb 1, 2017 9:54:59 AM
    Re: Use Magnolia Mail Module in Custom Form
    #3 by runger on Feb 1, 2013 3:07:13 PM

    Hello Jonas,

    Create a mail Template and Template Definition. You can look at some of the existing examples under config->modules->mail.

    Create a model class for the page or component containing your form. In the model-class, implement the execute() method to check your form parameters, and if the form was submitted correctly, send the mail.

    The code for this could look something like this (in Magnolia 4.4, 4.5 shouldn't be too different):

    public void sendEmail() throws Exception{
    String recipient = NodeDataUtil.getString(getContent(),"orderEmail",null);
    if (StringUtils.isEmpty(recipient))
    throw new IllegalArgumentException("Film Bestellen: recipient is not set in the publication. Set the recipient to send emails for this form.");
    String subject = NodeDataUtil.getString(getContent(),"orderEmailSubject",null);
    MgnlMailFactory factory = MailModule.getInstance().getFactory();
    Map<String, Object> params = new HashMap<String,Object>();
    params.put("formModel", this);
    if (!StringUtils.isEmpty(subject))
    params.put("orderEmailSubject", subject);
    params.put("film", content.getParent().getTitle());
    MgnlEmail mail = factory.getEmailFromTemplate("filmBestellen", params);
    if (!StringUtils.isEmpty(subject))
    mail.setSubject(subject);
    mail.setRecipient(RecipientType.TO, new InternetAddress(recipient));
    InternetAddress[] addr = new InternetAddress[1];
    addr[0] = new InternetAddress(getForm_email());
    mail.setReplyTo(addr);
    mail.setBodyFromResourceFile();
    factory.getEmailHandler().sendMail(mail);
    }

    Regards from Vienna,

    Richard

    -----Ursprüngliche Nachricht-----
    Von: user-list-owner@magnolia-cms.com [mailto:user-list-owner@magnolia-cms.com] Im Auftrag von Jonas Mende (via Magnolia Forums)
    Gesendet: Freitag, 01. Februar 2013 14:59
    An: Magnolia User List
    Betreff: [magnolia-user] Use Magnolia Mail Module in Custom Form

    Hi,

    I am currently working on a project using Magnolia CE. I do not use any STK Features.
    I created a simple Form and would like to know how to make use of the Magnolia Mail module in order to submit form entries via Mail.
    I know that there is a Form Module as well, but since I do not use STK and this module depends on STK, I would rather like to know how to use the mail module itself. Does anybody know how to achieve this goal?

    Many thanks in advance,

    Jonas

  • jmende
    jmende
    Full name: Jonas Mende
    Posts: 4
    Last post: Feb 7, 2014 1:44:30 PM
    Registered on: Feb 1, 2013
    Re: Use Magnolia Mail Module in Custom Form
    #4 by jmende on Feb 4, 2013 9:31:22 AM

    At first, thanks for the fast and detailed replies.
    I configured a template and implemented a model class. But how do I access Form parameters within a model class in order to validate and process them?

    Thanks again,


    Jonas

  • runger
    runger
    Full name: Richard Unger
    Posts: 571
    Last post: Feb 1, 2017 9:54:59 AM
    Re: Use Magnolia Mail Module in Custom Form
    #5 by runger on Feb 4, 2013 2:28:25 PM

    Hi Jonas,

    To get your form-parameters, there are 2 ways:

    1) Within your model-class you can access the request via the MgnlContext object. You can then read the parameters in the normal way ( request.getParameter() ).
    2) Create Bean-style setter methods in your model class. For example, if your Form-Field is called "first_name", write a setter method called "setFirst_name()". When magnolia finds a name-correspondence between your model's setter methods and a request parameter, magnolia will automatically call the setter method with the parameter value. This happens after your model-class constructor is called, but before the execute() method is called on your model.

    So one way to handle a form might be to write a setter and getter for each form field, and then to validate the values in your model's execute() method.

    Regards from Vienna,

    Richard

    -----Ursprüngliche Nachricht-----
    Von: user-list-owner@magnolia-cms.com [mailto:user-list-owner@magnolia-cms.com] Im Auftrag von Jonas Mende (via Magnolia Forums)
    Gesendet: Montag, 04. Februar 2013 09:31
    An: Magnolia User List
    Betreff: [magnolia-user] Re: Use Magnolia Mail Module in Custom Form

    At first, thanks for the fast and detailed replies.
    I configured a template and implemented a model class. But how do I access Form parameters within a model class in order to validate and process them?

    Thanks again,

    Jonas

  • ing-glgonza
    ing-glgonza
    Full name: Gerardo Gonzalez
    Posts: 1
    Last post: Apr 20, 2017 11:09:04 PM
    Registered on: Apr 20, 2017
    Re: Use Magnolia Mail Module in Custom Form
    #6 by ing-glgonza on Apr 20, 2017 11:09:04 PM

    That such friends, I am trying to use mailModule but it still has not worked for me. Already create a template as Richard says, but I have not yet been able to create a Template Definition. To create the next class as a magnolia filter but does not work


    package com.brakepartsinc.project.techportal.mail;

    import info.magnolia.cms.filters.AbstractMgnlFilter;
    import info.magnolia.context.Context;
    import info.magnolia.context.MgnlContext;
    import info.magnolia.module.mail.MailModule;
    import info.magnolia.module.mail.MgnlMailFactory;
    import info.magnolia.module.mail.handlers.MgnlMailHandler;
    import info.magnolia.module.mail.templates.MgnlEmail;

    import javax.servlet.FilterChain;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.util.Map;


    /**
    * Created by Gerardo on 19/04/2017.
    */
    public class MailFilter extends AbstractMgnlFilter{


    Map<String, String> parameters = null;

    final Context context = MgnlContext.getInstance();

    final MgnlMailFactory factory = MailModule.getInstance().getFactory();

    final MgnlMailHandler handler = factory.getEmailHandler();

    public static final String PARAMETER_MAIL = "mgnlSendMail";


    @Override
    public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
    throws IOException, ServletException {

    System.out.println("this is a test of MailFilter");
    parameters.put("user", "gerardo");
    parameters.put("path", "/local/path");
    String bpiMail = request.getParameter(PARAMETER_MAIL);


    if (bpiMail != "") {
    System.out.println("this is a test of MailFilter");
    final MgnlEmail email;//parameters);
    try {
    email = factory.getEmailFromTemplate("simpleTemplate", null);
    //email.setToList(to);
    email.setBodyFromResourceFile();
    MgnlContext.setInstance(context);
    handler.prepareAndSendMail(email);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    System.out.println("this is a test of MailFilter");
    chain.doFilter(request, response);
    }
    }

You don't have the permission to post on this thread

Sign in

To login on this forum, you can use your Magnolia Forge, Support or Partner account, or, below, your Google, Yahoo! or OpenID account. If you have trouble logging in, or any other sort of issue, please let us know in the Meta forum, on the user-list, or simply by email at forum-admin at magnolia-cms dot com.

* Required

... or sign in with:

  • icon http://{your-openid-url}
  • icon
  • icon https://me.yahoo.com/