Προς το περιεχόμενο

Smtp php Gmail


kfoynt

Προτεινόμενες αναρτήσεις

Δημοσ.

Καλησπέρα

 

Προσπαθώ να προσθέσω μία λειτουργία στο site για να στέλνει αυτό mail όταν συμβαίνει κάποιο γεγονός αλλά χωρίς επιτυχία... :cry::cry:

 

Ο κώδικας για το gmail είναι:

><?php
  /*
  ** $Id: Gmail SMTP Class
  ** $Author: MaZz
  ** $E-Mail: [email protected]
  *
  *   This program is free software: you can redistribute it and/or modify
  *   it under the terms of the GNU General Public License as published by
  *   the Free Software Foundation, either version 3 of the License, or
  *   (at your option) any later version.
  *
  *   This program is distributed in the hope that it will be useful,
  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *   GNU General Public License for more details.
  *
  *   You should have received a copy of the GNU General Public License
  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */

  class gmail {
     var $con;
     var $receiver;            //Send to email address
     var $lineterm = "\r\n";

     /******************************
     ** Set receiver
     */
     function setReceiver($receiver) {
        $this->receiver = $receiver;
     }

     /******************************
     ** Connect to gmail SMTP Server
     */
     function gmail() {
        $this->con = @fsockopen('ssl://smtp.gmail.com', 465, $errno, $errstr, 2);
        if (!$this->con)
           die('Σφάλμα: Αδυναμία σύνδεσης με τον smtp server του gmail');
     }

     /******************************
     ** Get server response
     */
     function getData() {
        $data = '';
        while (!feof($this->con)) {
           $data .= fgets($this->con, 128);
           if (strpos($data, "\n"))
              break;
        }

        return $data;
     }

     /******************************
     ** Send data
     */
     function sendData($data) {
        fputs($this->con, $data);
     }

     /******************************
     ** Check code
     */
     function checkCode($code) {
        if (substr($this->getData(), 0, 3) == $code)
           return true;

        return false;
     }
     /******************************
     ** Send greeting
     */
     function sendGreet() {
        $this->sendData('HELO localhost' . $this->lineterm);
        if (!$this->checkCode(250))
           return false;

        return true;
     }

     /******************************
     ** Auth Login
     */
     function auth($user, $pass) {
        $this->sendData('AUTH LOGIN' . $this->lineterm);
        if (!$this->checkCode(334))
           return false;

        $this->sendData(base64_encode($user) . $this->lineterm);
        if (!$this->checkCode(334))
           return false;

        $this->sendData(base64_encode($pass) . $this->lineterm);
        if (!$this->checkCode(235))
           return false;

        return true;
     }

     /******************************
     ** Send e-mail
     */
     function sendMail($user, $pass, $headers, $text) {

        if (!$this->checkCode(220))
           die('Σφάλμα: Αδυναμία σύνδεσης με τον smtp server του gmail');

        if (!$this->sendGreet())
           die('Σφάλμα: Πρόβλημα επικοινωνίας με τον smtp server του gmail');

        if (!$this->auth($user, $pass))
           die('Σφάλμα: Λανθασμένα στοιχεία λογαριασμού gmail');

        $this->sendData('MAIL FROM:<>' . $this->lineterm);
        if (!$this->checkCode(250))
           return false;

        $this->sendData('RCPT TO:<' . $this->receiver . '>' . $this->lineterm);
        if (!$this->checkCode(250))
           return false;

        $this->sendData('DATA' . $this->lineterm);
        if (!$this->checkCode(354))
           return false;

        $this->sendData(implode($this->lineterm, $headers) . $this->lineterm);
        $this->sendData($this->lineterm);
        $this->sendData($text . $this->lineterm);
        $this->sendData('.' . $this->lineterm);
        if (!$this->checkCode(250))
           return false;

        return true;
     }
  }
?>

 

ενώ το αρχείο που χρησιμοποιεί τα παραπάνω είναι:

>            $username = '[email protected]';
           $password = 'password';
           $password_return = mysql_result($result, 0);

           $gmail = new gmail;
           $gmail->setReceiver($email);
           $headers = array(
               'to: ' . $gmail->receiver, //Το header 'to:' πρέπει να πάρει το ίδιο e-mail με αυτό που ορίστηκε στην $gmail->setReceiver();
               'subject: Επιστροφή προσωπικού κωδικού', //Το θέμα του e-mail
               'Content-type: text/plain', //Ο τύπος του e-mail. (Για αλλαγή του character set προσθέστε ';charset=[charset]' π.χ. ';charset=utf8'
           );

           $text = "Ο προσωπικός σας κωδικός είναι: ".$password_return;

           if($gmail->sendMail($username, $password, $headers, $text)) {
              echo "Ο κωδικός στάλθηκε στην email διεύθυνση σας με επιτυχία";
           }

Δημοσ.

Υπάρχει κάποιος συγκεκριμένος λόγος που χρησιμοποιείς αυτή την κλάση; Επίσης, για να χρησιμοποιήσεις τον SMTP του gmail πρέπει να κάνεις authenticate με το gmail user/pass σου. Δεν βλέπω πουθενά να καλείς την gmail->auth() πριν στειλεις το e-mail. Πολύ μπλέξιμο για ένα πάναπλο πράγμα...

 

Καλύτερο θα ήταν να χρησιμοποιήσεις την sendmail() της PHP ή τον SMTP του server από όπου τρέχει ο κώδικάς σου. Επίσης θα μπορούσες να χρησιμοποιήσεις και μια mail library όπως αυτή για ακόμα μεγαλύτερη ευκολία.

Αρχειοθετημένο

Αυτό το θέμα έχει αρχειοθετηθεί και είναι κλειστό για περαιτέρω απαντήσεις.

  • Δημιουργία νέου...