MandrillEmailManager.cs
2.87 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using WebKit;
public class MandrillEmailManager : Singleton<MandrillEmailManager>
{
[SerializeField]
private string _mandrillKey;
[SerializeField]
private string _mandrilTemplate;
private void Awake()
{
if (MandrillEmailManager.Instance != null && MandrillEmailManager.Instance != this)
{
Destroy(this.gameObject);
return;
}
WebRequest.cacheRequests = true;
}
private const string mandrilUrl = "https://mandrillapp.com/api/1.0/messages/send-template.json";
private Queue<Request> _requests = new Queue<Request>();
public void SendEmail(string emailAddress, Dictionary<string, object> args)
{
var formattedContent = new List<Dictionary<string,object>>();
foreach(KeyValuePair<string,object> kvp in args)
{
var newDict = new Dictionary<string, object>()
{
{ "name", kvp.Key },
{ "content", kvp.Value }
};
formattedContent.Add(newDict);
}
var payload = new Dictionary<string, object>()
{
{ "key", _mandrillKey },
{ "template_name", _mandrilTemplate },
{ "template_content", "" },
{ "async", false },
{
"message", new Dictionary<string,object>()
{
{
"merge_language", "handlebars"
},
{
"to", new List<Dictionary<string, object>>()
{
new Dictionary<string, object>()
{
{ "email", emailAddress },
{ "type", "to" }
}
}
},
{ "global_merge_vars", formattedContent }
}
}
};
print(MiniJSON.Json.Serialize(payload));
//Request req = new Request()
//{
// method = RequestMethod.POST,
// url = mandrilUrl,
// data = payload,
// onComplete = EmailSendComplete
//};
//req.Send();
StartCoroutine(SendMandrillEmail(payload));
}
private IEnumerator SendMandrillEmail(object payload)
{
byte[] data = System.Text.Encoding.UTF8.GetBytes(Newtonsoft.Json.JsonConvert.SerializeObject(payload));
WWW thisReq = new WWW(mandrilUrl, data);
yield return thisReq;
print(thisReq.text);
}
private void EmailSendComplete(Response resp)
{
if(!string.IsNullOrEmpty(resp.error))
Debug.LogError("Error with mail request - " + resp.error);
else
Debug.Log("Email sent " + resp.text);
}
}