dow56114 2014-07-26 03:29
浏览 64

CakeMp中的PHPMailer不发送邮件

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?

  • 写回答

1条回答 默认 最新

  • douchai7891 2014-07-26 13:47
    关注

    Your controller code cannot properly tell whether sending was successful, as your EmailComponent::send() method returns PHPMailer::$ErrorInfo on failure, ie it might return a string, and as long as that string isn't empty or '0' (in case of an error it will usually contain an error message), your if($result) will evaluate to true.

    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值