tags:

views:

116

answers:

2

I have a variety of Velocity template files that represent e-mail messages. I am searching for an elegant way to allow the template file to output a subject line for the message as well as the contents. I have considered a few options:

  1. Make the first line of the output something like "Subject: Bla" and strip it out in the Java. I have rejected this approach because performing fancy string manipulation from Java is exactly the problem I'm trying to solve by using Velocity in the first place.

  2. Throw a settable object into the mapping for some code in the Velocity macro to call a setter on. Pros: Pretty simple. Cons: I dislike the idea of setting external objects from Velocity, even as just a form of output. It might be an unreasonable dislike, though.

  3. Create some custom Velocity command, like #setSubjectLine. Pros: More elegant. Cons: Would apply as a valid command to all of my templates, regardless of whether they're e-mails or not.

  4. Have separate template files for the subject line and the rest of the email. Pro: No special tricks at all! Con: Not all email logic lives in the same place.

  5. Some cool Velocity trick I don't know.

  6. ???

So what should I do? Do you have a suggestion for 5 or 6? A reason I should choose 1, 2, 3, or 4?

+1  A: 

At $work we do 4. It works fine, and the argument against not all email logic living in the same place seems a bit too strict. You would just have two velocity files next to each other. And even with just one template file, you still have logic to populate the data, determine the recipients, attach the attachments, and so on, which also does not live in the Velocity file.


I would prefer 2 over 3, just because I do not like Velocity macros. Using Java objects seems more flexible.

$mailContext.setSubjectLine($subject)

With either 2 or 3, you can get closer to your goal of having everything in one place: You could get Velocity to set the recipient list, too, for example. This, one the other hand, seems to conflict with the idea of Velocity being a pure templating technology.

A thing to consider here is who is writing the Velocity templates. The closer it gets to "end-users", the less you want to trouble them with (and keep them from messing with) anything that is not directly related to creating the email content.


Not sure if that is feasible, makes sense or is even possible with the Java Mail API, but just for completeness:

 6. Have velocity create the whole email message including headers, not just content and subject. There difference to 1 is that you do not need to post-process this output, it can go straight to the mail transport agent. Probably does not work with attachments.

To: $to
CC: $cc
Subject: $subject

Here comes the content.
Thilo
+2  A: 

2) Throw a settable object into the mapping for some code in the Velocity macro to call a setter on. Pros: Pretty simple. Cons: I dislike the idea of setting external objects from Velocity, even as just a form of output. It might be an unreasonable dislike, though.

This was my approach, and I am pleased with it despite my own similar misgivings at first. I even set character encoding and other attributes this way. This leaves the rest of the template as the message body. Again, I am pleased.

#set ( $myAtts.encoding = "utf-8" )
#set ( $myAtts.from = "[email protected]" )
Stu Thompson
I like that this keeps everything for an email in one place.
Will Glass