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”.

If you read documantation you can find this:

Additional action parameters

To have more control over your generated email, you may also use the following additional parameters:

  • reply_to : Set one or more addresses that should be used to reply to the message.
  • cc (Carbon copy) : Add one or more addresses to the delivery list. Many email clients will mark email in one’s inbox differently depending on whether they are in the To: or Cc: list.
  • bcc (Blind carbon copy) : Add one or more addresses to the delivery list that should (usually) not be listed in the message data, remaining invisible to other recipients.
  • charset : Explicitly set a charset for the generated email body (only takes effect if body parameter is a string, defaults to utf-8 )

Specifying email addresses

Email-related parameters ( from , to , reply_to , cc and bcc ) allow different notations for single / multiple values:

Single email address string

to: mail@example.com

Multiple email address strings

to:
  - mail@example.com
  - mail+1@example.com
  - mail+2@example.com

I hope is your answer :wink: