summaryrefslogtreecommitdiff
path: root/Postman/Postman-Configuration/PostmanSmtpDiscovery.php
blob: 90777d5b5fc09c4d3abc4940ee721be06a3ff439 (plain)
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
if (! class_exists ( 'PostmanSmtpMappings' )) {
	class PostmanSmtpMappings {
		// if an email is in this domain array, it is a known smtp server (easy lookup)
		private static $emailDomain = array (
				// from http://www.serversmtp.com/en/outgoing-mail-server-hostname
				'1and1.com' => 'smtp.1and1.com',
				'airmail.net' => 'smtp.airmail.net',
				'aol.com' => 'smtp.aol.com',
				'Bluewin.ch' => 'Smtpauths.bluewin.ch',
				'Comcast.net' => 'Smtp.comcast.net',
				'Earthlink.net' => 'Smtpauth.earthlink.net',
				'gmail.com' => 'smtp.gmail.com',
				'Gmx.com' => 'mail.gmx.com',
				'Gmx.net' => 'mail.gmx.com',
				'Gmx.us' => 'mail.gmx.com',
				'hotmail.com' => 'smtp.live.com',
				'icloud.com' => 'smtp.mail.me.com',
				'mail.com' => 'smtp.mail.com',
				'ntlworld.com' => 'smtp.ntlworld.com',
				'rocketmail.com' => 'plus.smtp.mail.yahoo.com',
				'rogers.com' => 'smtp.broadband.rogers.com',
				'yahoo.ca' => 'smtp.mail.yahoo.ca',
				'yahoo.co.id' => 'smtp.mail.yahoo.co.id',
				'yahoo.co.in' => 'smtp.mail.yahoo.co.in',
				'yahoo.co.kr' => 'smtp.mail.yahoo.com',
				'yahoo.com' => 'smtp.mail.yahoo.com',
				'yahoo.com.ar' => 'smtp.mail.yahoo.com.ar',
				'yahoo.com.au' => 'smtp.mail.yahoo.com.au',
				'yahoo.com.br' => 'smtp.mail.yahoo.com.br',
				'yahoo.com.cn' => 'smtp.mail.yahoo.com.cn',
				'yahoo.com.hk' => 'smtp.mail.yahoo.com.hk',
				'yahoo.com.mx' => 'smtp.mail.yahoo.com',
				'yahoo.com.my' => 'smtp.mail.yahoo.com.my',
				'yahoo.com.ph' => 'smtp.mail.yahoo.com.ph',
				'yahoo.com.sg' => 'smtp.mail.yahoo.com.sg',
				'yahoo.com.tw' => 'smtp.mail.yahoo.com.tw',
				'yahoo.com.vn' => 'smtp.mail.yahoo.com.vn',
				'yahoo.co.nz' => 'smtp.mail.yahoo.com.au',
				'yahoo.co.th' => 'smtp.mail.yahoo.co.th',
				'yahoo.co.uk' => 'smtp.mail.yahoo.co.uk',
				'yahoo.de' => 'smtp.mail.yahoo.de',
				'yahoo.es' => 'smtp.correo.yahoo.es',
				'yahoo.fr' => 'smtp.mail.yahoo.fr',
				'yahoo.ie' => 'smtp.mail.yahoo.co.uk',
				'yahoo.it' => 'smtp.mail.yahoo.it',
				'zoho.com' => 'smtp.zoho.com',
				// from http://www.att.com/esupport/article.jsp?sid=KB401570&cv=801
				'ameritech.net' => 'outbound.att.net',
				'att.net' => 'outbound.att.net',
				'bellsouth.net' => 'outbound.att.net',
				'flash.net' => 'outbound.att.net',
				'nvbell.net' => 'outbound.att.net',
				'pacbell.net' => 'outbound.att.net',
				'prodigy.net' => 'outbound.att.net',
				'sbcglobal.net' => 'outbound.att.net',
				'snet.net' => 'outbound.att.net',
				'swbell.net' => 'outbound.att.net',
				'wans.net' => 'outbound.att.net' 
		);
		// if an email's mx is in this domain array, it is a known smtp server (dns lookup)
		// useful for custom domains that map to a mail service
		private static $mxMappings = array (
				'1and1help.com' => 'smtp.1and1.com',
				'google.com' => 'smtp.gmail.com',
				'Gmx.net' => 'mail.gmx.com',
				'icloud.com' => 'smtp.mail.me.com',
				'hotmail.com' => 'smtp.live.com',
				'mx-eu.mail.am0.yahoodns.net' => 'smtp.mail.yahoo.com',
				// 'mail.protection.outlook.com' => 'smtp.office365.com',
				// 'mail.eo.outlook.com' => 'smtp.office365.com',
				'outlook.com' => 'smtp.office365.com',
				'biz.mail.am0.yahoodns.net' => 'smtp.bizmail.yahoo.com',
				'BIZ.MAIL.YAHOO.com' => 'smtp.bizmail.yahoo.com',
				'hushmail.com' => 'smtp.hushmail.com',
				'gmx.net' => 'mail.gmx.com',
				'mandrillapp.com' => 'smtp.mandrillapp.com',
				'smtp.secureserver.net' => 'relay-hosting.secureserver.net',
				'presmtp.ex1.secureserver.net' => 'smtp.ex1.secureserver.net',
				'presmtp.ex2.secureserver.net' => 'smtp.ex2.secureserver.net',
				'presmtp.ex3.secureserver.net' => 'smtp.ex2.secureserver.net',
				'presmtp.ex4.secureserver.net' => 'smtp.ex2.secureserver.net',
				'htvhosting.com' => 'mail.htvhosting.com' 
		);
		public static function getSmtpFromEmail($hostname) {
			reset ( PostmanSmtpMappings::$emailDomain );
			foreach ( PostmanSmtpMappings::$emailDomain as $domain => $smtp ) {
				if (strcasecmp ( $hostname, $domain ) == 0) {
					return $smtp;
				}
			}
			return false;
		}
		public static function getSmtpFromMx($mx) {
			reset ( PostmanSmtpMappings::$mxMappings );
            foreach ( PostmanSmtpMappings::$mxMappings as $domain => $smtp ) {
				if (PostmanUtils::endswith ( $mx, $domain )) {
					return $smtp;
				}
			}
			return false;
		}
	}
}
if (! class_exists ( 'PostmanSmtpDiscovery' )) {
	class PostmanSmtpDiscovery {
		
		// private instance variables
		public $isGoogle;
		public $isGoDaddy;
		public $isWellKnownDomain;
		private $smtpServer;
		private $primaryMx;
		private $email;
		private $domain;
		
		/**
		 * Constructor
		 *
		 * @param mixed $email        	
		 */
		public function __construct($email) {
			$this->email = $email;
			$this->determineSmtpServer ( $email );
			$this->isGoogle = $this->smtpServer == 'smtp.gmail.com';
			$this->isGoDaddy = $this->smtpServer == 'relay-hosting.secureserver.net';
		}
		/**
		 * The SMTP server we suggest to use - this is determined
		 * by looking up the MX hosts for the domain.
		 */
		public function getSmtpServer() {
			return $this->smtpServer;
		}
		public function getPrimaryMx() {
			return $this->primaryMx;
		}
		/**
		 *
		 * @param mixed $email        	
		 * @return Ambigous <number, boolean>
		 */
		private function validateEmail($email) {
			return PostmanUtils::validateEmail ( $email );
		}
		private function determineSmtpServer($email) {
			$hostname = substr ( strrchr ( $email, "@" ), 1 );
			$this->domain = $hostname;
			$smtp = PostmanSmtpMappings::getSmtpFromEmail ( $hostname );
			if ($smtp) {
				$this->smtpServer = $smtp;
				$this->isWellKnownDomain = true;
				return true;
			} else {
				$host = strtolower ( $this->findMxHostViaDns ( $hostname ) );
				if ($host) {
					$this->primaryMx = $host;
					$smtp = PostmanSmtpMappings::getSmtpFromMx ( $host );
					if ($smtp) {
						$this->smtpServer = $smtp;
						return true;
					} else {
						return false;
					}
				} else {
					return false;
				}
			}
		}
		
		/**
		 * Uses getmxrr to retrieve the MX records of a hostname
		 *
		 * @param mixed $hostname        	
		 * @return mixed|boolean
		 */
		private function findMxHostViaDns($hostname) {
			if (function_exists ( 'getmxrr' )) {
				$b_mx_avail = getmxrr ( $hostname, $mx_records, $mx_weight );
			} else {
				$b_mx_avail = $this->getmxrr ( $hostname, $mx_records, $mx_weight );
			}
			if ($b_mx_avail && sizeof ( $mx_records ) > 0) {
				// copy mx records and weight into array $mxs
				$mxs = array ();
				
				for($i = 0; $i < count ( $mx_records ); $i ++) {
					$mxs [$mx_weight [$i]] = $mx_records [$i];
				}
				
				// sort array mxs to get servers with highest prio
				ksort ( $mxs, SORT_NUMERIC );
				reset ( $mxs );
				$mxs_vals = array_values ( $mxs );
				return array_shift ( $mxs_vals );
			} else {
				return false;
			}
		}
		/**
		 * This is a custom implementation of mxrr for Windows PHP installations
		 * which don't have this method natively.
		 *
		 * @param mixed $hostname        	
		 * @param mixed $mxhosts        	
		 * @param mixed $mxweight        	
		 * @return boolean
		 */
		function getmxrr($hostname, &$mxhosts, &$mxweight) {
			if (! is_array ( $mxhosts )) {
				$mxhosts = array ();
			}
			$hostname = escapeshellarg ( $hostname );
			if (! empty ( $hostname )) {
				$output = "";
				@exec ( "nslookup.exe -type=MX $hostname.", $output );
				$imx = - 1;
				
				foreach ( $output as $line ) {
					$imx ++;
					$parts = "";
					if (preg_match ( "/^$hostname\tMX preference = ([0-9]+), mail exchanger = (.*)$/", $line, $parts )) {
						$mxweight [$imx] = $parts [1];
						$mxhosts [$imx] = $parts [2];
					}
				}
				return ($imx != - 1);
			}
			return false;
		}
	}
}