1

I have the following problem, as you may have imagined by the title I have a CakePHP v2.5.6 application with a contact form, and it's giving me an authentication error every time I submit it, how was my surprise, after trying a simple test using PHPMailer it works perfectly with apparently the same configuration.

CakePHP configuration (app/Config/email.php)

<?php

class EmailConfig {

public $info = array(
    'transport' => 'Smtp',
    'host' => 'smtp.foo.com',
    'port' => 25,
    'username' => 'username',
    'password' => 'password'
    );
}   

CakePHP sender code

CakeEmail::deliver('foo@foo.es', 'Subject', 'Test', 'info');  

CakePHP error report

enter image description here

PHPMailer test script

<?php
require './PHPMailer/PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               

$mail->isSMTP();                                     
$mail->Host = 'smtp.foo.com';  
$mail->SMTPAuth = true;                              
$mail->Username = 'username';               
$mail->Password = 'password';                           
$mail->Port = 25;                                   

$mail->setFrom('info@example.es', 'Mailer');
$mail->addAddress('foo@foo.es', 'Mr. foo');                  
$mail->addReplyTo('info@example.es', 'Information');

$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}  

So my question is, is there actually any differences between configurations, or I'm missing something? Why is PHPMailer working and CakeEmail isn't?
Thank you in advance :)

Asur
  • 379
  • 3
  • 20
  • 2
    Are you sure your server allows unencrypted auth? PHPMailer uses opportunistic TLS, which might be working here - set `SMTPDebug = 2` and you will see. – Synchro Feb 24 '16 at 10:48
  • @Synchro Yes , it requires basic auth, actually I asked the server admin for the configuration I needed and they insisted on that point – Asur Feb 24 '16 at 11:11
  • @Synchro The authentication par of the debug looks ok to me... `2016-02-24 11:07:15 SERVER -> CLIENT: 235 2.7.0 Authentication successful` – Asur Feb 24 '16 at 11:14
  • OK, so it's not auto-TLS that's making it work - I guess you need to get Cake to give you more debug output. BTW, those random-looking strings are easily decoded into a real password, so I suggest you edit them out. – Synchro Feb 24 '16 at 11:16
  • @Synchro Thanks for the tip! I think I found the cause of the error, even though the debug wasn't completly accurate about the cause ... – Asur Feb 24 '16 at 11:26

1 Answers1

1

I found the solution which seems pretty dummy but the debug information was not completly accurate about the error cause.
The thing is missing and where PHPMailer and CakeEmail differ is in the from config field.

PHPMailer from configuration

$mail->setFrom('info@example.es', 'Mailer');  

As you may notice the CakeEmail config file lacks of that field, so just adding it the error disappears.

The final CakeEmail config file should look like:

<?php

class EmailConfig {

public $info = array(
    'transport' => 'Smtp',
    'host' => 'smtp.foo.com',
    'port' => 25,
    'username' => 'username',
    'password' => 'password',
    'from' => 'info@example.es'
    );
}   

I hope this helps someone :)

Asur
  • 379
  • 3
  • 20