aboutsummaryrefslogtreecommitdiff
path: root/Emails.Transactional.Client/EmailTransactionRequest.cs
blob: 1f1a4e2584e221d1488aec2a07957a5e307e555c (plain)
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
using System;
using System.Collections.Generic;
using System.Text.Json.Serialization;

namespace Emails.Transactional.Client
{
    /// <summary>
    /// A transactional email request to send an email 
    /// template.
    /// </summary>
    public class EmailTransactionRequest
    {
        /// <summary>
        /// The transactional send endpoint address
        /// </summary>
        [JsonIgnore]
        public Uri Endpoint { get; init; }
        /// <summary>
        /// A dictionary of email addresses/names of 
        /// users to send this email to
        /// </summary>
        [JsonPropertyName("to")]
        public Dictionary<string, string> ToAddresses { get; set; }
        /// <summary>
        /// A dictionary of email addresses/names of 
        /// users to carbon copy this email to
        /// </summary>
        [JsonPropertyName("cc")]
        public Dictionary<string, string> CcAddresses { get; set; }
        /// <summary>
        /// A dictionary of email addresses/names of 
        /// users to blind carbon copy this email to
        /// </summary>
        [JsonPropertyName("bcc")]
        public Dictionary<string, string> BccAddresses { get; set; }
        /// <summary>
        /// A dictionary of variables to substitute into the liquid 
        /// email template
        /// </summary>
        [JsonPropertyName("variables")]
        public Dictionary<string, string> Variables { get; set; }
        
        /// <summary>
        /// The subject of the email to send
        /// </summary>
        [JsonPropertyName("subject")]
        public string Subject { get; set; }
        
        /// <summary>
        /// The unique id of the email template to send 
        /// </summary>
        [JsonPropertyName("template_id")]
        public string TemplateId { get; set; }
        /// <summary>
        /// The system from email name. NOTE: This is a protected value
        /// </summary>
        [JsonPropertyName("from_name")]
        public string FromName { get; set; }
        /// <summary>
        /// The system from email address. NOTE: This is a protected value
        /// </summary>
        [JsonPropertyName("from_address")]
        public string FromAddress { get; set; }

        /// <summary>
        /// Creates a new email transaction with the specified email template to send
        /// </summary>
        /// <param name="templateId">The id of the template to send</param>
        /// <exception cref="ArgumentNullException"></exception>
        public EmailTransactionRequest(string templateId)
        {
            this.TemplateId = templateId ?? throw new ArgumentNullException(nameof(templateId));
        }
        /// <summary>
        /// Creates a new email transaction with the specified email template 
        /// and a single recipient
        /// </summary>
        /// <param name="templateId">The id of the template to send</param>
        /// <param name="toAddress">A singular recipient name</param>
        /// <param name="toName">A singlular recipient email address</param>
        /// <exception cref="ArgumentNullException"></exception>
        public EmailTransactionRequest(string templateId, string toName, string toAddress)
        {
            this.TemplateId = templateId ?? throw new ArgumentNullException(nameof(templateId));
            AddToAddress(toName, toAddress);
        }
        /// <summary>
        /// Adds a recipient to the To email address dictionary
        /// </summary>
        /// <param name="toName">The name of the user to send the email to</param>
        /// <param name="toAddress">The unique email address of the user to add to the recipient collection</param>
        public void AddToAddress(string toName, string toAddress)
        {
            ToAddresses ??= new(1);
            ToAddresses.Add(toAddress, toName);
        }
        /// <summary>
        /// Adds a recipient to the To email address dictionary
        /// </summary>
        /// <param name="toAddress">The unique email address of the user to add to the recipient collection</param>
        public void AddToAddress(string toAddress)
        {
            string name = toAddress.Split('@')[0];
            AddToAddress(name, toAddress);
        }
        /// <summary>
        /// Adds a carbon copy recipient to the current cc dictionary
        /// </summary>
        /// <param name="ccName">The name of the recipient</param>
        /// <param name="ccAddress">The unique email address of the bcc recipient</param>
        public void AddCcAddress(string ccName, string ccAddress)
        {
            CcAddresses ??= new(1);
            CcAddresses.Add(ccAddress, ccName);
        }
        /// <summary>
        /// Adds a blind carbon copy recipient to the current bcc dictionary
        /// </summary>
        /// <param name="bccName">The name of the recipient</param>
        /// <param name="bccAddress">The unique email address of the bcc recipient</param>
        public void AddBccAddress(string bccName, string bccAddress)
        {
            BccAddresses ??= new(1);
            BccAddresses.Add(bccAddress, bccName);
        }
        /// <summary>
        /// Adds a liquid template variable to be subsituted by the template
        /// renderer.
        /// </summary>
        /// <param name="varName">The unique name of the variable to add to the collection</param>
        /// <param name="varValue">The value if the variable that will be substituted into the template</param>
        public void AddVariable(string varName, string varValue)
        {
            Variables ??= new(1);
            Variables.Add(varName, varValue);
        }
    }
}