Can you perform DML operations from visualforce email templates?

I don't think you can use DML in this way. I was able to get some code with DML in it to fire by adding an attribute to the component and executing DML in the setter. However, this results in the following error:

Error occurred trying to load the template for preview: common.apex.runtime.impl.ExecutionException: Attempt to de-reference a null object. Please try editing your markup to correct the problem.

so it looks like there's something about the context that doesn't allow the DML to execute correctly.


Here is another answer. Not sure if this will work for you.

  1. Create an email service

  2. Add your email address

  3. You can send an email using Sendsingleemailmessage to the email address that you have added in the email service from your component controller's constructor. For the email message body, do a json.serialize of your object and assign it to Plaintextbody or HTMLbody and send it.

  4. Write a class to processes the email using Messaging.inboundemailhandler interface

  5. Perform a DML operation inside that method after deserializing the string to your object

Let me know if this worked.