1.0.4 • Published 2 years ago

@tellimer/mailable v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

@tellimer/mailable

Allows you to create React components that render to email HTML

Example mailable

import React from 'react'
import { Components, Mailable } from '@tellimer/mailable'

export class ExampleMailable extends Mailable {
  // This is required
  subject = 'Test'

  // This is required, can be just a string (containing email)
  from = {
    name: 'Tellimer Test',
    email: 'test@test.com',
  }
  // OR from = 'test@test.com'

  // Does not have to return a promise, but allows for it
  async view () {
    const somethingElse = await new Promise((resolve) => setTimeout(() => resolve('hello'), 500))

    return <div>This is an email!</div>
  }
}

// .........

const mailable = new ExampleMailable()
mailable.render().then(html => console.log(html))

Would output the following HTML

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:o="urn:schemas-microsoft-com:office:office" style="margin: 0; padding: 0; font-size: 16px; color: #242424; font-family: Arial, Helvetica, Verdana, Tahoma, sans-serif;">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <meta name="x-apple-disable-message-reformatting">
    <title>Test</title>
    <!--[if mso]>
    <noscript>
        <xml>
            <o:OfficeDocumentSettings>
                <o:PixelsPerInch>96</o:PixelsPerInch>
            </o:OfficeDocumentSettings>
        </xml>
    </noscript>
    <![endif]-->
</head>
<body style="background: #eaeaea; margin: 0; padding: 0; font-size: 16px; color: #242424; font-family: Arial, Helvetica, Verdana, Tahoma, sans-serif;">
  <div data-reactroot>This is an email!</div>
</body>
</html>

Versioned example

First, you must create some versions of the email. These versions must be MailableVersion, which extends the Mailable class. The only new property in this class, is the version property, which is required.

Then, you must create a class that extends MailableVersionFactory. This will contain a method version that returns the correct MailableVersion to send to a person.

import React from 'react'
import {
  Components,
  Mailable,
  MailableVersion,
  MailableVersionFactory,
  Personalization
} from '@tellimer/mailable';

const { Email } = Components

export class Versioned extends MailableVersionFactory {
  version(personalization: Personalization) {
    return parseInt(personalization.customArgs?.userId, 10) % 2 === 0 ? new VersionA() : new VersionB()
  }
}

class VersionA extends MailableVersion {
  version = 'A'
  subject = 'this is a subject for A'
  from = {email: 'test@test.com'}

  view() {
    return (
      <Email>
        <div>
          for version B
        </div>
      </Email>
    )
  }
}

class VersionB extends MailableVersion {
  version = 'B'
  subject = 'this is a subject for B'
  from = {email: 'test@test.com'}

  view() {
    return (
      <Email>
        <div>
          for version B
        </div>
      </Email>
    )
  }
}

Components

Run storybook to see more information!

yarn storybook

<Email header?={React.ReactComponent} footer?={React.ReactComponent}>
<EmailRow>
<EmailCol>
1.0.2

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.0

3 years ago

1.0.0-RC48

3 years ago

1.0.0-RC40

3 years ago

1.0.0-RC42

3 years ago

1.0.0-RC41

3 years ago

1.0.0-RC47

3 years ago

1.0.0-RC44

3 years ago

1.0.0-RC43

3 years ago

1.0.0-RC46

3 years ago

1.0.0-RC45

3 years ago

1.0.0-RC29

3 years ago

1.0.0-RC31

3 years ago

1.0.0-RC30

3 years ago

1.0.0-RC37

3 years ago

1.0.0-RC36

3 years ago

1.0.0-RC39

3 years ago

1.0.0-RC38

3 years ago

1.0.0-RC33

3 years ago

1.0.0-RC32

3 years ago

1.0.0-RC35

3 years ago

1.0.0-RC34

3 years ago

1.0.0-RC20

3 years ago

1.0.0-RC26

3 years ago

1.0.0-RC25

3 years ago

1.0.0-RC28

3 years ago

1.0.0-RC27

3 years ago

1.0.0-RC22

3 years ago

1.0.0-RC21

3 years ago

1.0.0-RC24

3 years ago

1.0.0-RC23

3 years ago

1.0.0-RC15

3 years ago

1.0.0-RC14

3 years ago

1.0.0-RC17

3 years ago

1.0.0-RC16

3 years ago

1.0.0-RC13

3 years ago

1.0.0-RC19

3 years ago

1.0.0-RC18

3 years ago

1.0.0-RC6

3 years ago

1.0.0-RC7

3 years ago

1.0.0-RC8

3 years ago

1.0.0-RC11

3 years ago

1.0.0-RC10

3 years ago

1.0.0-RC12

3 years ago

1.0.0-RC9

3 years ago

1.0.0-RC5

3 years ago

1.0.0-RC4

3 years ago

1.0.0-RC3

3 years ago

1.0.0-RC2

3 years ago

1.0.0-RC1

3 years ago

1.0.0-RC

3 years ago