Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 755041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:05:00+00:00 2026-05-14T15:05:00+00:00

I’m running some tests with the Gmail SMTP, for some reason my script is

  • 0

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" <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

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

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:05:00+00:00Added an answer on May 14, 2026 at 3:05 pm

    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('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>';
    

    Output:

    Array
    (
        [0] => 220
        [1] => 250
        [2] => 334
        [3] => 334
        [4] => 235
        [5] => 250
        [6] => 250
        [7] => 354
        [8] => 250
        [9] => 221
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 372k
  • Answers 372k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer finally it works me like this. for (var index in… May 14, 2026 at 7:22 pm
  • Editorial Team
    Editorial Team added an answer I ended up going with the "unselectable" attribute. May 14, 2026 at 7:22 pm
  • Editorial Team
    Editorial Team added an answer Apparently this was a Linux thing rather than a Firefox… May 14, 2026 at 7:22 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.