简体   繁体   English

Cakephp中的PHPMailer不发送邮件

[英]PHPMailer in cakephp not sending mail

I set up a component named CorreoComponent for sending mail in cakephp. 我设置了一个名为CorreoComponent的组件,用于在cakephp中发送邮件。 It uses PHPMailer to send the mail, but it's not sending the mail. 它使用PHPMailer发送邮件,但未发送邮件。 Strange thing is, apparently the send() function is sending the mail, because I have a condition like this `$result = $this->Correo->send(); 奇怪的是,显然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?

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. 因此,如果不对控制器进行重定向,则会在浏览器上看到default_html.ctp中定义的视图,我认为这很好。 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? 有人可以帮忙吗?

您的控制器代码无法正确判断发送是否成功,因为您的EmailComponent::send()方法会在失败时返回PHPMailer::$ErrorInfo ,即,它可能会返回一个字符串,只要该字符串不为空或为'0' (如果发生错误,通常会包含一条错误消息), if($result)评估if($result)true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM