Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verify connection before register #335

Open
EmmCamp opened this issue Nov 28, 2022 · 4 comments
Open

Verify connection before register #335

EmmCamp opened this issue Nov 28, 2022 · 4 comments
Labels

Comments

@EmmCamp
Copy link

EmmCamp commented Nov 28, 2022

Hello, I'm trying to store IMAP accounts in a database, but I can't find a way to verify that the accounts are real before registering them in the database.

This is the closest I've gotten

(excuse my translator English)

public function store(Request $request)
    {
        $cm = new ClientManager($options = []);
        $account= new accountModel();
        $account->usrID=auth()->id();
        $account->alias=$request->get('alias');
        $account->email=$request->get('email');
        $account->password=$request->get('password');
        $account->email=$request->get('email');

        $domain = explode('@', $account->email);
        $host="";
        switch ($domain) {
            case 'gmail.com':
                $host="imap.gmail.com";
                break;
            case 'outlook.com': case 'hotmail.com':
                $host="imap.gmail.com";
                break;
            default:
                $host="mail.spamanalizer.ibx.lat";
                break;
        }

        $client = $cm->make([
            'host'          => $host,
            'port'          => 993,
            'encryption'    => 'ssl',
            'validate_cert' => true,
            'username'      => $account->email,
            'password'      => $account->password,
            'protocol'      => 'imap'
        ]);
        $account->password = Crypt::encrypt($account->password);
        try {
            $client->connect();
            $account->status="connection successful";
            $client->disconnect();
        } catch (Throwable $th) {
            report($th);
            $account->status="authentication problems";
        }
  
        #echo "<pre>".print_r($account,true)."</pre>";
        $account->save();
        #cho "<pre>".print_r(Crypt::decrypt($account->password),true)."</pre>";
        return redirect('/tables');
    }
@Webklex
Copy link
Owner

Webklex commented Nov 28, 2022

Hi @EmmCamp ,
many thanks for your question.

I don't see an easy way to find the correct domain name and port of any given email address. The port doesn't has to be 993, perhaps encryption is not supported or the service is used under a different root domain.

However you could try to use the associated mx record and hope for default ports and / or some nice internal service forwarding. Some providers use the same domain for smtp and imap communication - if that's the case, the following could provide the information you are looking for:

$domain = "somedomain.com";

dns_get_mx($domain, $dns_get_mx);
getmxrr($domain, $getmxrr);
$dns_get_record = dns_get_record($domain, DNS_MX);

var_dump($dns_get_mx);
var_dump($getmxrr);
var_dump($dns_get_record);

Best regards & happy coding,

@EmmCamp
Copy link
Author

EmmCamp commented Nov 29, 2022

Sorry for not explaining enough.

I just want to see a way to check the authentication of the IMAP user before saving it to the database. since I want to store several emails to process the emails of each account automatically

I greatly appreciate the time you have to help me.

@Webklex
Copy link
Owner

Webklex commented Nov 30, 2022

Ah I see :) In that case, your provide code logic looks fine to me.

Do you receive any exceptions or is the authentication not failing as expected? I'm not sure what kind of issue you are facing, or which behavior isn't working as expected.

Best regards & happy coding,

@EmmCamp
Copy link
Author

EmmCamp commented Nov 30, 2022

image

this exception appears

I just need the "authentication problems" tag to be added if the connection is unsuccessful

or "connection successful" if connected successfully

the second option if it happens, in the first I get the exception

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants