I have a CakePHP application that is written with version 2.7.7.
Following is original environment setting on server before I upgrade with this source code.
PHP - 5.3.3
Apache - 2.2.15
CentOS - 6.10
With this original source code, the email is sent correctly and email config is as shown in below in app/Config/email.php.
public $smtp = array(
'host' => 'startls://mail.xxx.com',
'port' => 587,
'username' => 'xxx@xxx',
'password' => 'xxx',
'transport' => 'Smtp',
);
Then, I had upgraded server with following versions and deploy the existing source code again.
PHP - 7.0.33
Apache - 2.4.47
CentOS - 7.x
After server upgraded, email is not working without error. So, I changed mail setting like following.
public $smtp = array(
'host' => 'mail.xxx.com',
'port' => 587,
'username' => 'xxx@xxx',
'password' => 'xxx',
'transport' => 'Smtp',
'tls' => true
);
Based on this setting, I created new crontroller and try to send mail.
<?php
App::uses('AppController', 'Controller');
App::uses('CakeEmail', 'Network/Email');
class MailTestController extends AppController
{
public function index()
{
$this->autoRender = false;
$receiver = 'xxx';
App::uses('CakeEmail', 'Network/Email');
$email = new CakeEmail();
$email->config('smtp');
$from_email_address = 'xxx@xxx';
$email->from($from_email_address);
$email->to($receiver);
$email->subject('MAIL TESTING');
$message = "mail sending test";
if (!$email->send($message)) {
CakeLog::write('debug', $this->Email->smtpError);
}
}
}
I have got following error when I try to send mail via cakephp.
Error: [SocketException] SMTP server did not accept the connection or trying to connect to non TLS SMTP server using TLS.
I’m not sure why is it happening like this. While I’m asking about this error to mail vendor, they said they already open port 587 in mail server for SMTP. So, I’m created new php code file called index.php
to send mail with PHPMailer like following and try to send mail via that code in upgraded application server with command php index.php
with the same host, username, password and port. Mail is sent and receive smoothly and no error occurrs. Error is only happened while I sent mail via cakephp application code. What do I need to check in my application server or mail server?
<?php
//Import PHPMailer classes into the global namespace
//These must be at the top of your script, not inside a function
use PHPMailerPHPMailerPHPMailer;
use PHPMailerPHPMailerSMTP;
use PHPMailerPHPMailerException;
//Load Composer's autoloader
require 'vendor/autoload.php';
//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output
$mail->isSMTP(); //Send using SMTP
$mail->Host = 'mail.xxx.com'; //Set the SMTP server to send through
$mail->SMTPAuth = true; //Enable SMTP authentication
$mail->Username = 'xxx@xxx'; //SMTP username
$mail->Password = 'xxx'; //SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; //Enable implicit TLS encryption
$mail->Port = 587; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS`
//Recipients
$mail->setFrom('xxx@xxx', 'Mailer');
$mail->addAddress('xxx', 'John Doe'); //Add a recipient
//Content
$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';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}