Skip to content

Commit

Permalink
Update documentation on handling emails
Browse files Browse the repository at this point in the history
  • Loading branch information
wernerkrauss committed Oct 18, 2023
1 parent 3e26643 commit 7dca346
Showing 1 changed file with 62 additions and 7 deletions.
69 changes: 62 additions & 7 deletions docs/en/02_Customisation/Emails.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ SilverShop\Extension\ShopConfigExtension:

## Modifying templates

Update email content by overriding the templates inside the `templates/email/` folder. Just create a corresponding folder/template in your mysite folder, such as `mysite/templates/email/Order_RecieptEmail`.
Update email content by overriding the templates inside the `templates/email/` folder.
Silvershop respects the frontend theme and tries to use those templates.
Just create a corresponding folder/template in your theme or app folder, such as `themes/mytheme/templates/email/Order_RecieptEmail.ss` or `app/templates/email/Order_RecieptEmail.ss`.

Remember: `app/templates` overrules `themes/mytheme/templates`.

## Modifying subject lines

Expand All @@ -35,17 +39,68 @@ en:
ShopEmail:
ConfirmationSubject: 'My Website Order #{OrderNo} confirmation'
ReceiptSubject: 'My Website Order #{OrderNo} receipt'
CancelSubject: 'My Website Order #{OrderNo} cancelled by member'
CancelSubject: 'My Website Order #{OrderNo} cancelled by member'
```
## Making your own Notifier
There may be times when you want to add custom logic around the e-mail. For instance if your purchase requires your user to enter e-mail addresses and each of those addresses receives a different confirmation e-mail. In this situation make a `MyCustomOrderEmailNotifier` that can optionally extend `OrderEmailNotifier` and add custom logic into it then declare it to replace the current notifier
There may be times when you want to add custom logic around the email. For instance if your purchase requires your user to enter email addresses and each of those addresses receives a different confirmation email. Or if you want to add custom content to the emails sent.
In this situation you can use `OrderEmailNotifier`'s exension hooks to modify all emails, e.g.:

```php
<?php
namespace My\Namespace\Extensions;
use SilverStripe\Control\Email\Email;
use SilverStripe\Core\Extension;
class ShopEmails extends Extension
{
public function updateClientEmail(Email $email): void
{
$confirmationmessage = 'some text';
$email->addData([
'EmailConfirmationMessage' => $confirmationmessage,
]);
}
public function updateAdminEmail(Email $email): void
{
if($replyTo = $this->getOwner()->getOrder()->getLatestEmail()){
$email->setReplyTo($replyTo);
}
}
}
```

Now add this extension to `OrderEmailNotifier` in one of your confing files:

```yaml
# in mysite/config.yml
Injector:
OrderEmailNotifier:
class: MyCustomOrderEmailNotifier
SilverShop\Checkout\OrderEmailNotifier:
extensions:
depotemails: My\Namespace\Extensions\ShopEmails
```

You can also add your own `sendFooEmail()` methods to that extension.

## Previewing and Testing Emails

Silvershop has a task to preview the generated emails, `SilverShop\Tasks\ShopEmailPreviewTask`.

In case you send customer specific emails (e.g. for sending download links), you can add those to the task's config:

```yaml
SilverShop\Tasks\ShopEmailPreviewTask:
previewable_emails:
download: DownloadInformation
```

Now you need to add a method `sendDownloadInformation` to your `ShopEmails` notification, similar to `OrderEmailNotifier`'s `sendConfimation()` or `sendReceipt()` methods.

## See also

* [Silverstripe CMS Documentation - Email](https://docs.silverstripe.org/en/5/developer_guides/email/)

0 comments on commit 7dca346

Please sign in to comment.