从PHP发送使用SMTP gmail的电子邮件

时间:2015-07-31 01:46:27

标签: php smtp gmail phpmailer

我正试图通过GMAIL SMTP从php发送电子邮件。

当我点击发送网站时,会冻结一段时间,然后在没有发送电子邮件时也会恢复正常状态。

我尝试了两个ssl - 495端口和tls - 587端口。

 function sendEmail() {

    require_once 'PHPMailer-master/class.phpmailer.php';        
    $email = new PHPMailer();
    $email ->isSMTP();
    $email->SMTPDebug = '1';
    $email->SMTPAuth = true;
    $email->SMTPSecure = 'tls';
    $email->Host = "smtp.gmail.com";
    $email ->port = 587;
    $email->Username = "something@gmail.com";
    $email -> password = "xxxxx";
    $email ->setFrom("something@gmail.com");
    $email -> Subject = "Hello World";
    $email->Body = "Hello this body";
    $email ->addAddress("something@gmail.com");
    if(!$email ->send()){
        echo $email ->ErrorInfo;
    }
    else{
        echo "email sent";
    }
}

2 个答案:

答案 0 :(得分:0)

Gmail的SMTP端口是465,而不是495.同时将调试模式设置为2,以便您可以查看服务器错误(如果有)。

检查您的Gmail帐户是否已激活两步验证。它被激活,停用它。

检查此功能。希望它有所帮助。

function sendEmail() {
    require 'PHPMailerAutoload.php';
    $mail = new PHPMailer();
    $mail->isSMTP();
    $mail->SMTPDebug = 2; //2 for both client and server side response
    $mail->Debugoutput = 'html';
    $mail->Host = 'smtp.gmail.com';
    $mail->Port = 587;
    $mail->SMTPSecure = 'tls';
    $mail->SMTPAuth = true;
    $mail->Username = "me@gmail.com";//sender's gmail address
    $mail->Password = "mypassword";//sender's password
    $mail->setFrom('me@gmail.com', 'Barack Obama');//sender's incormation
    $mail->addReplyTo('myanotheremail@gmail.com', 'Barack Obama');//if alternative reply to address is being used
    $mail->addAddress('you@yahoo.com', 'George Bush');//receiver's information
    $mail->Subject = 'Howdy!';//subject of the email
    $mail->msgHTML("Have a good day!");
    $mail->AltBody = 'If you can\'t view the email, contact us';
    $mail->addAttachment('images/logo.png');//some attachment

    if (!$mail->send()) {
        return false; //not sent
    } else {
        return true; //sent
    }
}

答案 1 :(得分:0)

或者使用ssl socket简单:

<?php
// Send with smtp ssl
// ini_set("SMTP","ssl://smtp.gmail.com");
// ini_set("smtp_port","465");

// Login email and password
$login = "your-email@cool.xx";
$pass = "123456";

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'verify_peer', false);
stream_context_set_option($ctx, 'ssl', 'verify_peer_name', false);
try{
    // echo $socket = stream_socket_client('ssl://smtp.gmail.com:587', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
    echo $socket = stream_socket_client('tcp://smtp.gmail.com:587', $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
    if (!$socket) {
        print "Failed to connect $err $errstr\n";
        return;
    }else{
        // Http
        // fwrite($socket, "GET / HTTP/1.0\r\nHost: www.example.com\r\nAccept: */*\r\n\r\n");
        // Smtp
        echo fread($socket,8192);
        echo fwrite($socket, "EHLO cool.xx\r\n");
        echo fread($socket,8192);

        // Start tls connection
        echo fwrite($socket, "STARTTLS\r\n");
        echo fread($socket,8192);

        echo stream_socket_enable_crypto($socket, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);

        // Send ehlo
        echo fwrite($socket, "EHLO cool.xx\r\n");
        echo fread($socket,8192);

        // echo fwrite($socket, "MAIL FROM: <hello@cool.com>\r\n");
        // echo fread($socket,8192);

        echo fwrite($socket, "AUTH LOGIN\r\n");
        echo fread($socket,8192);

        echo fwrite($socket, base64_encode($login)."\r\n");
        echo fread($socket,8192);

        echo fwrite($socket, base64_encode($pass)."\r\n");
        echo fread($socket,8192);

        echo fwrite($socket, "rcpt to: <to-email@boome.com>\r\n");
        echo fread($socket,8192);

        echo fwrite($socket, "DATA\n");
        echo fread($socket,8192);

        echo fwrite($socket, "Date: ".time()."\r\nTo: <to-email@boome.com>\r\nFrom:<zour-email@cool.xx\r\nSubject:Hello from php socket tls\r\n.\r\n");
        echo fread($socket,8192);

        echo fwrite($socket, "QUIT \n");
        echo fread($socket,8192);

        /* Turn off encryption for the rest */
        // stream_socket_enable_crypto($fp, false);

        fclose($socket);
    }
}catch(Exception $e){
    echo $e;
}