GithubHelp home page GithubHelp logo

z38 / grav-plugin-email Goto Github PK

View Code? Open in Web Editor NEW

This project forked from getgrav/grav-plugin-email

0.0 2.0 0.0 502 KB

Grav Email Plugin

Home Page: http://getgrav.org

License: MIT License

PHP 100.00%

grav-plugin-email's Introduction

Grav Email Plugin

The email plugin for Grav adds the ability to send email. This is particularly useful for the admin and login plugins.

Installation

The email plugin is easy to install with GPM.

$ bin/gpm install email

Configuration

By default, the plugin uses PHP Mail as the mail engine.

You can configure the Email plugin by using the Admin plugin, navigating to the Plugins list and choosing Email.

That's the easiest route. Or you can also alter the Plugin configuration by copying the user/plugins/email/email.yaml file into user/config/plugins/email.yaml and make your modifications there.

The first setting you'd likely change is your Email from / Email to names and emails.

Also, you'd likely want to setup a SMTP server instead of using PHP Mail, as the latter is not 100% reliable and you might experience problems with emails.

Testing emails

A good way to test emails is to use a SMTP server service that's built for testing emails, for example https://mailtrap.io

Setup the Email plugin to use that SMTP server with the fake inbox data. For example enter this configuration in user/config/plugins/email.yaml or through the Admin panel:

mailer:
  engine: smtp
  smtp:
    server: mailtrap.io
    port: 2525
    encryption: none
    user: YOUR_MAILTRAP_INBOX_USER
    password: YOUR_MAILTRAP_INBOX_PASSWORD

That service will intercept emails and show them on their web-based interface instead of sending them for real.

You can try and fine tune the emails there while testing.

Programmatically send emails

Add this code in your plugins:

        $to = '[email protected]';
        $from = '[email protected]';

        $subject = 'Test';
        $content = 'Test';

        $message = $this->grav['Email']->message($subject, $content, 'text/html')
            ->setFrom($from)
            ->setTo($to);

        $sent = $this->grav['Email']->send($message);

Emails sent with Forms

When executing email actions during form processing, action parameters are inherited from the global configuration but may also be overridden on a per-action basis.

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 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'

Sending Attachments

You can add file inputs to your form, and send those files via Email. Just add an attachments field and list the file input fields names. You can have multiple file fields, and this will send all the files as attachments. Example:

form:
  name: custom_form
  fields:

    -
      name: my-file
      label: 'Add a file'
      type: file
      multiple: false
      destination: user/data/files
      accept:
        - application/pdf
        - application/x-pdf
        - image/png
        - text/plain

  process:
    -
      email:
        body: '{% include "forms/data.html.twig" %}'
        attachments:
          - 'my-file'

Additional action parameters

To have more control over your generated email, you may also use the following additional parameters:

  • reply_to: Set one or more addresses that should be used to reply to the message.
  • cc (Carbon copy): Add one or more addresses to the delivery list. Many email clients will mark email in one's inbox differently depending on whether they are in the To: or Cc: list.
  • bcc (Blind carbon copy): Add one or more addresses to the delivery list that should (usually) not be listed in the message data, remaining invisible to other recipients.
  • charset: Explicitly set a charset for the generated email body (only takes effect if body parameter is a string, defaults to utf-8)

Specifying email addresses

Email-related parameters (from, to, reply_to, ccand bcc) allow different notations for single / multiple values:

Single email address string

Multiple email address strings

Single email address with name

to:
  mail: [email protected]
  name: Human-readable name

Multiple email addresses (with and without names)

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):

body:
  -
    content_type: 'text/html'
    body: "{% include 'forms/default/data.html.twig' %}"
  -
    content_type: 'text/plain'
    body: "{% include 'forms/default/data.txt.twig' %}"
    charset: 'iso-8859-1'

Troubleshooting

Emails are not sent

As explained above in the Configuration section, if you're using the default settings, set the Plugin configuration to use a SMTP server. It can be Gmail or another SMTP server you have at your disposal.

This is the first thing to check. The reason is that PHP Mail, the default system used by the Plugin, is not 100% reliable and emails might not arrive.

grav-plugin-email's People

Contributors

diazwatson avatar flaviocopes avatar gour avatar groberts84 avatar hctom avatar hwmaier avatar janklostermann avatar johnmica avatar mahagr avatar rhukster avatar w00fz avatar z38 avatar zeegin avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.