Created
March 21, 2011 20:57
-
-
Save joeriks/880205 to your computer and use it in GitHub Desktop.
Helper function to send Mad Mimi promotional email
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@using System.Text | |
@using System.Reflection | |
@functions { | |
private const string Username = "your email"; | |
private const string APIKey = "your api-key"; | |
private const string Sender = "your email"; | |
public static string ToYaml(object o) | |
{ | |
// usage ToYaml(new {key1="name", key2="name"}) | |
// only one dimensional | |
var yaml ="--- \n"; | |
var properties = o.GetType().GetProperties(); | |
foreach (PropertyInfo prop in properties) | |
{ | |
yaml += prop.Name + ": " + prop.GetValue(o, null).ToString() +"\n"; | |
} | |
return yaml; | |
} | |
private static string Post(string url, string properties) | |
{ | |
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); | |
request.Method = "POST"; | |
byte[] byteArray = Encoding.UTF8.GetBytes(properties); | |
request.ContentType = "application/x-www-form-urlencoded"; | |
request.ContentLength = byteArray.Length; | |
Stream dataStream = request.GetRequestStream(); | |
dataStream.Write(byteArray, 0, byteArray.Length); | |
dataStream.Close(); | |
HttpWebResponse response = (HttpWebResponse)request.GetResponse(); | |
string message=""; | |
if (HttpStatusCode.OK == response.StatusCode) | |
{ | |
dataStream = response.GetResponseStream(); | |
StreamReader reader = new StreamReader(dataStream); | |
var ReadToEnd= reader.ReadToEnd(); | |
response.Close(); | |
string statUrl = String.Format("https://madmimi.com/mailers/status/{0}?username={1}&api_key={2}", ReadToEnd, Username, APIKey); | |
return statUrl; | |
} | |
else | |
{ | |
return "Error: " + response.StatusCode.ToString(); | |
} | |
} | |
} | |
} | |
@helper SendPromotionToList(string promotionName, string listName, string subject= "", object keyValuePair=null, string bcc="") | |
{ | |
var url = "https://api.madmimi.com/mailer/to_list"; | |
var s = "username=" + Username + "&"; | |
s += "api_key=" + APIKey + "&"; | |
s += "promotion_name=" + promotionName + "&"; | |
s += "list_name=" + listName + "&"; | |
if (subject!="") { s += "subject=" + subject + "&"; } | |
s += "from=" + Sender + "&"; | |
if (bcc!="") {s += "bcc=" + bcc + "&";} | |
if (keyValuePair!=null) { s += "body=" + ToYaml(keyValuePair); } | |
Post(url, s); | |
} | |
@helper SendPromotionToRecipient(string promotionName, string recipients, string subject= "", object keyValuePair=null, string bcc="") | |
{ | |
var url = "https://api.madmimi.com/mailer"; | |
var s = "username=" + Username + "&"; | |
s += "api_key=" + APIKey + "&"; | |
s += "promotion_name=" + promotionName + "&"; | |
s += "recipients=" + recipients + "&"; | |
if (subject!="") { s += "subject=" + subject + "&"; } | |
s += "from=" + Sender + "&"; | |
if (bcc!="") {s += "bcc=" + bcc + "&";} | |
if (keyValuePair!=null) { s += "body=" + ToYaml(keyValuePair); } | |
Post(url, s); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment