2014-07-26 2 views
0

cakephp에서 메일을 보내기 위해 CorreoComponent라는 구성 요소를 설정했습니다. PHPMailer를 사용하여 메일을 보내지 만 메일을 보내지 않습니다. 이상한 일은 분명히 send() 함수가 메일을 보내는 것입니다. 왜냐하면`$ result = $ this-> Correo-> send();cakephp의 PHPMailer가 메일을 보내지 않음

 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 = '[email protected]'; 
var $fromName = "Informes en CompraVentaRenta.com.mx"; 
var $sitePrefix = 'compraventarenta'; 
var $useSMTPAuth = true; 
var $smtpSecure = 'ssl'; 
var $smtpPort = 465; 
var $smtpUserName = '[email protected]'; 
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

Gmail은 임의의 "보낸 사람"주소를 허용하지 않습니다. 계정이 Gmail 환경 설정에서 자신의 계정으로 설정되어 있는지 확인해야합니다. –

답변

1
컨트롤러 코드가 제대로만큼 해당 문자열이 비어 있거나 '0' 아니므로 문자열을 반환하고, 수, 즉 당신의 EmailComponent::send() 방법은 실패 PHPMailer::$ErrorInfo 반환로 보내는, 성공 여부를 알 수 없습니다

(오류 그것의 경우 일반적으로 오류 메시지가 표시됩니다) if($result)true으로 평가됩니다.

+0

맞아 그래서이 조건을 변경 :'($ result === true) { \t \t \t // echo "Correo enviado"; \t \t \t $ this-> Session-> setFlash (__ ('__)', 'ㅈㅈ 다른 \t \t \t \t} \t \t \t { \t \t \t $ this-> 세션 -> setFlash (__ ('하는 않은 오류 알 친구에게 보내기 덧글 엘 correo을 ocurrido Lamentamos 엘 inconveniente를..'$ 결과, TRUE).); \t \t \t \t // echo "No se pudo enviar el correo"; \t \t \t} 이제 제대로 작동합니다. SMTP 연결에 오류가 발생했습니다. 그러나 '$ mail-> SMTPSecure'를 "tls"로 바꾸고 587 번 포트 (또한 방화벽을 연결 해제)하면 이제는 작동합니다. –

관련 문제