Reputation: 387
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('[email protected]', 'Subject', 'Test', 'info');
CakePHP error report
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('[email protected]', 'Mailer');
$mail->addAddress('[email protected]', 'Mr. foo');
$mail->addReplyTo('[email protected]', '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 :)
Upvotes: 1
Views: 266
Reputation: 387
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('[email protected]', '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' => '[email protected]'
);
}
I hope this helps someone :)
Upvotes: 1