Enviar mensajes desde Codeigniter con gmail

  1. Descargar Codeigniter
  2. Crear el controlador email  ./application/controllers/email.php con el siguiente código:
    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
    
    class Email extends CI_Controller {
    
    	public function index()
    	{
    		$config = Array(		
    				    'protocol' => 'smtp',
    				    'smtp_host' => 'ssl://smtp.gmail.com',
    				    'smtp_port' => 465,
    				    'smtp_user' => 'correo@gmail.com',
    				    'smtp_pass' => 'clave',
    				    'smtp_timeout' => '4',
    				    'mailtype'  => 'text', 
    				    'charset'   => 'iso-8859-1'
    				);
     
    		$this->load->library('email', $config);
    		$this->email->set_newline("\r\n");
    
    		$this->email->from('correo@gmail.com', 'correo@gmail.com');
    		$this->email->to('destino@ejemplo.com');
    		//$this->email->cc('otro@otro-ejemplo.com');
    		//$this->email->bcc('copiaoculta@otro-ejemplo.com');
    
    		$this->email->subject('Email Test');
    		$this->email->message('Mensaje de Prueba');
    
    		$this->email->send();
    
    		echo $this->email->print_debugger();
    
    
    	}
    }

  3. Editar el archivo ./application/config/routes.php para que el controlador cargado por defecto sea email
    $route['default_controller'] = "email";
  4. Entramos al index.php y como tenemos cargado por defecto el controlador email éste mostrará este mensaje si todo funciona correctamente:
    220 mx.google.com ESMTP asdfasdfsdf.6 - gsmtp 
    hello: 250-mx.google.com at your service, [190.xxx.xxx.227]
    250-SIZE 35882577
    250-8BITMIME
    250-AUTH LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN
    250-ENHANCEDSTATUSCODES
    250-PIPELINING
    250-CHUNKING
    250 SMTPUTF8
    from: 250 2.1.0 OK asdfasdfasd.6 - gsmtp
    to: 250 2.1.5 OK asdfasdfasdf.6 - gsmtp
    data: 354  Go ahead asdfasdfasdf.6 - gsmtp
    250 2.0.0 OK 1427553826 asdfasdfasdf.6 - gsmtp 
    quit: 221 2.0.0 closing connection zf3sm896122vdb.6 - gsmtp
    Your message has been successfully sent using the following protocol: smtp
    User-Agent: CodeIgniter
    Date: Sat, 28 Mar 2015 09:43:25 -0500
    From: "correo@gmail.com" <correo@gmail.com>
    Return-Path: <correo@gmail.com>
    To: destino@otrocorreo.com
    Subject: =?iso-8859-1?Q?Email_Test?=
    Reply-To: "correo@gmail.com" <correo@gmail.com>
    X-Sender: correo@gmail.com
    X-Mailer: CodeIgniter
    X-Priority: 3 (Normal)
    Message-ID: <asdfasdfasd@gmail.com>
    Mime-Version: 1.0
    
    
    Content-Type: text/plain; charset=iso-8859-1
    Content-Transfer-Encoding: 8bit
    
    Mensaje de Prueba.

     

 

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments