contact-form icon indicating copy to clipboard operation
contact-form copied to clipboard

"Message Template" setting

Open brandonkelly opened this issue 9 years ago • 12 comments

This could be used to customize the body contents, such as include the sender's email address within the message body.

brandonkelly avatar Mar 14 '17 21:03 brandonkelly

+1

outline4 avatar Jun 03 '17 19:06 outline4

Hah, I don't even remember posting this FR, but worth mentioning that a plugin can take over the message rendering using the beforeMessageCompile event (v1) or beforeSend event (v2).

brandonkelly avatar Jun 04 '17 14:06 brandonkelly

I know that... But it seems to be hard to achieve and kind of buggy.

I mean, the advantage over other add-ons it's that it's free and easy... If I'd had to customize it, there's no point in using this add-on.

Hiding the reply email and name within the "reply to" field is just not so very natural... Having the name and email address within the body is much more failsafe, logic and it's easy to implement... Even for me :)

It's just adding:

$email->htmlBody = '<p>Name: '.$message->fromName.'</p>';
$email->htmlBody .= '<p>Email Adresse: '.$message->fromEmail.'</p>';

before

// Prevent Twig tags from getting parsed
// add the "." before "=" when customizing this
$email->htmlBody .= str_replace(array('{', '}'), array('&lbrace;', '&rbrace;'), $message->htmlMessage);

+💯

outline4 avatar Jun 04 '17 23:06 outline4

@outline4 thanks

benblub avatar Dec 21 '17 12:12 benblub

$email->htmlBody = '<p>Name: '.$message->fromName.'</p>';
$email->htmlBody .= '<p>Email Adresse: '.$message->fromEmail.'</p>';

@outline4 Where did you add these lines??

KevinBeckers avatar May 02 '18 13:05 KevinBeckers

Please include such functionality. Honestly, its pretty much basic thing to have in any contact form plugin.

piotrpog avatar Apr 04 '19 11:04 piotrpog

A template would be nice. A little enchantment for now; https://github.com/craftcms/contact-form/pull/161

bertoost avatar Apr 19 '19 16:04 bertoost

@KevinBeckers I just wrote a simple Craft CMS module to solve this problem, if it's of interest. It can probably be done in a far more elegant way, but this works for my use case. https://github.com/LukePeters/contactformextras

LukePeters avatar Mar 02 '20 00:03 LukePeters

+1 A way to set a template for the email body is a pretty basic requirement and the body now is pretty primitive. I'm surprised it's not possible with this plugin with Craft having a full Twig template system on board.

Friksel avatar Apr 15 '20 15:04 Friksel

Yes please, the ability to set a template would be very useful!

billmn avatar Sep 14 '20 09:09 billmn

@brandonkelly

Hah, I don't even remember posting this FR, but worth mentioning that a plugin can take over the message rendering using the beforeMessageCompile event (v1) or beforeSend event (v2).

I'm trying to do this with the beforesend event but this doesn't seem to do anything at all. When following the code of contact-form v2, the message is already compiled before this event is fired. Also, there is a foreach on line 119 in Mailer.php which is not using the $event->message. So am i correct when saying this is impossible to achieve?

kevinmu17 avatar Jul 22 '21 09:07 kevinmu17

N.V.M. i made a stupid assumption/mistake.

Here is the code for people who want to achieve the same.

Event::on(Mailer::class, Mailer::EVENT_BEFORE_SEND, function(SendEvent $e) {
        // Change whatever you want in your submission data ($e->submission->message)
        $e->message->setTextBody($text);
        $e->message->setHtmlBody($html);
});

kevinmu17 avatar Jul 22 '21 09:07 kevinmu17