0

I would like to use a "dynamic" variable (for lack of better words) in my plugin settings. I am sending whomever fills out my contact form a welcome email. I'd like to be able to include the persons name if I could.

Here is my settings file:

_settings.html

{{
forms.textareaField({
    label:        "Welcome email Message"|t,
    id:           'welcomeEmailMessage',
    name:         'welcomeEmailMessage',
    instructions: "The email message that is sent to that the person who filled out the contact form."|t,
    value:        (settings.welcomeEmailMessage ? settings.welcomeEmailMessage : "Thank you {firstName} from {siteName}"|t({ siteName: craft.app.siteName })),
    errors:       settings.getErrors('welcomeEmailMessage')
    })
}}

I know {siteName} is a variable that's available everywhere. {firstName} is the name of my form input that I'd like to somehow hook into.

I have all of the information I need to send the email to the user - just not sure how to connect the dots between the service that sends the email and the settings so I can use {firstName} and it will get replaced with the value of the firstName input field.

Here is my service if it helps to see what I'm trying to do...

MyService.php

...
    $email->toEmail = $contact->email;
    $email->subject = $settings->guestSubject;
    $email->body = $settings->welcomeEmailMessage;
...

EDIT Sorry, this is kind of hard to describe. I'll keep going maybe that will help.

I have a contact form plugin, that has a setting of welcomeEmailMessage. That setting allows the admin to set the text to the email that is sent to whomever filled out my form. Think of it like a confirmation email.

But, instead of just saying "Hi there..." I was trying to grab the value of the firstName input and inject that into the email message that resides in the plugin settings.

The email could then read: "Hi there Bob..."

I have attached a screen shot of my settings page. enter image description here

So you can see in the image I have {firstName} in the textarea. I'm struggling on how to grab the value from the contact form - and inject it into that template variable.

It looks like it's very similar to the "Email Settings" area within the CP. Only instead of getting the dynamic parts from the install settings, I want to get the values from the form that was submitted.

enter image description here

Hope that helps?

EDIT

Reading though the comments, I'm struggling a little about how to implement them.

Here is my welcomeEmailMessage. It's located in my plugin's directory/templates

html

<html>
    <body>
        <div>
            Hello, {{ firstName }}
        </div>
    </body>
</html>

Then in my service I now have this:

php

$email = new EmailModel();
craft()->templates->renderString($settings->welcomeEmailMessage, ['firstName' => $contact->firstName]);

So now it seems like I need to tell my plugin how to get to that template?

  • Not sure I'm following... where is the person's name coming from? The currently logged in user? – Brad Bell Jul 15 '15 at 2:24
  • The person's name would come from the contact form. Once they click submit, I can grab the input value(s) but I'm not sure how to associate those values back to my plugin settings. – Damon Jul 15 '15 at 15:07
3

First - you don't need a template file. You can type the template right into the settings textarea like you already had, and like the other email twig templates do it. Then, in your service, you do:

$email->body = craft()->templates->renderString($settings->welcomeEmailMessage, ['firstName'=>$firstName]);

The settings textarea then need to have something like:

Hello {{firstName}}, welcome!

And then you should be all set!

  • By using this method, you will also have all your variables like {{ siteName }} rendered aswell. So you only need to worry about the custom variables like {{ link }} and by passing a user model as 'user' => $user you will have access to things like {{ user.friendlyName }} without having to specify them one by one – Alec Ritson Jul 16 '15 at 9:16
  • Thank you very much! I apologize though, you'll need to hold my hand just a bit more. I've updated my question to show what I have so far. – Damon Jul 16 '15 at 17:17
  • I updated my answer a little to hopefully make it a little clearer. – megatrond Jul 17 '15 at 11:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.