POP before SMTP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pedro

    POP before SMTP

    Hi all,

    a friend using Windows wanted to send mail, but he got timeout errors
    after editing php.ini Mail section

    smtp = smtp.server.com
    sendmail_from = friend@server.c om

    I realized that was because of the need to "POP before SMTP",
    so I made him a function. It's working ... for his server (and
    hopefully other servers too).

    Can this function be improved?
    Is it ok to fsockopen in blocking mode?
    What about timeout values?

    Any hints to make this a more reliable function will be greatly
    appreciated.


    Here it is, hope it suits more Windows people without a mail server
    and the need to POP before SMTP


    <?php
    function POP_authenticat e($username, $password, $server) {
    $socket = fsockopen($serv er, 110); // POP3 port
    if (!$socket) {
    return "Couldn't connect to $server:110\r\n ";
    }

    $res = fgets($socket, 512); // read +OK
    if (substr(trim($r es), 0, 3) != "+OK") {
    return $res; // return the error
    }
    fputs($socket, "USER $username\r\n") ; // send user
    $res = fgets($socket, 512); // read +OK
    if (substr(trim($r es), 0, 3) != "+OK") {
    return $res;
    }
    fputs($socket, "PASS $password\r\n") ; // send pass
    $res = fgets($socket, 512); // read +OK
    if (substr(trim($r es), 0, 3) != "+OK") {
    return $res;
    }
    fputs($socket, "QUIT\r\n") ; // quit

    ### I don't care for errors after quitting :-)

    fclose($socket) ;
    return false;
    }
    ?>

    and this is an example on how you can use it:

    <?php
    $username = "netuser";
    $password = "pAzw04D";
    $POPserver = "pop.server.com ";
    ### php.ini's SMTP must correspond to this server
    ### and sendmail_from must be from this server (??)

    $msg = POP_authenticat e($username, $password, $POPserver);
    if ($msg === FALSE) {
    mail("someone@s omewhere.com", "PHP test", "Line 1\nLine 2");
    $msg = "mail (probably) sent.\r\n";
    }
    exit($msg);
    ?>


    Comments? Suggestions? Corrections?
    All are very welcome.



    --
    I have a spam filter working.
    To mail me include "urkxvq" (with or without the quotes)
    in the subject line, or your mail will be ruthlessly discarded.
  • Yves Brault

    #2
    Re: POP before SMTP

    I think your function is pretty well done.


    "Pedro" <hexkid@hotpop. com> wrote in message
    news:ieuumvgbd9 ppvejcjiav3lmje ij4r3h6pg@4ax.c om...[color=blue]
    > Hi all,
    >
    > a friend using Windows wanted to send mail, but he got timeout errors
    > after editing php.ini Mail section
    >
    > smtp = smtp.server.com
    > sendmail_from = friend@server.c om
    >
    > I realized that was because of the need to "POP before SMTP",
    > so I made him a function. It's working ... for his server (and
    > hopefully other servers too).
    >
    > Can this function be improved?
    > Is it ok to fsockopen in blocking mode?
    > What about timeout values?
    >
    > Any hints to make this a more reliable function will be greatly
    > appreciated.
    >
    >
    > Here it is, hope it suits more Windows people without a mail server
    > and the need to POP before SMTP
    >
    >
    > <?php
    > function POP_authenticat e($username, $password, $server) {
    > $socket = fsockopen($serv er, 110); // POP3 port
    > if (!$socket) {
    > return "Couldn't connect to $server:110\r\n ";
    > }
    >
    > $res = fgets($socket, 512); // read +OK
    > if (substr(trim($r es), 0, 3) != "+OK") {
    > return $res; // return the error
    > }
    > fputs($socket, "USER $username\r\n") ; // send user
    > $res = fgets($socket, 512); // read +OK
    > if (substr(trim($r es), 0, 3) != "+OK") {
    > return $res;
    > }
    > fputs($socket, "PASS $password\r\n") ; // send pass
    > $res = fgets($socket, 512); // read +OK
    > if (substr(trim($r es), 0, 3) != "+OK") {
    > return $res;
    > }
    > fputs($socket, "QUIT\r\n") ; // quit
    >
    > ### I don't care for errors after quitting :-)
    >
    > fclose($socket) ;
    > return false;
    > }
    > ?>
    >
    > and this is an example on how you can use it:
    >
    > <?php
    > $username = "netuser";
    > $password = "pAzw04D";
    > $POPserver = "pop.server.com ";
    > ### php.ini's SMTP must correspond to this server
    > ### and sendmail_from must be from this server (??)
    >
    > $msg = POP_authenticat e($username, $password, $POPserver);
    > if ($msg === FALSE) {
    > mail("someone@s omewhere.com", "PHP test", "Line 1\nLine 2");
    > $msg = "mail (probably) sent.\r\n";
    > }
    > exit($msg);
    > ?>
    >
    >
    > Comments? Suggestions? Corrections?
    > All are very welcome.
    >
    >
    >
    > --
    > I have a spam filter working.
    > To mail me include "urkxvq" (with or without the quotes)
    > in the subject line, or your mail will be ruthlessly discarded.[/color]


    Comment

    Working...