Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Forms & Blueprints

How can single form send two emails using different titles and bodies

Solved by pamtbaau View solution

Started by Sjoerd Smeets 8 years ago · 9 replies · 1531 views
8 years ago

I'm trying to figure out how to specify the output of a form.
The standard output is enough for the configured emailaddress, but for the form-email i want a different format with output. How do i do this?

TWIG

        - email:
            from: "{{ config.plugins.email.from }}"
            to:
              - "{{ config.plugins.email.from }}"
              - "{{ form.value.email }}"
            subject: "[Aanmeldformulier] {{ form.value.name|e }}"
            body: "{% include 'forms/data.html.twig' %}"```

I need to figure out how to specify a second body/subject for {{ form.value.email }}.
8 years ago

I have setup my email form to display a simple html format in the email within the body. What you need to do is change this line from:

TWIG
body: "{% include 'forms/data.html.twig' %}" 

to:

TWIG
body: "{% extends 'email/base.html.twig' %}"
  • In your theme templates folder, create an 'email' folder inside templates/
  • Download this html email template and upload the base.html.twig to the email folder.
  • Test the template by filling out the contact form. You should receive a customize message containing the contact's info. Verify and tweak however you want the body of the email to read.
8 years ago

Thanks but this is not what i am looking for.. any other suggestions?

8 years ago

@SjoerdSmeets It might be worth considering to rephrase the question and explaining what you do look for... ;-)

After some pondering... Do you perhaps mean that you want to send two different emails, each with its own to/subject/body? One to yourself and one to {{ form.value.email }}?

I have no hands-on experience with the Email plugin, but have you tried splitting the email action into two separate actions? One to yourself and one to the client each with their own to/subject/body?

8 years ago

@SjoerdSmeets And? Have your tried the suggestion of sending two different 'email' actions?

8 years ago

I'm still looking for an example howto do this.. haven't found it yet..

8 years ago Solution

@SjoerdSmeets With some help of the Form docs, albeit a bit hidden I have to admit... The following seems to be doing what you are looking for.

Note: I used a fresh install of Grav 1.6.0-beta.6 with Quark and an inherited theme.

  • Create a new page with a contact form:
    TXT
    user/pages/03.contact/
      form.md
    
  • Add the following frontmatter with two separate email actions:

    YAML
    ---
    title: Contact Form
    
    form:
      name: contact
    
      fields:
          - name: name
            label: Name
            placeholder: Enter your name
            autocomplete: on
            type: text
            validate:
              required: true
    
          - name: email
            label: Email
            placeholder: Enter your email address
            type: email
            validate:
              required: true
    
          - name: message
            label: Message
            placeholder: Enter your message
            type: textarea
            validate:
              required: true
    
      buttons:
          - type: submit
            value: Submit
          - type: reset
            value: Reset
    
      process:
          - email:
              # To myself
              to: "{{ config.plugins.email.to }}"
              subject: "Email to myself"
          - email:
              # To visitor
              # Using defaults for 'to' and 'from'
              subject: "Email to visitor"
              body: "{% include 'forms/visitor.html.twig' %}"  <-- a custom template
    ---
    
    # Contact form
    
    Some sample page content
    
  • In your inherited theme create a new Twig template 'users/themes/mytheme/templates/forms/visitor.html.twig'. Add the following content:

    TWIG
    <p>This is the body for the visitor's email based on template "forms/data.html.twig"
    which can be found in folder "/user/plugins/form/templates/forms/".</p>;
    
    {% include "forms/data.html.twig" %} 
    

Hope this helps...

last edited 12/08/18 by pamtbaau
8 years ago

@SjoerdSmeets Nice!

I have taken the liberty to change the title of your post to match your intention a tad better.

Suggested topics

Topic Participants Replies Views Activity
Forms & Blueprints · by Ton Haarmans, 5 years ago
13 1135 4 months ago
Forms & Blueprints · by Hugo Oliveira, 5 months ago
0 61 5 months ago
Forms & Blueprints · by Flachy Joe, 6 months ago
9 134 6 months ago
Forms & Blueprints · by Augustus, 7 months ago
7 108 7 months ago
Forms & Blueprints · by Julien, 7 months ago
10 127 7 months ago