Last active
October 30, 2018 17:54
-
-
Save stefanolsen/9d49a3a3c80267435b18108eb2c4a2b9 to your computer and use it in GitHub Desktop.
Code listings for blog post about using SendGrid with Episerver Commerce. Read about it here: https://stefanolsen.com/posts/sending-emails-from-episerver-commerce-using-sendgrid/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"OrderId":"PO512658", | |
"BillingAddress":{ | |
"AddressId":"Stefan Olsen", | |
"Name":"Stefan Olsen", | |
"FirstName":"Stefan", | |
"LastName":"Olsen", | |
"CountryName":"United Arab Emirates", | |
"CountryCode":"ARE", | |
"City":"Dubai", | |
"PostalCode":"0000", | |
"Line1":"My Tower, Apt. 3300", | |
"Line2":"", | |
"CountryRegion":{ | |
"Region":"Dubai" | |
}, | |
"Email":"stefan@stefanolsen.com", | |
"ShippingDefault":false, | |
"BillingDefault":false, | |
"DaytimePhoneNumber":"" | |
}, | |
"BillingAddressUK":false, | |
"BillingAddressUS":false, | |
"Payments":[ | |
{ | |
"__MetaClass":"OtherPayment", | |
"CreatorId":null, | |
"ModifierId":null, | |
"Created":"2018-10-30T16:22:48.24Z", | |
"Modified":"2018-10-30T16:22:48.433Z", | |
"ValidationCode":"", | |
"Status":"Processed", | |
"PaymentType":"4", | |
"TransactionID":"", | |
"ImplementationClass":"Mediachase.Commerce.Orders.OtherPayment,Mediachase.Commerce", | |
"OrderFormId":"1007", | |
"PaymentId":"1007", | |
"AuthorizationCode":"", | |
"PaymentMethodId":"81b565b8-ee5e-40c6-9259-ba5d89d42f2c", | |
"BillingAddressId":"Stefan Olsen", | |
"TransactionType":"Authorization", | |
"OrderGroupId":"1007", | |
"PaymentMethodName":"CashOnDelivery", | |
"Amount":"29.280000000", | |
"CustomerName":"", | |
"ProviderTransactionID":"" | |
} | |
], | |
"Created":"10/30/2018 4:22:48 PM", | |
"Currency":"USD", | |
"HandlingTotal":"$0.00", | |
"ShippingSubTotal":"$20.00", | |
"ShippingDiscountTotal":"$0.00", | |
"ShippingTotal":"$20.00", | |
"TaxTotal":"$0.00", | |
"CartTotal":"$29.28", | |
"OrderLevelDiscountTotal":"$0.00", | |
"Shipments":[ | |
{ | |
"LineItems":[ | |
{ | |
"Quantity":1, | |
"DisplayName":"Faded Glory Mens Canvas Twin Gore Slip-On Shoe", | |
"Url":"http://localhost:50244/en/fashion/mens/mens-shoes/p-36127195/?variationCode=SKU-36127195", | |
"UnitPrice":"$14.50", | |
"Price":"$14.50", | |
"Discount":"$5.22", | |
"Total":"$9.28" | |
} | |
], | |
"ShipmentCost":"$20.00", | |
"Address":{ | |
"AddressId":"Stefan Olsen", | |
"Name":"Stefan Olsen", | |
"FirstName":"Stefan", | |
"LastName":"Olsen", | |
"CountryName":"United Arab Emirates", | |
"CountryCode":"ARE", | |
"City":"Dubai", | |
"PostalCode":"0000", | |
"Line1":"My Tower, Apt. 3300", | |
"Line2":"", | |
"CountryRegion":{ | |
"Region":"Dubai" | |
}, | |
"Email":"stefan@stefanolsen.com", | |
"ShippingDefault":false, | |
"BillingDefault":false, | |
"DaytimePhoneNumber":"" | |
}, | |
"ShippingAddressUK":false, | |
"ShippingAddressUS":false, | |
"DiscountPrice":"$0.00", | |
"ShippingItemsTotal":"$9.28", | |
"ShippingMethodName":"Express-USD" | |
} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title></title> | |
</head> | |
<body> | |
<table style="width: 900px; margin:50px; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 10pt; line-height: 1.5em;"> | |
<tr> | |
<td> | |
<p>Thank you for your order and for using Quicksilver.</p> | |
<h2>Order ID: {{OrderId}}</h2> | |
Order date: {{Created}} | |
{{#each Shipments}} | |
<table style="width:100%; vertical-align: top; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 10pt; line-height: 1.5em;"> | |
<thead> | |
<tr> | |
<td style="padding: 5px;">Product</td> | |
<td style="text-align: right; padding: 5px;">Quantity</td> | |
<td style="text-align: right; padding: 5px;">Unit price</td> | |
<td style="text-align: right; padding: 5px;">Price</td> | |
<td style="text-align: right; padding: 5px;">Discount</td> | |
<td style="text-align: right; padding: 5px;">Total</td> | |
</tr> | |
</thead> | |
<tbody> | |
{{#each LineItems}} | |
<tr> | |
<td style="border-top: 1px solid #c7c7c7; padding: 5px;"><a href="{{this.Url}}">{{this.DisplayName}}</a></td> | |
<td style="text-align: right; border-top: 1px solid #c7c7c7; padding: 5px;">{{this.Quantity}}</td> | |
<td style="text-align: right; border-top: 1px solid #c7c7c7; padding: 5px;">{{this.UnitPrice}}</td> | |
<td style="text-align: right; border-top: 1px solid #c7c7c7; padding: 5px;">{{this.Price}}</td> | |
<td style="text-align: right; border-top: 1px solid #c7c7c7; padding: 5px;">{{this.Discount}}</td> | |
<td style="text-align: right; border-top: 1px solid #c7c7c7; padding: 5px;">{{this.Total}}</td> | |
</tr> | |
{{/each}} | |
</tbody> | |
</table> | |
<br /> | |
<h4>Shipping details</h4> | |
<table class="table"> | |
<tbody> | |
<tr> | |
<td class="text-right col-md-10">Shipment Cost</td> | |
<td class="total-price text-right col-md-3">{{this.ShipmentCost}}</td> | |
</tr> | |
<tr> | |
<td class="text-right">Shipping Items Total</td> | |
<td class="total-price text-right col-md-3">{{this.ShippingItemsTotal}}</td> | |
</tr> | |
<tr> | |
<td class="text-right">Shipping Method</td> | |
<td class="total-price text-right col-md-3">{{this.ShippingMethodName}}</td> | |
</tr> | |
</tbody> | |
</table> | |
<div class="row"> | |
<div class="col-md-6"> | |
</div> | |
<div class="col-md-6"> | |
<div class="well quicksilver-well"> | |
<h4>Shipping address</h4> | |
<address> | |
<p> | |
{{#if ShippingAddressUK}} | |
{{#if Address.Organization}}<span>{{Address.Organization}}</span><br/>{{/if}} | |
{{#if Address.FirstName}}<span>{{Address.FirstName}} {{Address.LastName}}</span><br/>{{/if}} | |
{{#if Address.Email}}<a href="{{Address.Email}}">{{Address.Email}}</a><br/>{{/if}} | |
{{#if Address.Line1}}<span>{{Address.Line1}}</span><br/>{{/if}} | |
{{#if Address.Line2}}<span>{{Address.Line2}}</span><br/>{{/if}} | |
{{#if Address.City}}<span>{{Address.City}}</span><br/>{{/if}} | |
{{#if Address.CountryRegion.Region}}<span>{{Address.CountryRegion.Region}}</span><br/>{{/if}} | |
{{#if Address.PostalCode}}<span>{{Address.PostalCode}}</span><br/>{{/if}} | |
{{#if Address.CountryName}}<span>{{Address.CountryName}}</span>{{/if}} | |
{{else if ShippingAddressUS}} | |
{{#if Address.Organization}}<span>{{Address.Organization}}</span><br/>{{/if}} | |
{{#if Address.FirstName}}<span>{{Address.FirstName}} {{Address.LastName}}</span><br/>{{/if}} | |
{{#if Address.Email}}<a href="{{Address.Email}}">{{Address.Email}}</a><br/>{{/if}} | |
{{#if Address.Line1}}<span>{{Address.Line1}}</span><br/>{{/if}} | |
{{#if Address.Line2}}<span>{{Address.Line2}}</span><br/>{{/if}} | |
{{#if Address.PostalCode}}<span>{{Address.City}} {{Address.PostalCode}}</span><br/>{{/if}} | |
{{#if Address.CountryRegion.Region}}<span>{{Address.CountryRegion.Region}}</span><br/>{{/if}} | |
{{#if Address.CountryName}}<span>{{Address.CountryName}}</span>{{/if}} | |
{{else}} | |
{{#if Address.Organization}}<span>{{Address.Organization}}</span><br/>{{/if}} | |
{{#if Address.FirstName}}<span>{{Address.FirstName}} {{Address.LastName}}</span><br/>{{/if}} | |
{{#if Address.Email}}<a href="{{Address.Email}}">{{Address.Email}}</a><br/>{{/if}} | |
{{#if Address.Line1}}<span>{{Address.Line1}}</span><br/>{{/if}} | |
{{#if Address.Line2}}<span>{{Address.Line2}}</span><br/>{{/if}} | |
{{#if Address.PostalCode}}<span>{{Address.PostalCode}} {{Address.City}}</span><br/>{{/if}} | |
{{#if Address.CountryRegion.Region}}<span>{{Address.CountryRegion.Region}}</span><br/>{{/if}} | |
{{#if Address.CountryName}}<span>{{Address.CountryName}}</span>{{/if}} | |
{{/if}} | |
</p> | |
</address> | |
</div> | |
</div> | |
</div> | |
{{/each}} | |
<br /> | |
<br /> | |
<br /> | |
<table style="width:100%; vertical-align: top; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 10pt; line-height: 1.5em;"> | |
<tbody> | |
<tr> | |
<td style="width:80%; text-align: right; border-top: 1px solid #c7c7c7; padding: 5px;">Additional discounts</td> | |
<td style="width:20%; text-align: right; border-top: 1px solid #c7c7c7; padding: 5px;">- {{OrderLevelDiscountTotal}}</td> | |
</tr> | |
<tr> | |
<td style="text-align: right; border-top: 1px solid #c7c7c7; padding: 5px;">Handling cost</td> | |
<td style="text-align: right; border-top: 1px solid #c7c7c7; padding: 5px;">{{HandlingTotal}}</td> | |
</tr> | |
<tr> | |
<td style="text-align: right; border-top: 1px solid #c7c7c7; padding: 5px;">Shipping subtotal</td> | |
<td style="text-align: right; border-top: 1px solid #c7c7c7; padding: 5px;">{{ShippingSubTotal}}</td> | |
</tr> | |
<tr> | |
<td style="text-align: right; border-top: 1px solid #c7c7c7; padding: 5px;">Shipping discount</td> | |
<td style="text-align: right; border-top: 1px solid #c7c7c7; padding: 5px;">- {{ShippingDiscountTotal}}</td> | |
</tr> | |
<tr> | |
<td style="text-align: right; border-top: 1px solid #c7c7c7; padding: 5px;">Shipping cost</td> | |
<td style="text-align: right; border-top: 1px solid #c7c7c7; padding: 5px;">{{ShippingTotal}}</td> | |
</tr> | |
<tr> | |
<td style="text-align: right; border-top: 1px solid #c7c7c7; padding: 5px;">Tax cost</td> | |
<td style="text-align: right; border-top: 1px solid #c7c7c7; padding: 5px;">{{TaxTotal}}</td> | |
</tr> | |
<tr> | |
<td style="text-align: right; border-top: 1px solid #c7c7c7; padding: 5px;"><strong>Total</strong></td> | |
<td style="text-align: right; border-top: 1px solid #c7c7c7; padding: 5px;"><strong>{{CartTotal}}</strong></td> | |
</tr> | |
</tbody> | |
</table> | |
<table style="width:100%; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 10pt; line-height: 1.5em;"> | |
<tr> | |
<td style="vertical-align:top; width:50%;"> | |
<h4>Billing details</h4> | |
<address> | |
<p> | |
{{#if BillingAddressUK}} | |
{{#if BillingAddress.Organization}}<span>{{BillingAddress.Organization}}</span><br/>{{/if}} | |
{{#if BillingAddress.FirstName}}<span>{{BillingAddress.FirstName}} {{BillingAddress.LastName}}</span><br/>{{/if}} | |
{{#if BillingAddress.Email}}<a href="{{BillingAddress.Email}}">{{BillingAddress.Email}}</a><br/>{{/if}} | |
{{#if BillingAddress.Line1}}<span>{{BillingAddress.Line1}}</span><br/>{{/if}} | |
{{#if BillingAddress.Line2}}<span>{{BillingAddress.Line2}}</span><br/>{{/if}} | |
{{#if BillingAddress.City}}<span>{{BillingAddress.City}}</span><br/>{{/if}} | |
{{#if BillingAddress.CountryRegion.Region}}<span>{{BillingAddress.CountryRegion.Region}}</span><br/>{{/if}} | |
{{#if BillingAddress.PostalCode}}<span>{{BillingAddress.PostalCode}}</span><br/>{{/if}} | |
{{#if BillingAddress.CountryName}}<span>{{BillingAddress.CountryName}}</span>{{/if}} | |
{{else if BillingAddressUS}} | |
{{#if BillingAddress.Organization}}<span>{{BillingAddress.Organization}}</span><br/>{{/if}} | |
{{#if BillingAddress.FirstName}}<span>{{BillingAddress.FirstName}} {{BillingAddress.LastName}}</span><br/>{{/if}} | |
{{#if BillingAddress.Email}}<a href="{{BillingAddress.Email}}">{{BillingAddress.Email}}</a><br/>{{/if}} | |
{{#if BillingAddress.Line1}}<span>{{BillingAddress.Line1}}</span><br/>{{/if}} | |
{{#if BillingAddress.Line2}}<span>{{BillingAddress.Line2}}</span><br/>{{/if}} | |
{{#if BillingAddress.PostalCode}}<span>{{BillingAddress.City}} {{BillingAddress.PostalCode}}</span><br/>{{/if}} | |
{{#if BillingAddress.CountryRegion.Region}}<span>{{BillingAddress.CountryRegion.Region}}</span><br/>{{/if}} | |
{{#if BillingAddress.CountryName}}<span>{{BillingAddress.CountryName}}</span>{{/if}} | |
{{else}} | |
{{#if BillingAddress.Organization}}<span>{{BillingAddress.Organization}}</span><br/>{{/if}} | |
{{#if BillingAddress.FirstName}}<span>{{BillingAddress.FirstName}} {{BillingAddress.LastName}}</span><br/>{{/if}} | |
{{#if BillingAddress.Email}}<a href="{{BillingAddress.Email}}">{{BillingAddress.Email}}</a><br/>{{/if}} | |
{{#if BillingAddress.Line1}}<span>{{BillingAddress.Line1}}</span><br/>{{/if}} | |
{{#if BillingAddress.Line2}}<span>{{BillingAddress.Line2}}</span><br/>{{/if}} | |
{{#if BillingAddress.PostalCode}}<span>{{BillingAddress.PostalCode}} {{BillingAddress.City}}</span><br/>{{/if}} | |
{{#if BillingAddress.CountryRegion.Region}}<span>{{BillingAddress.CountryRegion.Region}}</span><br/>{{/if}} | |
{{#if BillingAddress.CountryName}}<span>{{BillingAddress.CountryName}}</span>{{/if}} | |
{{/if}} | |
</p> | |
</address> | |
</td> | |
<td style="vertical-align:top; width:50%;"> | |
</td> | |
</tr> | |
</table> | |
</td> | |
</tr> | |
</table> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Threading.Tasks; | |
using SendGrid; | |
using SendGrid.Helpers.Mail; | |
namespace EPiServer.Reference.Commerce.Site.Features.Mail.Services | |
{ | |
public class SendGridMailService : ITemplateMailService | |
{ | |
private readonly ISendGridSettings _settings; | |
private readonly ISendGridClient _sendGridClient; | |
public SendGridMailService(ISendGridSettings settings) | |
{ | |
_settings = settings; | |
_sendGridClient = new SendGridClient(_settings.ApiKey); | |
} | |
public Task SendMail( | |
string templateId, | |
string toMail, | |
string toName, | |
object parameters) | |
{ | |
return SendMail(templateId, null, null, toMail, toName, parameters); | |
} | |
public async Task SendMail( | |
string templateId, | |
string senderMail, | |
string senderName, | |
string toMail, | |
string toName, | |
object parameters) | |
{ | |
var senderAddress = !string.IsNullOrWhiteSpace(senderMail) | |
? new EmailAddress(senderMail, senderName) | |
: new EmailAddress(_settings.DefaultSenderEmail, _settings.DefaultSenderName); | |
var message = new SendGridMessage(); | |
message.AddTo(toMail, toName); | |
message.SetFrom(senderAddress); | |
message.SetTemplateData(parameters); | |
message.SetTemplateId(templateId); | |
Response response = await _sendGridClient.SendEmailAsync(message); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment