1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using System . Globalization ;
4
+ using System . Linq ;
5
+ using System . Net . Mail ;
6
+ using System . Text ;
7
+ using System . Text . RegularExpressions ;
8
+ using System . Web ;
9
+ using DotNetNuke . Entities . Host ;
10
+ using DotNetNuke . Entities . Portals ;
11
+ using DotNetNuke . Entities . Users ;
12
+ using DotNetNuke . Services . Mail ;
13
+ using Newtonsoft . Json . Linq ;
14
+
15
+ namespace Satrabel . OpenForm . Components
16
+ {
17
+ public static class FormUtils
18
+ {
19
+ internal static MailAddress GenerateMailAddress ( string typeOfAddress , string email , string name , string formEmailField , string formNameField , JObject form )
20
+ {
21
+ MailAddress adr = null ;
22
+ var portalSettings = PortalSettings . Current ;
23
+
24
+ if ( typeOfAddress == "host" )
25
+ {
26
+ adr = GenerateMailAddress ( Host . HostEmail , Host . HostTitle ) ;
27
+ }
28
+ else if ( typeOfAddress == "admin" )
29
+ {
30
+ var user = UserController . GetUserById ( portalSettings . PortalId , portalSettings . AdministratorId ) ;
31
+ adr = GenerateMailAddress ( user . Email , user . DisplayName ) ;
32
+ }
33
+ else if ( typeOfAddress == "form" )
34
+ {
35
+ if ( string . IsNullOrEmpty ( formNameField ) )
36
+ formNameField = "name" ;
37
+ if ( string . IsNullOrEmpty ( formEmailField ) )
38
+ formEmailField = "email" ;
39
+
40
+ string formEmail = GetProperty ( form , formEmailField ) ;
41
+ string formName = GetProperty ( form , formNameField ) ;
42
+ adr = GenerateMailAddress ( formEmail , formName ) ;
43
+ }
44
+ else if ( typeOfAddress == "custom" )
45
+ {
46
+ adr = GenerateMailAddress ( email , name ) ;
47
+ }
48
+ else if ( typeOfAddress == "current" )
49
+ {
50
+ var userInfo = portalSettings . UserInfo ;
51
+ if ( userInfo == null )
52
+ throw new Exception ( $ "Can't send email to current user, as there is no current user. Parameters were TypeOfAddress: [{ typeOfAddress } ], Email: [{ email } ], Name: [{ name } ], FormEmailField: [{ formEmailField } ], FormNameField: [{ formNameField } ], FormNameField: [{ form } ]") ;
53
+
54
+ adr = GenerateMailAddress ( userInfo . Email , userInfo . DisplayName ) ;
55
+ if ( adr == null )
56
+ throw new Exception ( $ "Can't send email to current user, as email address of current user is unknown. Parameters were TypeOfAddress: [{ typeOfAddress } ], Email: [{ email } ], Name: [{ name } ], FormEmailField: [{ formEmailField } ], FormNameField: [{ formNameField } ], FormNameField: [{ form } ]") ;
57
+ }
58
+
59
+ if ( adr == null )
60
+ {
61
+ throw new Exception ( $ "Can't determine email address. Parameters were TypeOfAddress: [{ typeOfAddress } ], Email: [{ email } ], Name: [{ name } ], FormEmailField: [{ formEmailField } ], FormNameField: [{ formNameField } ], FormNameField: [{ form } ]") ;
62
+ }
63
+
64
+ return adr ;
65
+ }
66
+
67
+ private static MailAddress GenerateMailAddress ( string email , string title )
68
+ {
69
+ email = email . Trim ( ) ; //normalize email
70
+
71
+ return IsValidEmail ( email ) ? new MailAddress ( email , title ) : null ;
72
+ }
73
+
74
+ private static string GetProperty ( JObject obj , string propertyName )
75
+ {
76
+ string propertyValue = "" ;
77
+ var property = obj . Children < JProperty > ( ) . SingleOrDefault ( p => p . Name . ToLower ( ) == propertyName . ToLower ( ) ) ;
78
+ if ( property != null )
79
+ {
80
+ propertyValue = property . Value . ToString ( ) ;
81
+ }
82
+ return propertyValue ;
83
+ }
84
+
85
+ /// <summary>
86
+ /// Determines whether email is valid.
87
+ /// </summary>
88
+ /// <remarks>
89
+ /// https://technet.microsoft.com/nl-be/library/01escwtf(v=vs.110).aspx
90
+ /// </remarks>
91
+ public static bool IsValidEmail ( string strIn )
92
+ {
93
+ if ( string . IsNullOrEmpty ( strIn ) ) return false ;
94
+
95
+ bool invalid = false ;
96
+
97
+ // Use IdnMapping class to convert Unicode domain names.
98
+ try
99
+ {
100
+ strIn = Regex . Replace ( strIn , @"(@)(.+)$" , DomainMapper ) ;
101
+ }
102
+ catch ( Exception e )
103
+ {
104
+ invalid = true ;
105
+ }
106
+
107
+ if ( invalid )
108
+ return false ;
109
+
110
+ // Return true if strIn is in valid e-mail format.
111
+ return Regex . IsMatch ( strIn ,
112
+ @"^(?("")(""[^""]+?""@)|(([0-9a-z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-z])@))" +
113
+ @"(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-z][-\w]*[0-9a-z]*\.)+[a-z0-9]{2,17}))$" ,
114
+ RegexOptions . IgnoreCase ) ;
115
+ }
116
+
117
+ private static string DomainMapper ( Match match )
118
+ {
119
+ // IdnMapping class with default property values.
120
+ IdnMapping idn = new IdnMapping ( ) ;
121
+ string domainName = match . Groups [ 2 ] . Value ;
122
+ domainName = idn . GetAscii ( domainName ) ;
123
+ return match . Groups [ 1 ] . Value + domainName ;
124
+ }
125
+ public static string SendMail ( string mailFrom , string mailTo , string replyTo , string subject , string body , List < Attachment > attachments = null )
126
+ {
127
+ //string mailFrom
128
+ //string mailTo,
129
+ string cc = "" ;
130
+ string bcc = "" ;
131
+ //string replyTo,
132
+ DotNetNuke . Services . Mail . MailPriority priority = DotNetNuke . Services . Mail . MailPriority . Normal ;
133
+ //string subject,
134
+ MailFormat bodyFormat = MailFormat . Html ;
135
+ Encoding bodyEncoding = Encoding . UTF8 ;
136
+ //string body,
137
+ //List<Attachment> attachments = new List<Attachment>();
138
+ if ( attachments == null ) attachments = new List < Attachment > ( ) ;
139
+ string smtpServer = Host . SMTPServer ;
140
+ string smtpAuthentication = Host . SMTPAuthentication ;
141
+ string smtpUsername = Host . SMTPUsername ;
142
+ string smtpPassword = Host . SMTPPassword ;
143
+ bool smtpEnableSSL = Host . EnableSMTPSSL ;
144
+
145
+ string res = Mail . SendMail ( mailFrom ,
146
+ mailTo ,
147
+ cc ,
148
+ bcc ,
149
+ replyTo ,
150
+ priority ,
151
+ subject ,
152
+ bodyFormat ,
153
+ bodyEncoding ,
154
+ body ,
155
+ attachments ,
156
+ smtpServer ,
157
+ smtpAuthentication ,
158
+ smtpUsername ,
159
+ smtpPassword ,
160
+ smtpEnableSSL ) ;
161
+
162
+ //Mail.SendEmail(replyTo, mailFrom, mailTo, subject, body);
163
+ return res ;
164
+ }
165
+ }
166
+
167
+ }
0 commit comments