forked from dbhurley/MauticOutlookPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThisAddIn.cs
136 lines (113 loc) · 5.37 KB
/
ThisAddIn.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Security.Policy;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Xml.Linq;
using DamienG.Security.Cryptography;
using Microsoft.Win32;
using Outlook = Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;
namespace MauticOutlookPlugin {
public partial class ThisAddIn {
private void ThisAddIn_Startup(object sender, System.EventArgs e) {
Outlook.Application oOutlook = Globals.ThisAddIn.Application;
oOutlook.OptionsPagesAdd += new Outlook.ApplicationEvents_11_OptionsPagesAddEventHandler(Application_OptionsPagesAdd);
// Event handler to include the tracking gif when sending the email
oOutlook.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
Trackable = false;
// Retrieve the endpoint URL from the registry
try {
var key = Registry.CurrentUser.OpenSubKey("Software");
key = key.OpenSubKey("Mautic");
key = key.OpenSubKey("Outlook Plugin");
EndpointUrl = key.GetValue("Endpoint URL").ToString();
MauticSecret = key.GetValue("Secret").ToString();
} catch (Exception ex) {
//MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private static string Compress(string data) {
using (MemoryStream ms = new MemoryStream()) {
using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true)) {
zip.Write(Encoding.UTF8.GetBytes(data), 0, data.Length);
}
return Convert.ToBase64String(ms.ToArray());
}
}
public string GetSenderAddress(Outlook.MailItem mail)
{
if (mail == null) {
return "";
}
if ((mail.SenderEmailType != "EX")) return mail.SenderEmailAddress;
Outlook.Account acc = mail.SendUsingAccount;
if (acc == null) // use first account
{
Outlook.Accounts accounts = mail.GetInspector.Session.Accounts;
acc = accounts[0];
}
return acc.SmtpAddress;
}
public Stream GenerateStreamFromString(string s) {
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write(s);
writer.Flush();
stream.Position = 0;
return stream;
}
public void Application_ItemSend(object item, ref bool cancel) {
if (!Trackable) return;
var outlookMailtItem = (Outlook.MailItem)item;
// Add the tracking gif to the email body if format is HTML
if (outlookMailtItem.BodyFormat == Outlook.OlBodyFormat.olFormatHTML)
{
var a = "";
foreach (Outlook.Recipient t in outlookMailtItem.Recipients)
{
if (a.Length>0) a += ";";
if (t.AddressEntry.GetExchangeUser() == null)
a += t.Address;
else a += t.AddressEntry.GetExchangeUser().PrimarySmtpAddress;
}
var d = Uri.EscapeDataString(Compress($"from={Uri.EscapeDataString(GetSenderAddress(outlookMailtItem))}&email={Uri.EscapeDataString(a)}&subject={Uri.EscapeDataString(outlookMailtItem.Subject)}&body={Uri.EscapeDataString(outlookMailtItem.Body)}"));
var crc32 = new Crc32();
var hash = String.Empty;
var cr = UnixCrypt.crypt(d, MauticSecret);
using (var s = GenerateStreamFromString(cr))
{
hash = crc32.ComputeHash(s).Aggregate(hash, (current, b) => current + b.ToString("x2").ToLower());
}
var trackingGif = $"<img style=\"display: none;\" height=\"1\" width=\"1\" src=\"{EndpointUrl}/plugin/Outlook/tracking.gif?d={d}&sig={hash}\" alt=\"Mautic is open source marketing automation\">";
outlookMailtItem.HTMLBody = Regex.Replace(outlookMailtItem.HTMLBody, "</body>", trackingGif + "</body>", RegexOptions.IgnoreCase);
}
cancel = false;
}
void Application_OptionsPagesAdd(Outlook.PropertyPages pages) {
pages.Add(new PluginOptionsControl(), "");
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e) {
// Note: Outlook no longer raises this event. If you have code that
// must run when Outlook shuts down, see http://go.microsoft.com/fwlink/?LinkId=506785
}
#region VSTO generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup() {
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
public string EndpointUrl { get; set; }
public string MauticSecret { get; set; }
public bool Trackable { get; set; }
}
}