I set up a component named CorreoComponent for sending mail in cakephp. It uses PHPMailer to send the mail, but it's not sending the mail. Strange thing is, apparently the send() function is sending the mail, because I have a condition like this `$result = $this->Correo->send();
if($result) {
//echo "Correo enviado";
$this->Session->setFlash(__('Gracias por enviarnos tus datos de contacto, nos comunicaremos contigo en breve', true));
}
else {
$this->Session->setFlash(__('Ha ocurrido un error al enviar el correo. Lamentamos el inconveniente.', true));
//echo "No se pudo enviar el correo";
}
$this->redirect(array('controller'=>'propiedades', 'action'=>'index'));` in the controller that sends the email. So this condition checks if the Send() method of PHPMailer class is sending, and it returns the message "Gracias por enviarnos tus datos de contacto, nos comunicaremos contigo en breve" alright. But I do not see any mail in the mailbox (I set it up so that it sends messages to another account of mine), I don't see any message in the sent mailbox of the account that's sending it (I use smtp.gmail as the host). Can somebody tell me what I'm missing here, please?
The complete component class:
<?php
/**
* This is a component to send email from CakePHP using PHPMailer
* @link http://bakery.cakephp.org/articles/view/94
* @see http://bakery.cakephp.org/articles/view/94
*/
App::uses('Component', 'Controller');
class CorreoComponent extends Component
{
/**
* Send email using SMTP Auth by default.
*/
var $from = 'informes@compraventarenta.com.mx';
var $fromName = "Informes en CompraVentaRenta.com.mx";
var $sitePrefix = 'compraventarenta';
var $useSMTPAuth = true;
var $smtpSecure = 'ssl';
var $smtpPort = 465;
var $smtpUserName = 'gerardo.v.flores@gmail.com';
var $smtpPassword = '*************';
var $smtpHostNames = "smtp.gmail.com";
var $text_body = null;
var $html_body = null;
var $to = null;
var $toName = null;
var $subject = null;
var $cc = null;
var $bcc = null;
var $template = 'email/default';
var $attachments = null;
var $controller;
function startup(Controller $controller)
{
$this->controller = &$controller;
}
/**
* Helper function to generate the appropriate template location
*
* @return string CakePHP location of the template file
* @param object $template_type
*/
function templateLocation($template_type)
{
return ('..'.DS.strtolower($this->controller->name).DS.$this->template.$template_type);
}
/**
* Renders the content for either html or text of the email
*
* @return string Rendered content from the associated template
* @param object $type_suffix
*/
function bodyContent($type_suffix)
{
$temp_layout = $this->controller->layout; // store the current controller layout
if ($type_suffix == 'html')
$this->controller->layout = 'custom';
else
$this->controller->layout = '';
$mail = $this->controller- >render($this>templateLocation('_'.strtolower($type_suffix)));
// render() automatically adds to the controller->output, we'll remove it
$this->controller->output = str_replace($mail, '', $this->controller->output);
$this->controller->layout = $temp_layout; // restore the controller layout
return $mail;
}
function send()
{
App::import('Vendor', 'PHPMailer', array ('file'=>'phpmailer'.DS.'class.phpmailer.php'));
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPAuth = $this->useSMTPAuth;
$mail->SMTPSecure = $this->smtpSecure;
$mail->Host = $this->smtpHostNames;
$mail->Username = $this->smtpUserName;
$mail->Password = $this->smtpPassword;
$mail->From = $this->from;
$mail->FromName = $this->fromName;
$mail->AddAddress($this->to, $this->toName);
$mail->AddReplyTo($this->from, $this->fromName);
$mail->CharSet = 'UTF-8';
$mail->WordWrap = 80; // set word wrap to 50 characters
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = $this->sitePrefix.' '.$this->subject;
$mail->AltBody = $this->bodyContent('text');
$mail->Body = $this->bodyContent('html');
$result = $mail->Send();
if ($result == false)
$result = $mail->ErrorInfo;
return $result;
}
}
?>
So, if I don't do the redirect on the controller, I see the view defined in default_html.ctp on the browser, which I think it's fine. But maybe there's something wrong in the way I render the controller output. I don't know and I'm new to cake. Can someone help?