Skip to content

Commit

Permalink
Merge branch 'release/3.0.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
rhukster committed Aug 16, 2019
2 parents e992cda + ef08ad0 commit f0bba1e
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 17 deletions.
109 changes: 94 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -246,15 +246,79 @@ form:
# Their values may be referenced in email actions via '{{ form.value.FIELDNAME|e }}'
process:
- email:
subject: "[Custom form] {{ form.value.name|e }}"
email:
subject: "[Custom form] {{ form.value.name|e }}"
body: "{% include 'forms/data.txt.twig' %}"
from: [email protected]
from_name: 'Custom sender name'
to: [email protected]
to_name: 'Custom recipient name'
content_type: 'text/plain'
process_markdown: true
```

## Multiple Emails

You can send multiple emails by creating an array of emails under the `process: email:` option in the form:

```
title: Custom form
form:
name: custom_form
fields:
# Any fields you'd like to add to the form:
# Their values may be referenced in email actions via '{{ form.value.FIELDNAME|e }}'
process:
email:
-
subject: "[Custom Email 1] {{ form.value.name|e }}"
body: "{% include 'forms/data.txt.twig' %}"
from: [email protected]
from_name: 'Custom sender name'
to: [email protected]
to_name: 'Custom recipient name'
content_type: 'text/plain'
process_markdown: true
from: {mail: "[email protected]", name: "Site OWner"}'
to: {mail: "[email protected]", name: "Recepient 1"}
template: "email/base.html.twig"
-
subject: "[Custom Email 2] {{ form.value.name|e }}"
body: "{% include 'forms/data.txt.twig' %}"
from: {mail: "[email protected]", name: "Site OWner"}'
to: {mail: "[email protected]", name: "Recepient 1"}
template: "email/base.html.twig"
```

## Templating Emails

You can specify a Twig template for HTML rendering, else Grav will use the default one `email/base.html.twig` which is included in this plugin. You can also specify a custom template that extends the base, where you can customize the `{% block content %}` and `{% block footer %}`. For example:

```twig
{% extends 'email/base.html.twig' %}
{% block content %}
<p>
Greetings {{ form.value.name|e }},
</p>
<p>
We have received your request for help. Our team will get in touch with you within 3 Business Days.
</p>
<p>
Regards,
</p>
<p>
<b>My Company</b>
<br /><br />
E - <a href="mailto:[email protected]">[email protected]</a><br />
M - +1 555-123-4567<br />
W - <a href="https://mycompany.com">mycompany.com</a>
</p>
{% endblock %}
{% block footer %}
<p style="text-align: center;">My Company - All Rights Reserved</p>
{% endblock %}
```

## Sending Attachments
Expand All @@ -267,8 +331,7 @@ form:
name: custom_form
fields:
-
name: my-file
my-file:
label: 'Add a file'
type: file
multiple: false
Expand All @@ -280,11 +343,11 @@ form:
- text/plain
process:
-
email:
body: '{% include "forms/data.html.twig" %}'
attachments:
- 'my-file'
email:
body: '{% include "forms/data.html.twig" %}'
attachments:
- 'my-file'
```

## Additional action parameters
Expand Down Expand Up @@ -323,6 +386,12 @@ to:
name: Human-readable name
```

or inline:

```
to: {mail: '[email protected]', name: 'Human-readable name'}
```

#### Multiple email addresses (with and without names)

```
Expand All @@ -339,6 +408,16 @@ to:
[email protected]
```

or inline:

```
to:
- {mail: '[email protected]', name: 'Human-readable name'}
- {mail: '[email protected]', name: 'Another human-readable name'}
- [email protected]
- [email protected]
```

## Multi-part MIME messages

Apart from a simple string, an email body may contain different MIME parts (e.g. HTML body with plain text fallback). You may even specify a different charset for each part (default to `utf-8`):
Expand Down
6 changes: 4 additions & 2 deletions email.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function onFormProcessed(Event $event)

$this->grav->fireEvent('onEmailSend', new Event(['params' => &$params, 'vars' => &$vars]));

if (!$this->isAssocArray($params)) {
if ($this->isAssocArray($params)) {
$this->sendFormEmail($form, $params, $vars);
} else {
foreach ($params as $email) {
Expand Down Expand Up @@ -142,7 +142,9 @@ protected function sendFormEmail($form, $params, $vars)
protected function isAssocArray(array $arr)
{
if (array() === $arr) return false;
return array_keys($arr) !== range(0, count($arr) - 1);
$keys = array_keys($arr);
$index_keys = range(0, count($arr) - 1);
return $keys !== $index_keys;
}

}

0 comments on commit f0bba1e

Please sign in to comment.