Skip to content

Commit

Permalink
refactor: Update email generation template with new item class and pr…
Browse files Browse the repository at this point in the history
…oduct model.
  • Loading branch information
hsndmr committed Jul 7, 2024
1 parent 5699c77 commit 55d15b7
Showing 1 changed file with 79 additions and 84 deletions.
163 changes: 79 additions & 84 deletions Mailgen/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,107 +17,102 @@ dotnet add package Mailgen

Then, use the following code to generate an e-mail:

Create an item class that will be used in the table.

```csharp
using Mailgen;
using Mailgen.Dtos;
using Mailgen.Templates.Models;
public class Item
{
public required string Name { get; set; }
public required string Description { get; set; }
public required string Price { get; set; }
}
```

var createMailGeneratorDto = new CreateMailGeneratorDto
```csharp
var product = new ProductModel
{
Options = new TemplateOptionsModel
{
Product = new ProductModel
{
CopyrightLeft = "© 2024",
CopyrightRight = "All rights reserved.",
Link = "https://github.com/hsndmr",
Name = "Example Product"
}
}
CopyrightLeft = "© 2024",
CopyrightRight = "All rights reserved.",
Link = "https://github.com/hsndmr",
Name = "Example Product"
};


var mailGenerator = new MailGenerator(new HtmlGeneratorFactory(), createMailGeneratorDto);
var generateMailDto = new GenerateMailDto<Item>
var mailGenerator = new MailGenerator(product, new DefaultTemplate());
var body = new BodyModel<Item>
{
Body = new TemplateBodyModel<Item>
Title = "Hi John Appleseed,",
Intros =
[
"Your order has been processed successfully."
],
Tables = new List<TableModel<Item>>
{
Title = "Hi John Appleseed,",
Intros =
[
"Your order has been processed successfully."
],
Tables = new List<TableModel<Item>>
new()
{
new()
{
Columns =
[
new TableColumnModel<Item>
{
Header = "Item",
ValueSelector = item => item.Name,
Width = "20%"
},
new TableColumnModel<Item>
{
Header = "Description",
ValueSelector = item => item.Description
},
new TableColumnModel<Item>
{
Header = "Price",
ValueSelector = item => item.Price,
Width = "15%",
Align = "right"
}
],
Rows =
[
new Item
{
Name = "Node.js",
Description = "Event-driven I/O server-side JavaScript environment based on V8.",
Price = "$10.99"
},
new Item
{
Name = "Mailgen",
Description = "A .NET library for generating HTML emails.",
Price = "$1.99"
}
]
}
},
Actions =
[
new ActionModel
{
Instructions = "You can check the status of your order and more in your dashboard:",
Button = new ButtonModel
Columns =
[
new TableColumnModel<Item>
{
Header = "Item",
ValueSelector = item => item.Name,
Width = "20%"
},
new TableColumnModel<Item>
{
Header = "Description",
ValueSelector = item => item.Description
},
new TableColumnModel<Item>
{
Header = "Price",
ValueSelector = item => item.Price,
Width = "15%",
Align = "right"
}
],
Rows =
[
new Item
{
Name = "Node.js",
Description = "Event-driven I/O server-side JavaScript environment based on V8.",
Price = "$10.99"
},
new Item
{
Color = "#22BC66",
Text = "Go to Dashboard",
Link = "https://github.com/hsndmr"
Name = "Mailgen",
Description = "A .NET library for generating HTML emails.",
Price = "$1.99"
}
]
}
},
Actions =
[
new ActionModel
{
Instructions = "You can check the status of your order and more in your dashboard:",
Button = new ButtonModel
{
Color = "#22BC66",
Text = "Go to Dashboard",
Link = "https://github.com/hsndmr"
}
],
Outro =
[
"We thank you for your purchase."
],
Signature = "Yours truly"
}
}
],
Outro =
[
"We thank you for your purchase."
],
Signature = "Yours truly"
};

var html = await mailGenerator.GenerateMail(generateMailDto);

var html = await mailGenerator.GenerateMail(body);
Console.WriteLine(html);
```

This code would output the following HTML template:

<img src="https://raw.github.com/hsndmr/mailgen/main/screenshots/default/receipt.png" height="400" />
<img src="https://raw.github.com/hsndmr/mailgen/main/screenshots/default/receipt.png" />

## Example

Expand Down

0 comments on commit 55d15b7

Please sign in to comment.