forked from lukencode/FluentEmail
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathEmail.cs
520 lines (461 loc) · 18.9 KB
/
Email.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using FluentEmail.Core.Defaults;
using FluentEmail.Core.Interfaces;
using FluentEmail.Core.Models;
namespace FluentEmail.Core
{
public class Email : IFluentEmail
{
public EmailData Data { get; set; }
public ITemplateRenderer Renderer { get; set; }
public ISender Sender { get; set; }
public static ITemplateRenderer DefaultRenderer = new ReplaceRenderer();
public static ISender DefaultSender = new SaveToDiskSender("/");
/// <summary>
/// Creates a new email instance with default settings.
/// </summary>
public Email() : this(DefaultRenderer, DefaultSender) { }
/// <summary>
/// Creates a new email instance with overrides for the rendering and sending engines.
/// </summary>
/// <param name="renderer">The template rendering engine</param>
/// <param name="sender">The email sending implementation</param>
public Email(ITemplateRenderer renderer, ISender sender)
: this(renderer, sender, null, null)
{
}
/// <summary>
/// Creates a new Email instance with default settings, from a specific mailing address.
/// </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) { }
/// <summary>
/// Creates a new Email instance using the given engines and mailing address.
/// </summary>
/// <param name="renderer">The template rendering engine</param>
/// <param name="sender">The email sending implementation</param>
/// <param name="emailAddress">Email address to send from</param>
/// <param name="name">Name to send from</param>
public Email(ITemplateRenderer renderer, ISender sender, string emailAddress, string name = "")
{
Data = new EmailData
{
FromAddress = new Address {EmailAddress = emailAddress, Name = name}
};
Renderer = renderer;
Sender = sender;
}
/// <summary>
/// Creates a new Email instance and sets the from property
/// </summary>
/// <param name="emailAddress">Email address to send from</param>
/// <param name="name">Name to send from</param>
/// <returns>Instance of the Email class</returns>
public static IFluentEmail From(string emailAddress, string name = null)
{
var email = new Email
{
Data = {FromAddress = new Address(emailAddress, name ?? "")}
};
return email;
}
/// <summary>
/// Set the send from email address
/// </summary>
/// <param name="emailAddress">Email address of sender</param>
/// <param name="name">Name of sender</param>
/// <returns>Instance of the Email class</returns>
public IFluentEmail SetFrom(string emailAddress, string name = null)
{
Data.FromAddress = new Address(emailAddress, name ?? "");
return this;
}
/// <summary>
/// Adds a recipient to the email, Splits name and address on ';'
/// </summary>
/// <param name="emailAddress">Email address of recipient</param>
/// <param name="name">Name of recipient</param>
/// <returns>Instance of the Email class</returns>
public IFluentEmail To(string emailAddress, string name = null)
{
if (emailAddress.Contains(";"))
{
//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;
}
/// <summary>
/// Adds a recipient to the email
/// </summary>
/// <param name="emailAddress">Email address of recipient (allows multiple splitting on ';')</param>
/// <returns></returns>
public IFluentEmail To(string emailAddress)
{
if (emailAddress.Contains(";"))
{
foreach (string address in emailAddress.Split(';'))
{
Data.ToAddresses.Add(new Address(address));
}
}
else
{
Data.ToAddresses.Add(new Address(emailAddress));
}
return this;
}
/// <summary>
/// Adds all recipients in list to email
/// </summary>
/// <param name="mailAddresses">List of recipients</param>
/// <returns>Instance of the Email class</returns>
public IFluentEmail To(IEnumerable<Address> mailAddresses)
{
foreach (var address in mailAddresses)
{
Data.ToAddresses.Add(address);
}
return this;
}
/// <summary>
/// Adds a Carbon Copy to the email
/// </summary>
/// <param name="emailAddress">Email address to cc</param>
/// <param name="name">Name to cc</param>
/// <returns>Instance of the Email class</returns>
public IFluentEmail CC(string emailAddress, string name = "")
{
Data.CcAddresses.Add(new Address(emailAddress, name));
return this;
}
/// <summary>
/// Adds all Carbon Copy in list to an email
/// </summary>
/// <param name="mailAddresses">List of recipients to CC</param>
/// <returns>Instance of the Email class</returns>
public IFluentEmail CC(IEnumerable<Address> mailAddresses)
{
foreach (var address in mailAddresses)
{
Data.CcAddresses.Add(address);
}
return this;
}
/// <summary>
/// Adds a blind carbon copy to the email
/// </summary>
/// <param name="emailAddress">Email address of bcc</param>
/// <param name="name">Name of bcc</param>
/// <returns>Instance of the Email class</returns>
public IFluentEmail BCC(string emailAddress, string name = "")
{
Data.BccAddresses.Add(new Address(emailAddress, name));
return this;
}
/// <summary>
/// Adds all blind carbon copy in list to an email
/// </summary>
/// <param name="mailAddresses">List of recipients to BCC</param>
/// <returns>Instance of the Email class</returns>
public IFluentEmail BCC(IEnumerable<Address> mailAddresses)
{
foreach (var address in mailAddresses)
{
Data.BccAddresses.Add(address);
}
return this;
}
/// <summary>
/// Sets the ReplyTo address on the email
/// </summary>
/// <param name="address">The ReplyTo Address</param>
/// <returns></returns>
public IFluentEmail ReplyTo(string address)
{
Data.ReplyToAddresses.Add(new Address(address));
return this;
}
/// <summary>
/// Sets the ReplyTo address on the email
/// </summary>
/// <param name="address">The ReplyTo Address</param>
/// <param name="name">The Display Name of the ReplyTo</param>
/// <returns></returns>
public IFluentEmail ReplyTo(string address, string name)
{
Data.ReplyToAddresses.Add(new Address(address, name));
return this;
}
/// <summary>
/// Sets the subject of the email
/// </summary>
/// <param name="subject">email subject</param>
/// <returns>Instance of the Email class</returns>
public IFluentEmail Subject(string subject)
{
Data.Subject = subject;
return this;
}
/// <summary>
/// Adds a Body to the Email
/// </summary>
/// <param name="body">The content of the body</param>
/// <param name="isHtml">True if Body is HTML, false for plain text (default)</param>
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.
/// </summary>
/// <param name="body">The content of the body</param>
public IFluentEmail PlaintextAlternativeBody(string body)
{
Data.PlaintextAlternativeBody = body;
return this;
}
/// <summary>
/// Marks the email as High Priority
/// </summary>
public IFluentEmail HighPriority()
{
Data.Priority = Priority.High;
return this;
}
/// <summary>
/// Marks the email as Low Priority
/// </summary>
public IFluentEmail LowPriority()
{
Data.Priority = Priority.Low;
return this;
}
/// <summary>
/// Set the template rendering engine to use, defaults to RazorEngine
/// </summary>
public IFluentEmail UsingTemplateEngine(ITemplateRenderer renderer)
{
Renderer = renderer;
return this;
}
/// <summary>
/// Adds template to email from embedded resource
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="path">Path the the embedded resource eg [YourAssembly].[YourResourceFolder].[YourFilename.txt]</param>
/// <param name="model">Model for the template</param>
/// <param name="assembly">The assembly your resource is in. Defaults to calling assembly.</param>
/// <param name="isHtml">True if Body is HTML (default), false for plain text</param>
/// <returns></returns>
public IFluentEmail UsingTemplateFromEmbedded<T>(string path, T model, Assembly assembly, bool isHtml = true)
{
var template = EmbeddedResourceHelper.GetResourceAsString(assembly, path);
var result = Renderer.Parse(template, model, isHtml);
Data.IsHtml = isHtml;
Data.Body = result;
return this;
}
/// <summary>
/// Adds template to email from embedded resource
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="path">Path the the embedded resource eg [YourAssembly].[YourResourceFolder].[YourFilename.txt]</param>
/// <param name="model">Model for the template</param>
/// <param name="assembly">The assembly your resource is in. Defaults to calling assembly.</param>
/// <returns></returns>
public IFluentEmail PlaintextAlternativeUsingTemplateFromEmbedded<T>(string path, T model, Assembly assembly)
{
var template = EmbeddedResourceHelper.GetResourceAsString(assembly, path);
var result = Renderer.Parse(template, model, false);
Data.PlaintextAlternativeBody = result;
return this;
}
/// <summary>
/// Adds the template file to the email
/// </summary>
/// <param name="filename">The path to the file to load</param>
/// <param name="model">Model for the template</param>
/// <param name="isHtml">True if Body is HTML (default), false for plain text</param>
/// <returns>Instance of the Email class</returns>
public IFluentEmail UsingTemplateFromFile<T>(string filename, T model, bool isHtml = true)
{
var template = "";
using (var reader = new StreamReader(File.OpenRead(filename)))
{
template = reader.ReadToEnd();
}
var result = Renderer.Parse(template, model, isHtml);
Data.IsHtml = isHtml;
Data.Body = result;
return this;
}
/// <summary>
/// Adds the template file to the email
/// </summary>
/// <param name="filename">The path to the file to load</param>
/// <param name="model">Model for the template</param>
/// <returns>Instance of the Email class</returns>
public IFluentEmail PlaintextAlternativeUsingTemplateFromFile<T>(string filename, T model)
{
var template = "";
using (var reader = new StreamReader(File.OpenRead(filename)))
{
template = reader.ReadToEnd();
}
var result = Renderer.Parse(template, model, false);
Data.PlaintextAlternativeBody = result;
return this;
}
/// <summary>
/// Adds a culture specific template file to the email
/// </summary>
/// <param name="filename">The path to the file to load</param>
/// /// <param name="model">The razor model</param>
/// <param name="culture">The culture of the template (Default is the current culture)</param>
/// <param name="isHtml">True if Body is HTML (default), false for plain text</param>
/// <returns>Instance of the Email class</returns>
public IFluentEmail UsingCultureTemplateFromFile<T>(string filename, T model, CultureInfo culture, bool isHtml = true)
{
var cultureFile = GetCultureFileName(filename, culture);
return UsingTemplateFromFile(cultureFile, model, isHtml);
}
/// <summary>
/// Adds a culture specific template file to the email
/// </summary>
/// <param name="filename">The path to the file to load</param>
/// /// <param name="model">The razor model</param>
/// <param name="culture">The culture of the template (Default is the current culture)</param>
/// <returns>Instance of the Email class</returns>
public IFluentEmail PlaintextAlternativeUsingCultureTemplateFromFile<T>(string filename, T model, CultureInfo culture)
{
var cultureFile = GetCultureFileName(filename, culture);
return PlaintextAlternativeUsingTemplateFromFile(cultureFile, model);
}
/// <summary>
/// Adds razor template to the email
/// </summary>
/// <param name="template">The razor template</param>
/// <param name="model">Model for the template</param>
/// <param name="isHtml">True if Body is HTML, false for plain text (Optional)</param>
/// <returns>Instance of the Email class</returns>
public IFluentEmail UsingTemplate<T>(string template, T model, bool isHtml = true)
{
var result = Renderer.Parse(template, model, isHtml);
Data.IsHtml = isHtml;
Data.Body = result;
return this;
}
/// <summary>
/// Adds razor template to the email
/// </summary>
/// <param name="template">The razor template</param>
/// <param name="model">Model for the template</param>
/// <returns>Instance of the Email class</returns>
public IFluentEmail PlaintextAlternativeUsingTemplate<T>(string template, T model)
{
var result = Renderer.Parse(template, model, false);
Data.PlaintextAlternativeBody = result;
return this;
}
/// <summary>
/// Adds an Attachment to the Email
/// </summary>
/// <param name="attachment">The Attachment to add</param>
/// <returns>Instance of the Email class</returns>
public IFluentEmail Attach(Attachment attachment)
{
if (!Data.Attachments.Contains(attachment))
{
Data.Attachments.Add(attachment);
}
return this;
}
/// <summary>
/// Adds Multiple Attachments to the Email
/// </summary>
/// <param name="attachments">The List of Attachments to add</param>
/// <returns>Instance of the Email class</returns>
public IFluentEmail Attach(IEnumerable<Attachment> attachments)
{
foreach (var attachment in attachments.Where(attachment => !Data.Attachments.Contains(attachment)))
{
Data.Attachments.Add(attachment);
}
return this;
}
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
});
return this;
}
/// <summary>
/// Adds tag to the Email. This is currently only supported by the Mailgun provider. <see href="https://documentation.mailgun.com/en/latest/user_manual.html#tagging"/>
/// </summary>
/// <param name="tag">Tag name, max 128 characters, ASCII only</param>
/// <returns>Instance of the Email class</returns>
public IFluentEmail Tag(string tag)
{
Data.Tags.Add(tag);
return this;
}
public IFluentEmail Header(string header, string body)
{
Data.Headers.Add(header, body);
return this;
}
/// <summary>
/// Sends email synchronously
/// </summary>
/// <returns>Instance of the Email class</returns>
public virtual SendResponse Send(CancellationToken? token = null)
{
return Sender.Send(this, token);
}
public virtual Task<SendResponse> SendAsync(CancellationToken? token = null)
{
return Sender.SendAsync(this, token);
}
private static string GetCultureFileName(string fileName, CultureInfo culture)
{
var extension = Path.GetExtension(fileName);
var cultureExtension = string.Format("{0}{1}", culture.Name, extension);
var cultureFile = Path.ChangeExtension(fileName, cultureExtension);
if (File.Exists(cultureFile))
return cultureFile;
else
return fileName;
}
}
}