aboutsummaryrefslogtreecommitdiff
path: root/lib/Emails.Transactional.Client/src/EmailTransactionRequest.cs
blob: d6b1ebe539cf5d3378b0ccf7d3833b920f1a4b69 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* Copyright (c) 2022 Vaughn Nugent
* 
* Library: VNLib
* Package: Emails.Transactional.Client
* File: EmailTransactionRequest.cs 
*
* EmailTransactionRequest.cs is part of Emails.Transactional.Client which is part of the larger 
* VNLib collection of libraries and utilities.
*
* Emails.Transactional.Client is free software: you can redistribute it and/or modify 
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 2 of the License,
* or (at your option) any later version.
*
* Emails.Transactional.Client is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License 
* along with Emails.Transactional.Client. If not, see http://www.gnu.org/licenses/.
*/

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);
        }
    }
}