Is there a way to intercept calls to a Play Mailer class?
I would like to log all emails my application sends. I’d like to access the mailer method parameters and method name, and log all these.
I have tried using @With, but my @Before method is not called:
@With(EmailInterceptor.class)
public class Mails extends Mailer {
public static void welcome(User user) {
setSubject("Welcome to my app");
setFrom("Me <me@domain.com>");
addRecipient(user.email);
Mailer.send(user);
}
}
public final class EmailInterceptor extends UserAwareControllerBase {
private final static Logger logger = LoggerHelper.getLogger();
@Before
public void logEmail(){
String email = getUser() == null ? "<null>" : getUser().email;
logger.info("Sending email to " + email);
}
}
AFAIK there is no intercepts for Mailer. Check the https://github.com/playframework/play/blob/master/framework/src/play/mvc/ActionInvoker.java. Go to line 142, that clearly indicate only Controller will get intercepts.