views:

214

answers:

1

I'm running some tests with the Gmail SMTP, for some reason my script is hanging at this point:

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" <[email protected]>' . "\n");;
fwrite($smtp, 'To: "Alix Axel" <[email protected]>' . "\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

Why is this happening?


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('[email protected]') . "\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:<[email protected]>' . "\r\n");
    $result[5] = trim(fgets($smtp));
    $result[5] = substr($result[5], 0, 3); // 250

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

    fwrite($smtp, 'RCPT TO:<[email protected]>' . "\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" <[email protected]>' . "\r\n");
    fwrite($smtp, 'To: "Alix Axel" <[email protected]>' . "\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.
)

I'm not quite sure why the 502 status code is showing up.

+2  A: 

Fixed it, the problem was the ending full stop and line endings:

$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('[email protected]') . "\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:<[email protected]>' . "\r\n");
    $result[5] = trim(fgets($smtp));
    $result[5] = substr($result[5], 0, 3); // 250

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

    fwrite($smtp, 'RCPT TO:<[email protected]>' . "\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" <[email protected]>' . "\r\n");
    fwrite($smtp, 'To: "Alix Axel" <[email protected]>' . "\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>';

Output:

Array
(
    [0] => 220
    [1] => 250
    [2] => 334
    [3] => 334
    [4] => 235
    [5] => 250
    [6] => 250
    [7] => 354
    [8] => 250
    [9] => 221
)
Alix Axel
Wow, thanks for sharing!
Xeoncross