我设置了一个名为CorreoComponent的组件,用于在cakephp中发送邮件。它使用PHPMailer发送邮件,但它不发送邮件。奇怪的是,显然send()函数正在发送邮件,因为我有一个像这样的条件`$ 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?
完整的组件类:
<?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;
}
}
?>
所以,如果我没有在控制器上进行重定向,我会在浏览器上看到default_html.ctp中定义的视图,我认为这很好。但是,我渲染控制器输出的方式可能有些不对劲。我不知道,我是新蛋糕。有人可以帮忙吗?
答案 0 :(得分:1)
您的控制器代码无法正确判断发送是否成功,因为您的EmailComponent::send()
方法在失败时返回PHPMailer::$ErrorInfo
,即它可能返回一个字符串,并且只要该字符串不是空的或'0'
(如果出现错误通常会包含错误消息),您的if($result)
将评估为true
。