Skip to content

add support to mailgun templates #204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,4 @@ ModelManifest.xml
# FAKE - F# Make
.fake/
.vs
/.idea/
73 changes: 61 additions & 12 deletions src/FluentEmail.Core/Email.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ public Email(ITemplateRenderer renderer, ISender sender)
/// </summary>
/// <param name="emailAddress">Email address to send from</param>
/// <param name="name">Name to send from</param>
public Email(string emailAddress, string name = "")
: this(DefaultRenderer, DefaultSender, emailAddress, name) { }
public Email(string emailAddress, string name = "")
: this(DefaultRenderer, DefaultSender, emailAddress, name)
{
}

/// <summary>
/// Creates a new Email instance using the given engines and mailing address.
Expand All @@ -57,6 +59,7 @@ public Email(ITemplateRenderer renderer, ISender sender, string emailAddress, st
{
FromAddress = new Address() {EmailAddress = emailAddress, Name = name}
};

Renderer = renderer;
Sender = sender;
}
Expand Down Expand Up @@ -86,6 +89,7 @@ public static IFluentEmail From(string emailAddress, string name = null)
public IFluentEmail SetFrom(string emailAddress, string name = null)
{
Data.FromAddress = new Address(emailAddress, name ?? "");

return this;
}

Expand All @@ -102,20 +106,24 @@ public IFluentEmail To(string emailAddress, string name = null)
//email address has semi-colon, try split
var nameSplit = name?.Split(';') ?? new string [0];
var addressSplit = emailAddress.Split(';');

for (int i = 0; i < addressSplit.Length; i++)
{
var currentName = string.Empty;

if ((nameSplit.Length - 1) >= i)
{
currentName = nameSplit[i];
}

Data.ToAddresses.Add(new Address(addressSplit[i].Trim(), currentName.Trim()));
}
}
else
{
Data.ToAddresses.Add(new Address(emailAddress.Trim(), name?.Trim()));
}

return this;
}

Expand Down Expand Up @@ -152,6 +160,7 @@ public IFluentEmail To(IList<Address> mailAddresses)
{
Data.ToAddresses.Add(address);
}

return this;
}

Expand All @@ -164,6 +173,7 @@ public IFluentEmail To(IList<Address> mailAddresses)
public IFluentEmail CC(string emailAddress, string name = "")
{
Data.CcAddresses.Add(new Address(emailAddress, name));

return this;
}

Expand All @@ -178,6 +188,7 @@ public IFluentEmail CC(IList<Address> mailAddresses)
{
Data.CcAddresses.Add(address);
}

return this;
}

Expand All @@ -190,6 +201,7 @@ public IFluentEmail CC(IList<Address> mailAddresses)
public IFluentEmail BCC(string emailAddress, string name = "")
{
Data.BccAddresses.Add(new Address(emailAddress, name));

return this;
}

Expand All @@ -204,6 +216,7 @@ public IFluentEmail BCC(IList<Address> mailAddresses)
{
Data.BccAddresses.Add(address);
}

return this;
}

Expand Down Expand Up @@ -240,6 +253,7 @@ public IFluentEmail ReplyTo(string address, string name)
public IFluentEmail Subject(string subject)
{
Data.Subject = subject;

return this;
}

Expand All @@ -252,9 +266,10 @@ public IFluentEmail Body(string body, bool isHtml = false)
{
Data.IsHtml = isHtml;
Data.Body = body;

return this;
}
}

/// <summary>
/// Adds a Plaintext alternative Body to the Email. Used in conjunction with an HTML email,
/// this allows for email readers without html capability, and also helps avoid spam filters.
Expand All @@ -263,6 +278,7 @@ public IFluentEmail Body(string body, bool isHtml = false)
public IFluentEmail PlaintextAlternativeBody(string body)
{
Data.PlaintextAlternativeBody = body;

return this;
}

Expand All @@ -272,6 +288,7 @@ public IFluentEmail PlaintextAlternativeBody(string body)
public IFluentEmail HighPriority()
{
Data.Priority = Priority.High;

return this;
}

Expand All @@ -281,6 +298,7 @@ public IFluentEmail HighPriority()
public IFluentEmail LowPriority()
{
Data.Priority = Priority.Low;

return this;
}

Expand All @@ -290,6 +308,7 @@ public IFluentEmail LowPriority()
public IFluentEmail UsingTemplateEngine(ITemplateRenderer renderer)
{
Renderer = renderer;

return this;
}

Expand Down Expand Up @@ -329,7 +348,6 @@ public IFluentEmail PlaintextAlternativeUsingTemplateFromEmbedded<T>(string path
return this;
}


/// <summary>
/// Adds the template file to the email
/// </summary>
Expand Down Expand Up @@ -385,6 +403,7 @@ public IFluentEmail PlaintextAlternativeUsingTemplateFromFile<T>(string filename
public IFluentEmail UsingCultureTemplateFromFile<T>(string filename, T model, CultureInfo culture, bool isHtml = true)
{
var cultureFile = GetCultureFileName(filename, culture);

return UsingTemplateFromFile(cultureFile, model, isHtml);
}

Expand All @@ -398,6 +417,7 @@ public IFluentEmail UsingCultureTemplateFromFile<T>(string filename, T model, Cu
public IFluentEmail PlaintextAlternativeUsingCultureTemplateFromFile<T>(string filename, T model, CultureInfo culture)
{
var cultureFile = GetCultureFileName(filename, culture);

return PlaintextAlternativeUsingTemplateFromFile(cultureFile, model);
}

Expand Down Expand Up @@ -457,18 +477,22 @@ public IFluentEmail Attach(IList<Attachment> attachments)
{
Data.Attachments.Add(attachment);
}

return this;
}

public IFluentEmail AttachFromFilename(string filename, string contentType = null, string attachmentName = null)
public IFluentEmail AttachFromFilename(string filename, string contentType = null, string attachmentName = null)
{
var stream = File.OpenRead(filename);
Attach(new Attachment()
{
Data = stream,
Filename = attachmentName ?? filename,
ContentType = contentType
});

Attach(
new Attachment()
{
Data = stream,
Filename = attachmentName ?? filename,
ContentType = contentType
}
);

return this;
}
Expand All @@ -492,6 +516,30 @@ public IFluentEmail Header(string header, string body)
return this;
}

public IFluentEmail MailGunTemplate(string mailGunTemplate)
{
Data.MailGunTemplate = mailGunTemplate;

return this;
}

public IFluentEmail MailGunTemplateVar(string key, string value)
{
Data.MailGunTemplateVars.Add(key, value);

return this;
}

public IFluentEmail MailGunTemplateVar(Dictionary<string, string> vars)
{
foreach (var var in vars)
{
Data.MailGunTemplateVars.Add(var.Key, var.Value);
}

return this;
}

/// <summary>
/// Sends email synchronously
/// </summary>
Expand All @@ -512,6 +560,7 @@ private static string GetCultureFileName(string fileName, CultureInfo culture)
var cultureExtension = string.Format("{0}{1}", culture.Name, extension);

var cultureFile = Path.ChangeExtension(fileName, cultureExtension);

if (File.Exists(cultureFile))
return cultureFile;
else
Expand Down
24 changes: 23 additions & 1 deletion src/FluentEmail.Core/IFluentEmail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,5 +241,27 @@ public interface IFluentEmail: IHideObjectMembers
/// <param name="body">value of the header</param>
/// <returns>Instance of the Email class</returns>
IFluentEmail Header(string header, string body);
}

/// <summary>
/// Define mailgun template to use. Use this option, cause ignore body defined.
/// </summary>
/// <param name="mailGunTemplate">Name of template to use.</param>
/// <returns>Instance of the Email class</returns>
IFluentEmail MailGunTemplate(string mailGunTemplate);

/// <summary>
/// Adds mailgun template vars.
/// </summary>
/// <param name="key">Name of template var.</param>
/// <param name="value">Value of template var.</param>
/// <returns>Instance of the Email class</returns>
IFluentEmail MailGunTemplateVar(string key, string value);

/// <summary>
/// Adds mailgun template vars.
/// </summary>
/// <param name="vars">List of template vars.</param>
/// <returns>Instance of the Email class</returns>
IFluentEmail MailGunTemplateVar(Dictionary<string, string> vars);
}
}
6 changes: 6 additions & 0 deletions src/FluentEmail.Core/Models/EmailData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ public class EmailData
public Priority Priority { get; set; }
public List<string> Tags { get; set; }

public string MailGunTemplate { get; set; }

public bool IsHtml { get; set; }
public Dictionary<string, string> Headers { get; set; }

public Dictionary<string, string> MailGunTemplateVars { get; set; }

public EmailData()
{
ToAddresses = new List<Address>();
Expand All @@ -28,6 +32,8 @@ public EmailData()
Attachments = new List<Attachment>();
Tags = new List<string>();
Headers = new Dictionary<string, string>();
MailGunTemplate = string.Empty;
MailGunTemplateVars = new Dictionary<string, string>();
}
}
}
Loading