Best way to send a copy of an email?

I would like to do two things:

  1. send a copy of an email sent via a frontend contact form to the sender, if the user has ticked a checkbox, with a different subject,
  2. send a copy of an email sent via a frontend application form to the sender, with some added intro text in the message and a different subject.

I had a look at the code in the email plugin and noticed a convenient hook, onEmailMessage, which is fired just before the message is sent. I figured I could just jump in there, copy the whole message, tweak the subject, the recipient and the actual body text a bit, and send that copy off, buuuuuuut I can’t seem to change anything in the copy (I don’t know how) and also I’m not sure that is a good way to go about it?

Any ideas are much appreciated…

@Netzhexe, Although the Form plugin is quite a flexible tool, I’m not a user of it. I just like coding my own re-usable plugins… It’s not that hard.

If you build it once with some flexible options you can re-use it in many places.

Any way… If you do a grep -r fireEvent user/plugins/email in the root of your site, you will see there are 2 events fired:

  • $this->grav->fireEvent('onEmailSend', new Event(['params' => &$params, 'vars' => &$vars]));
    The variables are sent by ref, so you can change their values. Haven’t look at their meaning, although I think you might be able to add recipients, alter the message, …
  • $this->grav->fireEvent('onEmailMessage', new Event(['message' => $message, 'params' => $params, 'form' => $form]));
    I thinks this is more like, “FYI, this is what I’m about to send”.