Ne bu SMTP iletişim nesi var?

1 Cevap php

Ben komut dosyası bu noktada asılı nedense, Gmail SMTP ile bazı testler koşuyorum:

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

Bu neden oluyor?


The full SMTP dialog (updated):

$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 '</pre>';

Output:

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.
)

Ben 502 durum kodu gösteriyor neden oldukça emin değilim.

1 Cevap

Düzelttim, sorun biten tam durdurma ve satır sonları oldu:

$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 '</pre>';

Çıktı:

Array
(
    [0] => 220
    [1] => 250
    [2] => 334
    [3] => 334
    [4] => 235
    [5] => 250
    [6] => 250
    [7] => 354
    [8] => 250
    [9] => 221
)