Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/doxakis/MailBody

Create transactional email with a fluent interface (.net)
https://github.com/doxakis/MailBody

csharp dotnet dotnetcore email fluent-interface

Last synced: about 2 months ago
JSON representation

Create transactional email with a fluent interface (.net)

Awesome Lists containing this project

README

        

# MailBody [![NuGet Status](https://badge.fury.io/nu/mailbody.svg)](https://www.nuget.org/packages/MailBody)
MailBody is a library for generating transactional email by using a fluent interface.

The current mail template is based on https://github.com/leemunroe/responsive-html-email-template
(MIT License 2013 Lee Munroe)

# Supported framework
- .net standard 2.0 (so, both .net core and .net framework)

# Install from Nuget
To get the latest version:
```
Install-Package MailBody
```

# MailBody Editor
If you want to create email with the MailBody library while having a live preview, you can use the [MailBody Editor](https://github.com/doxakis/MailBodyEditor)


Preview

# Quick Examples

## Email Address Confirmation

### C# syntax:
```
var body = MailBody
.CreateBody()
.Paragraph("Please confirm your email address by clicking the link below.")
.Paragraph("We may need to send you critical information about our service and it is important that we have an accurate email address.")
.Button("https://example.com/", "Confirm Email Address")
.Paragraph("— [Insert company name here]")
.ToString();
```

### Visual Basic syntax:
```
Dim body As String = MailBody.CreateBody() _
.Paragraph("Please confirm your email address by clicking the link below.") _
.Paragraph("We may need to send you critical information about our service and it is important that we have an accurate email address.") _
.Button("https://example.com/", "Confirm Email Address") _
.Paragraph("— [Insert company name here]") _
.ToString()
```
[Preview](https://doxakis.github.io/MailBody/src/Example/Output/EmailAddressConfirmation.html)

## Password reset
```
var appName = "My app";

var body = MailBody
.CreateBody()
.Paragraph("Hi,")
.Paragraph("You're receiving this email because someone requested a password reset for your user account at " + appName + ".")
.Button("https://www.example.com/", "Reset password")
.Paragraph("Thanks for using " + appName + "!")
.Paragraph("— [Insert company name here]")
.ToString();
```

[Preview](https://doxakis.github.io/MailBody/src/Example/Output/PasswordReset.html)

## Order confirmation
```
var products = new string[] { "1 x Product A", "2 x Product B", "3 x Product C" };

// Format product display.
var items = products.Select(item => MailBody.CreateBlock().Text(item));

var body = MailBody
.CreateBody()
.Title("Confirmation of your order")
.Paragraph("Hello,")
.Paragraph("We confirm having received your order.")
.Paragraph("Here is the list of ordered items:")
.UnorderedList(items)
.Paragraph("Thank you for ordering from us!")
.Paragraph("— [Insert company name here]")
.ToString();
```

[Preview](https://doxakis.github.io/MailBody/src/Example/Output/OrderConfirmation.html)

## Notification
```
var productName = "ABC";
var productStatus = "available";
var productDescription = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla sagittis nisl ut tellus egestas facilisis. Nulla eget erat dictum, facilisis libero sit amet, sollicitudin tortor. Morbi iaculis, urna eu tincidunt dapibus, sapien ex dictum nibh, non congue urna tellus vitae risus.";
var components = new string[] { "Part A", "Part B" };

// Format product display.
var items = components.Select(item => MailBody.CreateBlock().Text(item));

var body = MailBody
.CreateBody()
.Paragraph("Hello,")
.Paragraph("The product " + productName + " is now " + productStatus + ".")
.SubTitle("Here is the product summary:")
.Paragraph(MailBody.CreateBlock()
.StrongText("Product name: ").Text(productName))
.Paragraph(MailBody.CreateBlock()
.StrongText("Description: ").Text(productDescription))
.Paragraph(MailBody.CreateBlock()
.StrongText("Components:"))
.UnorderedList(items)
.Paragraph("— [Insert company name here]")
.ToString();
```

[Preview](https://doxakis.github.io/MailBody/src/Example/Output/Notification.html)

## With footer
```
var footer = MailBody
.CreateBlock()
.Text("Follow ")
.Link("http://twitter.com/example", "@Example")
.Text(" on Twitter.");

var body = MailBody
.CreateBody(footer)
.Paragraph("Please confirm your email address by clicking the link below.")
.Paragraph("We may need to send you critical information about our service and it is important that we have an accurate email address.")
.Button("https://www.example.com/", "Confirm Email Address")
.Paragraph("— [Insert company name here]")
.ToString();
```

[Preview](https://doxakis.github.io/MailBody/src/Example/Output/Withfooter.html)

## With image
**Please note** You can use CID Embedded Images, Base64 Encoding or use absolute url.
Image may not appear on all email client. So, make sure to do some tests.

```
var body = MailBody
.CreateBody()
.Image("https://placehold.it/540x70/ffffff/e8117f?text=My+logo", "My company name")
.Paragraph("Please confirm your email address by clicking the link below.")
.Paragraph("We may need to send you critical information about our service and it is important that we have an accurate email address.")
.Button("https://example.com/", "Confirm Email Address")
.Paragraph("— [Insert company name here]")
.ToString();
```

[Preview](https://doxakis.github.io/MailBody/src/Example/Output/WithImage.html)

## Custom theme & Raw html
```
var template = MailBodyTemplate.GetDefaultTemplate();
template
.Paragraph(m => "

{m.Content}

")
.Body(m => "" + m.Content + "
" + m.Footer + "")
.Text(m =>
"{m.Content}");

var footer = MailBody
.CreateBlock()
.Text("Follow ", new { color = "red"})
.Link("http://twitter.com/example", "@Example")
.Text(" on Twitter.", new { color = "#009900", backgroundColor = "#CCCCCC", fontWeight = "bold" });

var body = MailBody
.CreateBody(template, footer)
.Paragraph("Please confirm your email address by clicking the link below.")
.Raw("

We may need to send you critical information about our service and it is important that we have an accurate email address.

")
.Button("https://www.example.com/", "Confirm Email Address")
.Paragraph("— [Insert company name here]", new { color = "white", backgroundColor = "black" })
.ToString();
```

[Preview](https://doxakis.github.io/MailBody/src/Example/Output/CustomThemeAndRawHtml.html)

## Another way to create your email
```
var body = MailBody.CreateBody();

body.Paragraph("Hi,")
.Paragraph("First paragraph..");

// Your code

body.Button("https://www.example.com/", "First button");
body.Paragraph("Another paragraph..");

// Your code

body.Button("https://www.example.com/", "Second button")
.Paragraph("— [Insert company name here]");

var htmlBody = body.ToString();
```

[Preview](https://doxakis.github.io/MailBody/src/Example/Output/AnotherWay.html)

## Override the default template
This example is based on [Postmark templates](https://github.com/wildbit/postmark-templates).

```
var template = MailBodyTemplate.GetDefaultTemplate()
.Paragraph(m => $"

{m.Content}

")
.Link(m => $"{m.Content}")
.Title(m => $"

{m.Content}

")
.SubTitle(m => $"

{m.Content}

")
.Text(m => $"{m.Content}")
.StrongText(m => $"{m.Content}")
.UnorderedList(m => $"
    {m.Content}
")
.OrderedList(m => $"
    {m.Content}
")
.ListItem(m => $"
  • {m.Content}
  • ")
    .LineBreak(m => $"")
    .Button(m => @"








    " + m.Content + @"








    ")
    .Block(m => m.Content)
    .Body(m => @"... (see the examples for the complete source) ...");

    var body = MailBody
    .CreateBody(template)
    .Paragraph("Please confirm your email address by clicking the link below.")
    .Paragraph("We may need to send you critical information about our service and it is important that we have an accurate email address.")
    .Button("https://example.com/", "Confirm Email Address")
    .Paragraph("— [Insert company name here]")
    .ToString();
    ```

    [Preview](https://doxakis.github.io/MailBody/src/Example/Output/OverrideDefaultTemplate.html)

    # Copyright and license
    Code released under the MIT license.