Is there a way to store all emails sent?

On my WordPress sites I have a small plugin that logs all emails sent by the system and stores them as a custom post type for later reference. This is especially useful for contact form submissions, to check they have been received and seen, and to refer back to.

Does anyone know if there is anything similar that has been done with Grav? I have tried turning on the logging option in the email plugin but that doesn’t store the actual content.

@ozfiddler, If you take a quick look at the code of the Email plugin, you can see there are 2 options when creating a custom plugin:

  • Add event handler for event ‘onFormProcessed’ and, like the Email plugin does, track the ‘email’ action and, instead of sending an email, save the form’s content into a file in eg. /user/data/email/email.log
  • Add event handler for event ‘onEmailSend’, get the forms content from the ‘vars’ parameter and save the form’s content.

Thanks. I’ll look into that. Not sure I am quite up to writing my own plugins yet but will see if I can see understand how it all works.

@ozfiddler, Try:

$ bin/gpm install devtools
$ bin/plugin devtools new-plugin

This will setup a basic functional plugin, which is approx. 75% of the code needed…

Unfortunately I don’t have access to command line. I see I can install devtools from the plugin list in admin, but is there any way to do the new-plugin setup without using that command?

@ozfiddler,

Unfortunately I don’t have access to command line.

I’m afraid the Admin route won’t help. You can’t create a plugin from within Admin. Even if you could, how would you then edit the PHP code of the plugin to save the emails?

If you want to learn to create/edit themes (as you did before) and plugins, a local development environment is almost a must have.

You could save each email as a file in user/data - just add

process:
    - save:
        fileprefix: feedback-
        dateformat: Ymd-His-u
        extension: txt
        body: "{% include 'forms/data.txt.twig' %}"
        operation: create

to the form config file - there used to be a plugin to stash them in a database too but i can’t find it right now.

Doh! Thanks @dan-james - I just checked my form and it already has those lines in there. Then I looked at my user/data folder and found all the contact emails had already been saved as individual text files.

Well, the people who designed all this are obviously way ahead of me.

@dan-james, That option totally slipped my mind…

@ozfiddler, Sorry for suggesting to create a custom plugin…

2 Likes

No problem! I still have to work out how to make these easily viewable, so that may still need to be done by a simple plugin. Now I have the basic plugin template installed I can work through the example in the docs.

For anyone trying to do the same thing, there is no need to reinvent the wheel (or even write a plugin). As pointed out above the lines added to the form will save the form information. You can save it in individual files or in one big file. As easy way to view them from the admin dashboard is to install the Data Manager plugin.

This adds a data manager link on the side menu that shows all the data types available:

Click on the item number link to open all the files:

contact-items

Then select the message you want to view:

message

Big thanks to @Karmalakas and @djamil for taking on board a suggestion to add a delete function. With version 1.4.1 of the Data Manager you can now delete items from both the list view…

and the individual item page…

1 Like