-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmartEmailingWriter.cs
More file actions
110 lines (100 loc) · 4.08 KB
/
SmartEmailingWriter.cs
File metadata and controls
110 lines (100 loc) · 4.08 KB
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
namespace ZohoSync
{
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Xml.Linq;
/// <summary>
/// smart emailing data writer
/// </summary>
internal class SmartEmailingWriter
{
/// <summary>
/// api url
/// </summary>
private const string API_URL = "https://admin.smartemailing.cz/xml.php";
/// <summary>
/// send data
/// </summary>
/// <param name="list">smart emailing list</param>
/// <param name="data">data</param>
public void SendData(string list, XElement data)
{
Program.OutputWrite("SmartEmailing: building xml request - ");
// build xml header
var root = new XElement("xmlrequest",
new XElement("username", ConfigReader.SmartEmailLogin),
new XElement("usertoken", ConfigReader.SmartEmailToken),
new XElement("requesttype", "sm_lists"),
new XElement("requestmethod", "SynchronizeList"),
new XElement("details",
new XElement("list_name", list),
new XElement("replytoemail", ConfigReader.SmartEmailReplyTo),
new XElement("ownername", ConfigReader.SmartEmailOwnerName),
new XElement("owneremail", ConfigReader.SmartEmailOwnerEmail),
new XElement("notMatchingContacts", "setUnsubscribed"),
new XElement("contacts")));
var contacts = root.Descendants("contacts").First();
// let's build data in
contacts.Add(data.Elements("record")
.Where(e => e.Element("email").Value.Length > 0)
.Select(e => new XElement("details",
new XElement("emailaddress", e.Element("email").Value),
new XElement("format", "html"),
new XElement("confirmed", "yes"),
new XElement("status", "active"),
new XElement("customfields",
new XElement("item",
new XElement("fieldid", "2"),
new XElement("value", e.Element("firstName").Value)),
new XElement("item",
new XElement("fieldid", "3"),
new XElement("value", e.Element("lastName").Value))))));
// convert done
Program.OutputWriteLine("done [" + contacts.Elements().Count() + " records]");
if (Program.WriteFiles)
{
// save it
string file = Path.Combine(Directory.GetCurrentDirectory(), list + ".xml");
root.Save(file);
Program.OutputWriteLine("SmartEmailing: export saved to '" + file + "' done.");
}
Program.OutputWrite("SmartEmailing: submit to server - ");
if (Program.TestOnly)
{
Program.OutputWriteLine("skipped (test only).");
}
else
{
// submit it
var webClient = new SlowWebClient();
webClient.Encoding = Encoding.UTF8;
try
{
var response = webClient.UploadString(API_URL, root.ToString());
// parse
root = XElement.Parse(response);
if (root.Element("status").Value.Equals("success", StringComparison.InvariantCultureIgnoreCase))
{
Program.OutputWriteLine("done.");
}
else
{
Program.OutputWriteLine("failed.");
}
}
catch (WebException ex)
{
Program.OutputWriteLine("failed (" + ex.Message + ")");
}
catch (Exception ex)
{
Program.OutputWriteLine("failed (" + ex.Message + ")");
}
}
}
}
}