PHP Tool That Auto-Detects IMAP/SMTP Settings

A PHP email automation tool that detects IMAP/SMTP settings, tests protocol combinations in parallel, and speeds up bulk account setup.

2024-10-15

Email Configuration: The Nightmare of IT Teams

Fifty new employees joined the company. Every one of them needs their email client configured. Outlook or Apple Mail? IMAP or POP3? SSL or STARTTLS? What is the server address? Port 993 or 143? Finding the answers to these questions means scanning documentation, calling a support line, or trial and error — and this process is repeated for every single employee.

When 50 new employees join a company, the IT team is in trouble. Manually configuring the email client for each person — tracking down and entering the server address, port number, SSL setting, and authentication method one by one — takes hours. And when you consider that every mail provider uses different combinations, the situation becomes truly unmanageable.

Gmail? imap.gmail.com:993 SSL. Yandex? imap.yandex.com:993. On-premises Exchange? Even the server name can be different. Getting this information requires scanning documentation, calling support lines, or trial and error.

We built a PHP tool that fully automates this process.

Auto-Detection Logic with PHP 8.0+

The tool does the following: it takes an email address, extracts the domain, and then tries the well-known common server configurations for that domain. It tests different combinations until a successful connection is made.

<?php

declare(strict_types=1);

class EmailConfigDetector
{
    private array $imapCandidates = [
        ['host' => 'imap.{domain}', 'port' => 993, 'ssl' => true],
        ['host' => 'imap.{domain}', 'port' => 143, 'ssl' => false],
        ['host' => 'mail.{domain}',  'port' => 993, 'ssl' => true],
        ['host' => 'mail.{domain}',  'port' => 143, 'ssl' => false],
    ];

    private array $smtpCandidates = [
        ['host' => 'smtp.{domain}', 'port' => 587, 'ssl' => 'tls'],
        ['host' => 'smtp.{domain}', 'port' => 465, 'ssl' => 'ssl'],
        ['host' => 'smtp.{domain}', 'port' => 25,  'ssl' => false],
        ['host' => 'mail.{domain}', 'port' => 587, 'ssl' => 'tls'],
    ];

    public function detect(string $email): array
    {
        $domain = substr(strrchr($email, '@'), 1);
        return $this->testCombinations($domain);
    }
}

Testing 8 Protocol Combinations in Parallel

The tool tests a total of 8 different protocol combinations: 4 IMAP + 4 SMTP variants. Testing these sequentially would be far too slow — each connection attempt would wait until it timed out. The solution is parallel testing.

PHP’s pcntl_fork() or fiber (PHP 8.1+) constructs allow concurrent connection tests. For a simpler and more portable approach, we use curl_multi:

private function testCombinations(string $domain): array
{
    $results = [];
    $handles = [];

    foreach ($this->getAllCandidates($domain) as $key => $candidate) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $candidate['url']);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
        curl_setopt($ch, CURLOPT_TIMEOUT, 8);
        $handles[$key] = ['handle' => $ch, 'config' => $candidate];
    }

    $mh = curl_multi_init();
    foreach ($handles as $item) {
        curl_multi_add_handle($mh, $item['handle']);
    }

    // Run all connections concurrently
    do {
        curl_multi_exec($mh, $running);
        curl_multi_select($mh);
    } while ($running > 0);

    return $this->collectResults($handles, $mh);
}

Real-Time Progress Tracking and Logging

For IT staff waiting at their screens during bulk account setup, instant feedback is essential. Every test attempt and its result is reflected on screen immediately:

[2024-10-15 14:23:01] TEST: imap.example.com:993 (SSL) ... SUCCESS ✓
[2024-10-15 14:23:01] TEST: smtp.example.com:587 (TLS) ... SUCCESS ✓
[2024-10-15 14:23:02] TEST: pop3.example.com:995 (SSL) ... TIMEOUT ✗
[2024-10-15 14:23:02] TEST: mail.example.com:143       ... CONNECTION REFUSED ✗

RESULT:
  IMAP: imap.example.com:993 (SSL/TLS)
  SMTP: smtp.example.com:587 (STARTTLS)
  POP3: Not supported

Logs are also written to a file. This is important for the audit trail — which settings were detected for which account and when are all on record.

Custom Provider Database

The tool also contains pre-defined configurations for popular email providers. For providers such as Gmail, Outlook, Yahoo, Yandex, and Apple iCloud, it returns known settings directly without performing a live connection test. This produces results much faster.

private array $knownProviders = [
    'gmail.com' => [
        'imap' => ['host' => 'imap.gmail.com', 'port' => 993, 'ssl' => true],
        'smtp' => ['host' => 'smtp.gmail.com',  'port' => 587, 'tls' => true],
    ],
    'outlook.com' => [
        'imap' => ['host' => 'outlook.office365.com', 'port' => 993, 'ssl' => true],
        'smtp' => ['host' => 'smtp.office365.com',    'port' => 587, 'tls' => true],
    ],
    // ...
];

Time Saved in Bulk Account Setup

Manually configuring a setup of 50 accounts takes an average of 3–5 hours. With this tool, the process works as follows:

  1. Import the email list from CSV
  2. Launch parallel detection for each address
  3. Automatically save successful configurations
  4. Receive a manual review report for any failures

Total time for 50 accounts: 8–12 minutes. The rate requiring human intervention is generally below 5% — only very unusual corporate server configurations require manual handling.

Conclusion

Automating repetitive, error-prone processes like email server configuration frees IT teams to focus on work that actually matters. The modern features of PHP 8.0+ provide a powerful enough foundation for this kind of network-based automation tooling.

Reducing 3–5 hours to 8–12 minutes for 50 accounts is possible with this tool — and these savings are not only realized during the initial setup but recaptured with every new wave of staff and every system migration. This type of IT process automation is a concrete example of the solutions Barlas Dijital provides to SME and enterprise clients: zero licensing cost, custom-built for the company, easy to maintain.

We used a similar command-line automation pattern in BD Reparo for corporate Windows maintenance flows. If you have a similar email automation need or want to accelerate your existing IT processes, let’s talk.