Что не так с этим диалогом SMTP? - PullRequest
0 голосов
/ 01 мая 2010

Я провожу некоторые тесты с SMTP Gmail, по какой-то причине мой скрипт зависает на этом этапе:

fwrite($smtp, 'DATA' . "\n");
$result[7] = trim(fgets($smtp));
$result[7] = substr($result[7], 0, 3); // 354

// the script starts hanging here
fwrite($smtp, 'From: "Alix Axel" <xxxxxxxx@gmail.com>' . "\n");;
fwrite($smtp, 'To: "Alix Axel" <xxxxxxxx@gmail.com>' . "\n");
fwrite($smtp, 'Date: Tue, 15 May 2010 18:50:00 -0000' . "\n")
fwrite($smtp, 'Subject: Testing SMTP' . "\n");
fwrite($smtp, '' . "\n");
fwrite($smtp, 'Helo SMTP!' . "\n");

fwrite($smtp, "\n.\n" . "\n");
$result[8] = trim(fgets($smtp));
$result[8] = substr($result[8], 0, 3); // 250

Почему это происходит?


Полный диалог SMTP (обновлено):

<code>$smtp = fsockopen('ssl://smtp.gmail.com', 465);
$result = array();

if (is_resource($smtp) === true)
{
    stream_set_timeout($smtp, 1);

    $result[0] = trim(fgets($smtp));
    $result[0] = substr($result[0], 0, 3); // 220

    fwrite($smtp, 'HELO ' . $_SERVER['HTTP_HOST'] . "\r\n");
    $result[1] = trim(fgets($smtp));
    $result[1] = substr($result[1], 0, 3); // 250

    fwrite($smtp, 'AUTH LOGIN' . "\r\n");
    $result[2] = trim(fgets($smtp));
    $result[2] = substr($result[2], 0, 3); // 334

    fwrite($smtp, base64_encode('xxxxxxxx@gmail.com') . "\r\n");
    $result[3] = trim(fgets($smtp));
    $result[3] = substr($result[3], 0, 3); // 334

    fwrite($smtp, base64_encode('xxxxxxxx') . "\r\n");
    $result[4] = trim(fgets($smtp));
    $result[4] = substr($result[4], 0, 3); // 235

    fwrite($smtp, 'MAIL FROM:<xxxxxxxx@gmail.com>' . "\r\n");
    $result[5] = trim(fgets($smtp));
    $result[5] = substr($result[5], 0, 3); // 250

    fwrite($smtp, 'RCPT TO:<xxxxxxxx@gmail.com>' . "\r\n");
    $result[6] = trim(fgets($smtp));
    $result[6] = substr($result[6], 0, 3); // 250

    fwrite($smtp, 'RCPT TO:<yyyyyyyy@gmail.com>' . "\r\n");
    $result[7] = trim(fgets($smtp));
    $result[7] = substr($result[7], 0, 3); // 250

    fwrite($smtp, 'DATA' . "\r\n");
    $result[7] = trim(fgets($smtp));
    $result[7] = substr($result[7], 0, 3); // 354

    fwrite($smtp, 'From: "Alix Axel" <xxxxxxxx@gmail.com>' . "\r\n");
    fwrite($smtp, 'To: "Alix Axel" <xxxxxxxx@gmail.com>' . "\r\n");
    fwrite($smtp, 'Date: Tue, 15 May 2010 18:50:00 -0000' . "\r\n");
    fwrite($smtp, 'Subject: Testing SMTP' . "\r\n");
    fwrite($smtp, '' . "\r\n");
    fwrite($smtp, 'Helo SMTP!' . "\r\n");

    fwrite($smtp, "\r\n" . '.' . "\r\n"); // Is this OK?
    fwrite($smtp, "\r\n");                // Is this OK?
    $result[8] = trim(fgets($smtp));
    $result[8] = substr($result[8], 0, 3); // 250

    fwrite($smtp, 'QUIT' . "\r\n");       // Is this OK?
    $result[9] = trim(fgets($smtp));
    //$result[9] = substr($result[9], 0, 3); // 221
    // 502 5.5.1 Unrecognized command.

    fclose($smtp);
}

echo '<pre>';
print_r($result);
echo '
';

Выход:

Array
(
    [0] => 220
    [1] => 250
    [2] => 334
    [3] => 334
    [4] => 235
    [5] => 250
    [6] => 250
    [7] => 354
    [8] => 250
    [9] => 502 5.5.1 Unrecognized command.
)

Я не совсем уверен, почему отображается код состояния 502.

1 Ответ

2 голосов
/ 01 мая 2010

Исправлено, проблема заключалась в окончании полного останова и окончании строки:

<code>$smtp = fsockopen('ssl://smtp.gmail.com', 465);
$result = array();

if (is_resource($smtp) === true)
{
    stream_set_timeout($smtp, 1);

    $result[0] = trim(fgets($smtp));
    $result[0] = substr($result[0], 0, 3); // 220

    fwrite($smtp, 'HELO ' . $_SERVER['HTTP_HOST'] . "\r\n");
    $result[1] = trim(fgets($smtp));
    $result[1] = substr($result[1], 0, 3); // 250

    fwrite($smtp, 'AUTH LOGIN' . "\r\n");
    $result[2] = trim(fgets($smtp));
    $result[2] = substr($result[2], 0, 3); // 334

    fwrite($smtp, base64_encode('xxxxxxxx@gmail.com') . "\r\n");
    $result[3] = trim(fgets($smtp));
    $result[3] = substr($result[3], 0, 3); // 334

    fwrite($smtp, base64_encode('XXXXXXXX') . "\r\n");
    $result[4] = trim(fgets($smtp));
    $result[4] = substr($result[4], 0, 3); // 235

    fwrite($smtp, 'MAIL FROM:<xxxxxxxx@gmail.com>' . "\r\n");
    $result[5] = trim(fgets($smtp));
    $result[5] = substr($result[5], 0, 3); // 250

    fwrite($smtp, 'RCPT TO:<xxxxxxxx@gmail.com>' . "\r\n");
    $result[6] = trim(fgets($smtp));
    $result[6] = substr($result[6], 0, 3); // 250

    fwrite($smtp, 'RCPT TO:<yyyyyyyy@gmail.com>' . "\r\n");
    $result[7] = trim(fgets($smtp));
    $result[7] = substr($result[7], 0, 3); // 250

    fwrite($smtp, 'DATA' . "\r\n");
    $result[7] = trim(fgets($smtp));
    $result[7] = substr($result[7], 0, 3); // 354

    fwrite($smtp, 'From: "Alix Axel" <xxxxxxxx@gmail.com>' . "\r\n");
    fwrite($smtp, 'To: "Alix Axel" <xxxxxxxx@gmail.com>' . "\r\n");
    fwrite($smtp, 'Date: Tue, 15 May 2010 18:50:00 -0000' . "\r\n");
    fwrite($smtp, 'Subject: Testing SMTP' . "\r\n");
    fwrite($smtp, '' . "\r\n");
    fwrite($smtp, 'Helo SMTP!' . "\r\n");

    fwrite($smtp, "\r\n" . '.' . "\r\n");
    $result[8] = trim(fgets($smtp));
    $result[8] = substr($result[8], 0, 3); // 250

    fwrite($smtp, 'QUIT' . "\r\n");
    $result[9] = trim(fgets($smtp));
    $result[9] = substr($result[9], 0, 3); // 221

    fclose($smtp);
}

echo '<pre>';
print_r($result);
echo '
';

Выход:

Array
(
    [0] => 220
    [1] => 250
    [2] => 334
    [3] => 334
    [4] => 235
    [5] => 250
    [6] => 250
    [7] => 354
    [8] => 250
    [9] => 221
)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...