Skip to content

Commit

Permalink
add inbox check for port 2525 and 587
Browse files Browse the repository at this point in the history
  • Loading branch information
noogen committed Jan 5, 2021
1 parent 40c4bdf commit 5d09b4d
Showing 1 changed file with 71 additions and 34 deletions.
105 changes: 71 additions & 34 deletions src/Controllers/BaseController.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,45 @@
<?php

namespace App\Controllers;

class BaseController
{
/**
* @param \Base $f3
* @param array $params
*/
public function __construct(\Base $f3, array $params = [])
{
$this->f3 = $f3;
$this->params = $params;
$this->cache = \Cache::instance();
}

/**
* @param $key
* @param $default
* @return mixed
*/
public function getOrDefault($key, $default = null)
{
$rst = $this->f3->get($key);
if (!isset($rst)) {
return $default;
}

return $rst;
}

public function getStorageDir()
{
return trim($this->getOrDefault('STORAGE', '../storage/'), '/') . '/';
}

/**
* Validate if exist is valid.
*
* @param String $email the email to validate
* @param boolean $checkDns true to check for MX record
* @return boolean false if email is not valid
* @return boolean false if email is not valid
*/
public function isValidEmail($email, $checkDns = false)
{
Expand All @@ -35,7 +58,7 @@ public function isValidEmail($email, $checkDns = false)
* Valid if email actually exists at destination email server.
*
* @param String $email this is the email
* @return boolean false means is invalid or does not exists
* @return boolean false means is invalid or does not exists
*/
public function isValidInbox($email)
{
Expand All @@ -53,16 +76,34 @@ public function isValidInbox($email)
$port = 25;
$max_read_time = 5;
$users = $name;
$hosts = array();
$mxweights = array();
$hosts = [];
$mxweights = [];

getmxrr($domain, $hosts, $mxweights);
$mxs = array_combine($hosts, $mxweights);
asort($mxs, SORT_NUMERIC);
$mxs[$domain] = 100;
$timeout = $max_conn_time / count($mxs);

// try to check each host
// try to check each host port 25
while (list($host) = each($mxs)) {
// connect to SMTP server
if ($sock = fsockopen($host, $port, $errno, $errstr, (float) $timeout)) {
stream_set_timeout($sock, $max_read_time);
break;
}
}
// try to check each host port 2525
$port = 2525;
while (list($host) = each($mxs)) {
// connect to SMTP server
if ($sock = fsockopen($host, $port, $errno, $errstr, (float) $timeout)) {
stream_set_timeout($sock, $max_read_time);
break;
}
}
// try to check each host port 587
$port = 587;
while (list($host) = each($mxs)) {
// connect to SMTP server
if ($sock = fsockopen($host, $port, $errno, $errstr, (float) $timeout)) {
Expand All @@ -77,63 +118,59 @@ public function isValidInbox($email)
preg_match('/^([0-9]{3}) /ims', $reply, $matches);
$code = isset($matches[1]) ? $matches[1] : '';

if ($code != '220') {
if ($code !== '220') {
return $result;
}

// initial SMTP connection
$msg = "HELO ".$domain;
fwrite($sock, $msg."\r\n");
$msg = 'HELO ' . $domain;
fwrite($sock, $msg . "\r\n");
$reply = fread($sock, 2082);

/* // check for STARTTLS
if ($port !== 25) {
fwrite($sock, 'STARTTLS');
$reply = fread($sock, 2082);
if (false === stream_socket_enable_crypto($smtp, true, STREAM_CRYPTO_METHOD_TLS_CLIENT)) {
fclose($sock);
return $result;
}
}*/

// sender call
$msg = "MAIL FROM: <".$name.'@'.$domain.">";
fwrite($sock, $msg."\r\n");
$msg = 'MAIL FROM: <' . $name . '@' . $domain . '>';
fwrite($sock, $msg . "\r\n");
$reply = fread($sock, 2082);

// ask to receiver
$msg = "RCPT TO: <".$name.'@'.$domain.">";
fwrite($sock, $msg."\r\n");
$msg = 'RCPT TO: <' . $name . '@' . $domain . '>';
fwrite($sock, $msg . "\r\n");
$reply = fread($sock, 2082);

// get response
preg_match('/^([0-9]{3}) /ims', $reply, $matches);
$code = isset($matches[1]) ? $matches[1] : '';

if ($code == '250') {
if ($code === '250') {
// email address accepted : 250
$result = true;
} elseif ($code == '451' || $code == '452') {
} elseif ($code === '451' || $code === '452') {
//email address greylisted : 451
$result = true;
} else {
$result = false;
}

//quit SMTP connection
$msg = "quit";
fwrite($sock, $msg."\r\n");
$msg = 'quit';
fwrite($sock, $msg . "\r\n");
//close socket
fclose($sock);
}

return $result;
}

public function getStorageDir()
{
return trim($this->getOrDefault('STORAGE', '../storage/'), '/') . '/';
}

public function getOrDefault($key, $default = null)
{
$rst = $this->f3->get($key);
if (!isset($rst)) {
return $default;
}
return $rst;
}

/**
* echo json
* @param object $data
Expand All @@ -145,7 +182,7 @@ public function json($data, array $params = [])
$body = json_encode($data, JSON_PRETTY_PRINT);
$headers = array_key_exists('headers', $params) ? $params['headers'] : [];

// set ttl
// set ttl
$ttl = (int) array_key_exists('ttl', $params) ? $params['ttl'] : -1; // cache for $ttl seconds
if (empty($ttl)) {
$ttl = -1;
Expand All @@ -158,11 +195,11 @@ public function json($data, array $params = [])
'Access-Control-Allow-Methods' =>
array_key_exists('acl_http_methods', $params) ? $params['acl_http_methods'] : null,
'Access-Control-Allow-Origin' =>
array_key_exists('acl_origin', $params) ? $params['acl_origin'] : '*',
array_key_exists('acl_origin', $params) ? $params['acl_origin'] : '*',
'Access-Control-Allow-Credentials' =>
array_key_exists('acl_credentials', $params) && !empty($params['acl_credentials']) ? 'true' : 'false',
'ETag' => array_key_exists('etag', $params) ? $params['etag'] : md5($body),
'Content-Length' => \UTF::instance()->strlen($body),
'Content-Length' => \UTF::instance()->strlen($body)
]);

// send the headers + data
Expand Down

0 comments on commit 5d09b4d

Please sign in to comment.