Skip to content

Mail content using content helpers

Sergi Baila edited this page Jun 9, 2016 · 6 revisions

Caution

This documentation is about a work in progress feature not currently published. If you are using the published version 1.0.0 please don't use this documentation and refer to the README


Motivation

The basic usage of this library allows you to send messages throwing simple strings as the text and/or html content at the respective methods. Sometimes, though, you may want to use some basic formatting that HTML provides but don't care about a full fledged complex HTML email template. Examples: add some bold text, create a link, format a basic table.

The classes on the package net.sargue.mailgun.content are designed to easily build basic HTML messages. It's not supposed to be used for building cutting edge responsive modern HTML messages. It's just for simple cases where you need to send a message and you want to use some basic HTML like tables and some formatting.

Basic examples

Some self explanatory examples:

Mail.using(configuration)
    .body()
    .h1("This is a heading")
    .p("And this some text")
    .mail()
    .to("[email protected]")
    .subject("This is the subject")
    .build()
    .send();
Mail.using(configuration)
    .body()
    .h3("Monthly report")
    .p("Report of the number of time travels this month")
    .table()
        .row("Marty", "5")
        .row("Doc", "7")
        .row("Einstein", "0")
    .end()
    .mail()
    .to("[email protected]")
    .subject("Monthly Delorean usage")
    .build()
    .send();

Of course you can keep the body content and mail building separated.

Body body = Body.builder()
                .h1("This is a heading")
                .p("And this some text")
                .build();

Mail.using(configuration)
    .to("[email protected]")
    .subject("This is the subject")
    .content(body)
    .build()
    .send();

Usage

The main class is the Builder which you use to construct a Body using a fluent interface. Like this:

Body body = Body.builder()
                .h1("This is a heading")
                .p("And this some text")
                .build();

A Body is really just a tuple with two strings: the test version and the HTML version. When you are building your mail using MailBuilder you have the MailBuilder#content() method which accepts a Body. Completing the example:

Mail.using(configuration)
    .to("[email protected]")
    .subject("This is the subject")
    .content(body)
    .build()
    .send();

Most of the time you would probably build the content, the email and send it. So you can chain it an get a nice fluent conversation.

Mail.using(configuration)
    .body()
    .h1("This is a heading")
    .p("And this some text")
    .mail()
    .to("[email protected]")
    .subject("This is the subject")
    .build()
    .send();

How it works

As said before this is for very simple formatted messages. Most email clients can read basic HTML but the proper way is to include also a text only version. Even if it, probably, won't be used. These classes take care of that just by generating both text and HTML versions of the orders you give. Most of the time the text version is just the content. You ask for emphasis of a text? The HTML will go <em>Hello world</em> but the text version will go just like Hello world. The aim is to be readable not an accurate representation of the formatting. No fancy ASCII tricks here.

Basic formatting

You will find several common HTML tags in the Builder class. Most accept content directly or can be called without parameters if you need more complex content or to nest other directives. Remember to close your tags calling end().

Example of heading with direct content and nested content. They produce the same result:

Body.builder()
    .h1("This is the heading")
    .build();
Body.builder()
    .h1()
    .text("This is the heading")
    .end()
    .build();

Tables

There is a basic HTML table support that was present since version 1.0.0 but is very raw so you have to explicit most of those HTML directives for a table. You are responsible for the order of the directives: table, tr and td.

A simple example:

Mail.using(configuration)
    .body()
    .h3("Monthly report")
    .p("Report of the number of time travels this month")
    .table()
        .row("Marty", "5")
        .row("Doc", "7")
        .row("Einstein", "0")
    .end()
    .mail()
    .to("[email protected]")
    .subject("Monthly Delorean usage")
    .build()
    .send();

Data tables

Content converters

Clone this wiki locally