Using Gmails Outgoing SMTP from PHP using TLS

前端 未结 2 1602
野的像风
野的像风 2020-12-21 13:10

I\'m sending email from PHP through the Gmail SMTP server. I\'ve been using the CakePHP email component with SMTP settings set. I originally had it all working fine using SS

相关标签:
2条回答
  • 2020-12-21 13:10

    Not sure if you are still looking for it, but to start tls, you have to do it from the commands with the server. Here is a simple set up that works for tls with gmail (if you want more help beyond just connecting via tls, start another question):

    <?php
    function get($socket,$length=1024){
        $send = '';
        $sr = fgets($socket,$length);
        while( $sr ){
            $send .= $sr;
            if( $sr[3] != '-' ){ break; }
            $sr = fgets($socket,$length);
        }
        return $send;
    }
    function put($socket,$cmd,$length=1024){
        fputs($socket,$cmd."\r\n",$length);
    }
    if (!($smtp = fsockopen("smtp.gmail.com", 587, $errno, $errstr, 15))) {
        die("Unable to connect");
    }
    echo "<pre>\n";
    echo get($smtp); // should return a 220 if you want to check
    
    $cmd = "EHLO ${_SERVER['HTTP_HOST']}";
    echo $cmd."\r\n";
    put($smtp,$cmd);
    echo get($smtp); // 250
    
    $cmd = "STARTTLS";
    echo $cmd."\r\n";
    put($smtp,$cmd);
    echo get($smtp); // 220
    if(false == stream_socket_enable_crypto($smtp, true, STREAM_CRYPTO_METHOD_TLS_CLIENT)){
        // fclose($smtp); // unsure if you need to close as I haven't run into a security fail at this point
        die("unable to start tls encryption");
    }
    
    $cmd = "EHLO ".$_SERVER['HTTP_HOST'];
    echo $cmd;
    put($smtp,$cmd);
    echo get($smtp); // 250
    
    $cmd = "QUIT";
    echo $cmd."\r\n";
    put($smtp,$cmd);
    echo get($smtp);
    
    echo "</pre>";
    
    fclose($smtp);
    
    0 讨论(0)
  • 2020-12-21 13:11

    Why not use PEAR?

    http://email.about.com/od/emailprogrammingtips/qt/PHP_Email_SMTP_Authentication.htm

    Change the port in the second example.

    0 讨论(0)
提交回复
热议问题