Custom email template for EmailFactory.sendParameterizedEmail

43 views
Skip to first unread message

Djacomo

unread,
Jul 26, 2017, 4:35:22 AM7/26/17
to dotCMS User Group
Hi all,
i'm trying to create a custom template for an email to send via EmailFactory.sendParameterizedEmail() API .

The only way it works is add a row in dotmarketing-config.properties near to "ACTIVATION_LINK_EMAIL_TEMPLATE=static/user_account/activation_link_email.html ", like "MY_TEMPLATE=static/user_account/news.html" but is very hard to handle change request (i must stop and restart the tomcat).

Reading documentation, it seems that there are a way to indicate a VTL/HTML page, like /email/news.vtl , but i get an send email error, ""Unable to send the email".

my code is the follow:

Host host = hostWebAPI.getCurrentHost(request);

User user = APILocator.getUserAPI().getSystemUser();

...
HashMap<String, Object> parameters = new HashMap<>();

...
parameters
.put("text", "text to insert");
parameters
.put("emailTemplate", "/email/news.vtl");
...
EmailFactory.sendParameterizedEmail(parameters, null, host, user);


From EmailFactory source code, i guess the problem is on:

 
if(UtilMethods.isSet(templatePath)) {
            idInode
= APILocator.getIdentifierAPI().find(host, templatePath).getInode();
           
Template t = null;


           
try {
               
if(InodeUtils.isSet(idInode)) {
                    t
= UtilMethods.getVelocityTemplate("live/" + idInode + "." + Config.getStringProperty("VELOCITY_HTMLPAGE_EXTENSION"));
               
} else {
                    t
= UtilMethods.getVelocityTemplate(templatePath);
               
}
           
} catch (Exception var19) {
               
;
           
}

Using same code idInode = APILocator.getIdentifierAPI().find(host, templatePath).getInode();

"t" value is "live/70909....09123.dotpage    (70909....09123 is the /email/news.vtl's inode)


/email/news.vtl code is

MY EMAIL TEMPLATE $text WORKS!!!




Anyone can help me!!!  There is another way to send a custom email?

Nathan Keiter

unread,
Jul 26, 2017, 9:18:35 AM7/26/17
to dot...@googlegroups.com

Djacomo,

 

I think I remember that not working for me as well.  The odd thing is my own code is basically doing the same thing as the dotCMS code, but mine works.  Since you are already coding Java.  Write your own email method to utilize the template:

 

 

If you don’t have a request context available, you can get a basic context:

 

 

Nathan I. Keiter | Lead Network Applications Programmer | Benefits Advisory Council Member | I.D.E.A Council Member
Gettysburg College | Information Technology | DataSystems
Campus Box 2453 | 300 North Washington Street | Gettysburg, PA 17325
Phone: 717.337.6993

https://www.gettysburg.edu

--
http://dotcms.com - Open Source Java Content Management
---
You received this message because you are subscribed to the Google Groups "dotCMS User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dotcms+un...@googlegroups.com.
To post to this group, send email to dot...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dotcms/a2d29b2b-1e25-4106-8cba-254a549b1fd5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Djacomo

unread,
Jul 26, 2017, 9:35:14 AM7/26/17
to dotCMS User Group
Thank you Nathan.

I'll try and let you know!

For curiosity, what type of TEMPLATE did you use? ACCOUNT_ACTIVATION_EMAIL_PATH, is a path to a VTL file?

Djacomo

Nathan Keiter

unread,
Jul 26, 2017, 9:48:11 AM7/26/17
to dot...@googlegroups.com

It is a page object.  Of course velocity content of some sort is included on the page either directly as content or via a widget.

 

Any variables you want to have accessible to that page must be added to the velocity context manually via Java. You add them in key/object pairs, where the key becomes the velocity variable name with a $ in front.  They are then accessible to velocity for the merge. During the merge the variables are replaced with the objects and any velocity scripting is run before returning the “rendered page” result string.

 

So velocityContext.put( “someJavaObject”, myJavaObject );

 

Is then available in velocity as $someJavaObject etc.

 

#if ( $someJavaObject.getSomeValue() == true )##

            <b>$someJavaObject.getSomePropertyValue()</b>

#end##

Djacomo

unread,
Jul 26, 2017, 10:04:10 AM7/26/17
to dotCMS User Group
this code: 

            VelocityEngine velocityEngine = new VelocityEngine();
            velocityEngine
.init();


           
String inode = identifierAPI.find(host, "/email/email.html").getInode();


           
String languageId = "_" + APILocator.getLanguageAPI().getDefaultLanguage().getId();


            log
.info("Languageid " + languageId);


           
String pageExtension = Config.getStringProperty("VELOCITY_HTMLPAGE_EXTENTION", "dotpage");


           
String liveUrl = "live/" + inode + "." + pageExtension; // REMOVED languageid for "NumberFormatException"


            log
.info("Liveurl " +liveUrl);


           
Template template = UtilMethods.getVelocityTemplate(liveUrl);


            log
.info("Template " + template.getName());


           
Context velocityContext = VelocityUtil.getBasicContext();
            log
.info("Contesto " +velocityContext);
//            velocityContext.put("host", host);
//            velocityContext.put("user", user);


           
StringWriter stringWriter = new StringWriter();
           
template.merge(velocityContext, stringWriter);


           
String htmlBody = stringWriter.toString();


            log
.trace("HTML BODY " + htmlBody );


get this error:

HTML BODY $velutil.mergeTemplate('live/1a0d6e9c-b7a3-4593-8211-53b39660324f.host')    $velutil.mergeTemplate("$dotTheme.templatePath")

???

Nathan Keiter

unread,
Jul 26, 2017, 10:04:49 AM7/26/17
to dot...@googlegroups.com

Included on that template page is a Simple Widget that does a #dotParse( ‘/some/path/someFile.vtl’ ) of a velocity file.  I believe this keeps things better organized.  Also as a plus you can easily upload changes via WebDAV if you put your velocity in as files in the virtual file system tree instead of obfuscating them as content objects - which forces edits to be done in the back-end UI and makes things harder to find in my opinion.

 

Also of note, the page uses a “blank template”, meaning the template source is just $body, nothing else.

 

You can see in the velocity widget source how I am accessing the objects I added to context, “host” and “user”.

 

 

Nathan I. Keiter | Lead Network Applications Programmer | Benefits Advisory Council Member | I.D.E.A Council Member
Gettysburg College | Information Technology | DataSystems
Campus Box 2453 | 300 North Washington Street | Gettysburg, PA 17325
Phone: 717.337.6993

https://www.gettysburg.edu

 

Nathan Keiter

unread,
Jul 26, 2017, 10:09:46 AM7/26/17
to dot...@googlegroups.com

Yeah, I forgot to mention language id is only required for dotCMS 3.x +

 

As for your error, can you provide more information?

 

Often a merge error is the result of a velocity code error.

Nathan Keiter

unread,
Jul 26, 2017, 10:14:38 AM7/26/17
to dot...@googlegroups.com

As a general rule, work your way up from simple to complex until you encounter an error.  Then the cause will become apparent.  So maybe start with a plain text template with no velocity on it?  Also I’m not sure what if any permissions requirements there are.  I don’t think there are any, but I’m not sure.  The template page should at least be published.

 

Nathan I. Keiter | Lead Network Applications Programmer | Benefits Advisory Council Member | I.D.E.A Council Member
Gettysburg College | Information Technology | DataSystems
Campus Box 2453 | 300 North Washington Street | Gettysburg, PA 17325
Phone: 717.337.6993

https://www.gettysburg.edu

 

From: dot...@googlegroups.com [mailto:dot...@googlegroups.com] On Behalf Of Nathan Keiter


Sent: Wednesday, July 26, 2017 10:10 AM
To: dot...@googlegroups.com

Djacomo

unread,
Jul 26, 2017, 10:33:44 AM7/26/17
to dotCMS User Group
My projcet structure:

1) /application/themes/email-template/template.vtl


##IMPORTANT
#parse ("preprocess.vl")
##IMPORTANT


<!DOCTYPE html>
<html lang="en">
<!--<![endif]-->


<head>


</head>


<body>
<h1> EMAIL THEME </
h1>
<div>
   
#set ($mainColumn = $dotThemeLayout.body)
   
#if ($mainColumn.rows)
       
#set($count = 0)
       
#foreach($row in $mainColumn.rows)
           
#foreach($column in $row.columns)
               
<div class="col-sm-12">
                    $render
.eval($column.draw())
               
</div>
            #end
        #end
    #end
</
div>


</body>
</
html>


##IMPORTANT
#parse ("postprocess.vl")
##IMPORTANT



2) Created a "Emai Template" with this "email theme" theme and a "Large column - 1" container

3) Create a HTML PAGE /email/email.html and assigner the "Email Template"

4) Added "VTL Include" /email/template.vtl to "/email/email.html" 

In /email/template.vll

HELLO WORKD


Where is my mistakes?

Thx

Nathan Keiter

unread,
Jul 26, 2017, 10:38:39 AM7/26/17
to dot...@googlegroups.com

Did you add $dotThemeLayout  to the context in Java?  If not that object will be null.  I’m not sure where to fetch that from, perhaps a dotCMS API call in Java?

Nathan Keiter

unread,
Jul 26, 2017, 10:40:40 AM7/26/17
to dot...@googlegroups.com

Also, $render may need to be added to context. If you don’t explicitly add an object to context, it won’t be there.

Djacomo

unread,
Jul 26, 2017, 10:46:59 AM7/26/17
to dotCMS User Group
i can remove dotThemeLayout ???

and $render too?

That theme is my common theme without css and other things...

i suppose it's a wrong theme.

Djacomo

Nathan Keiter

unread,
Jul 26, 2017, 10:53:38 AM7/26/17
to dot...@googlegroups.com

You’ll need to build a basic template from scratch.  Don’t use template designer.

 

Djacomo

unread,
Jul 26, 2017, 12:57:44 PM7/26/17
to dotCMS User Group
it's works...and with a my custom theme!!!

In the next days i'll paste the snippets!!!

Thank you very much Nathan!!!

Djacomo

Nathan Keiter

unread,
Jul 26, 2017, 1:38:38 PM7/26/17
to dot...@googlegroups.com

No problem.  Glad I could assist you.

Reply all
Reply to author
Forward
0 new messages