Coder Social home page Coder Social logo

codefork / mailbody Goto Github PK

View Code? Open in Web Editor NEW

This project forked from doxakis/mailbody

0.0 1.0 0.0 112 KB

Create transactional email with a fluent interface (.net)

Home Page: https://doxakis.github.io/MailBody/

License: MIT License

HTML 69.57% C# 30.43%

mailbody's Introduction

MailBody Build Status NuGet Status

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

  • dotnet core 1.0
  • .net framework 4.5

Install from Nuget

To get the latest version:

Install-Package MailBody

Quick Examples

Email Address Confirmation

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();

Preview

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

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

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

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

Custom theme & Raw html

var template = MailBodyTemplate.GetDefaultTemplate();
template
    .Paragraph(m =>
        "<p style='" +
        (m.IsProperty(() => m.Attributes.color) ? $"color:{m.Attributes.color};" : string.Empty) +
        (m.IsProperty(() => m.Attributes.backgroundColor) ? $"background-color:{m.Attributes.backgroundColor};" : string.Empty) +
        $"'>{m.Content}</p>")
    .Body(m => "<html><body>" + m.Content + "<br />" + m.Footer + "</body></html>")
    .Text(m =>
        $"<span style='" +
        (m.IsProperty(() => m.Attributes.color) ? $"color:{m.Attributes.color};" : string.Empty) +
        (m.IsProperty(() => m.Attributes.backgroundColor) ? $"background-color:{m.Attributes.backgroundColor};" : string.Empty) +
        (m.IsProperty(() => m.Attributes.fontWeight) ? $"font-weight:{m.Attributes.fontWeight};" : string.Empty) +
        $"'>{m.Content}</span>");

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("<p>We may need to send you <strong>critical information</strong> about our service and it is important that we have an accurate email address.</p>")
    .Button("https://www.example.com/", "Confirm Email Address")
    .Paragraph("— [Insert company name here]", new { color = "white", backgroundColor = "black" })
    .ToString();

Preview

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

Override the default template

This example is based on Postmark templates.

var template = MailBodyTemplate.GetDefaultTemplate()
    .Paragraph(m => $"<p style='font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; Margin-bottom: 15px;'>{m.Content}</p>")
    .Link(m => $"<a href='{m.Link}'>{m.Content}</a>")
    .Title(m => $"<h1>{m.Content}</h1>")
    .SubTitle(m => $"<h2>{m.Content}</h2>")
    .Text(m => $"{m.Content}")
    .StrongText(m => $"<strong>{m.Content}</strong>")
    .UnorderedList(m => $"<ul>{m.Content}</ul>")
    .OrderedList(m => $"<ol>{m.Content}</ol>")
    .ListItem(m => $"<li>{m.Content}</li>")
    .LineBreak(m => $"</br>")
    .Button(m => @"<table class='body-action' align='center' width='100%' cellpadding='0' cellspacing='0'>
            <tr>
                <td align='center'>
                <table width='100%' border='0' cellspacing='0' cellpadding='0'>
                    <tr>
                    <td align='center'>
                        <table border='0' cellspacing='0' cellpadding='0'>
                        <tr>
                            <td>
                            <a href='" + m.Link + @"' class='button button--' target='_blank'>" + m.Content + @"</a>
                            </td>
                        </tr>
                        </table>
                    </td>
                    </tr>
                </table>
                </td>
            </tr>
            </table>")
    .Block(m => m.Content)
    .Body(m => @"<html>... (see the examples for the complete source) ...</html>");

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

Copyright and license

Code released under the MIT license.

mailbody's People

Contributors

doxakis avatar ramon-balaguer avatar

Watchers

 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.