<?php
/**
 * @version		$Id: contacto.php 20196 2011-03-19 02:40:25Z pol $
 * @package		Joomla.Site
 * @subpackage	Contact
 * @copyright	Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

JHTML::_('stylesheet', 'add.css', 'components/com_contacto/css/');

//$nombre  = JRequest::getCmd('nombre', 'default');
//$email   = JRequest::getCmd('email', 'default');
//$empresa = JRequest::getCmd('empresa', 'default');
//$mensaje = JRequest::getCmd('mensaje', 'default');
$nombre = JRequest::getVar( 'nombre', '', 'post', 'STRING',JREQUEST_NOTRIM );
$email = JRequest::getVar( 'email', '', 'post', 'STRING',JREQUEST_NOTRIM );
$empresa = JRequest::getVar( 'empresa', '', 'post', 'STRING',JREQUEST_NOTRIM );
$mensajenew = JRequest::getVar( 'mensaje', '', 'post', 'STRING',JREQUEST_NOTRIM );

$trab_realizad = stripslashes($mensajenew);
$dar_enters= str_replace("\n","<br>",$trab_realizad);
$dar_espacops= str_replace(" ","&nbsp; ",$dar_enters);
$mensaje=$dar_espacops;


$db =& JFactory::getDBO();
$db->setQuery( 'SELECT * FROM #__contacto_form WHERE id=1');
$messages = $db->loadObjectList();
if ($messages){
    foreach($messages as $message)
    {
            $destinomail= $message->diremail;
            $mensajeterminacion=$message->mensaje;
    }
}

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
require_once(JPATH_COMPONENT.DS.'include/class.phpmailer.php');
$mail = new PHPMailer();

$body = "<strong>Message</strong><br><br>";
$body.=  "<h4>".$contrato."</h4>"
      ."<h4>Name:</h4>".$nombre."<br>"
      ."<h4>Email:</h4>".$email."<br>"
."<h4>Company:</h4>".$empresa."<br>"
."<h4>Message:</h4>".$mensaje."<br>";
$body.= "<br><br><i>Componente Joomla.Contacto por PBLF(c) www.pblf.com.ar // pblf@hotmail.com</i>";

$mail->IsSMTP(); // telling the class to use SMTP
//$mail->Host       = "mail.yourdomain.com"; // SMTP server
$mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
                                           // 1 = errors and messages
                                           // 2 = messages only
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 465;                   // set the SMTP port for the GMAIL server
$mail->Username   = "mailprogweb@gmail.com";  // GMAIL username
$mail->Password   = "mailer123";            // GMAIL password

$mail->SMTPDebug = "false";

$mail->SetFrom('mailprogweb@gmail.com', $nombre);

$mail->AddReplyTo("name@yourdomain.com","First Last");

$mail->Subject    = $email." leave a message";

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);

$address = $destinomail;
$mail->AddAddress($address, "Administrador Pagina");

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
        ////////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////////
        ////////////////////////////////////////////////////////////////////////////////

        ?>
        <div class="envio">
        <?php
        $query ="INSERT INTO #__contacto_historia ( `id` , `diremail` , `nombre` , `empresa` , `mensaje` ) VALUES ( NULL , '{$email}' , '{$nombre}' , '{$empresa}' , '{$mensaje}' )";
        $db->setQuery( $query );
        $db->query();

        echo $mensajeterminacion

        ?>
        </div>
<?php
}
?>

