Sending email Via SMTP with STARTTLS command with cakePHP - cakephp

I have been asked to use an external SMTP server with the following instructions:
"We expect you to connect to port 25 in cleartext, then issue a STARTTLS to commence TLS/SSL. Then log in"
Is this possible and if so, how should I do it? As I understand it, this is different to setting the TLS to true in the smtp connection array, is that right?
UPDATE
Plodding towards victory here. Looking at the code in SmtpTransport.php I can see it matches up with the spec linked in the comments (obviously) but the block 102-107 seems to set the host to either client or localhost - I'm setting the remote ip in the host configuration. Am i doing it wrong?
Cakes code:
protected function _connect() {
$this->_generateSocket();
if (!$this->_socket->connect()) {
throw new SocketException(__d('cake_dev', 'Unable to connect to SMTP server.'));
}
$this->_smtpSend(null, '220');
if (isset($this->_config['client'])) {
$host = $this->_config['client'];
} elseif ($httpHost = env('HTTP_HOST')) {
list($host) = explode(':', $httpHost);
} else {
$host = 'localhost';
}
try {
$this->_smtpSend("EHLO {$host}", '250');
if ($this->_config['tls']) {
$this->_smtpSend("STARTTLS", '220');
$this->_socket->enableCrypto('tls');
$this->_smtpSend("EHLO {$host}", '250');
}
} catch (SocketException $e) {
if ($this->_config['tls']) {
throw new SocketException(__d('cake_dev', 'SMTP server did not accept the connection or trying to connect to non TLS SMTP server using TLS.'));
}
try {
$this->_smtpSend("HELO {$host}", '250');
} catch (SocketException $e2) {
throw new SocketException(__d('cake_dev', 'SMTP server did not accept the connection.'));
}
}
}
My Email Config:
public $smtp = array(
'transport' => 'Smtp',
'from' => array('me#example.com' => 'my name'),
'host' => 'secretipaddress',
'port' => 25,
'timeout' => 30,
'username' => 'me#example.com',
'password' => 'secretpassword',
'client' => null,
'log' => true,
'tls' => true
);

Enable the 'tls' to true in your config file app.php.
It work for me and in cakephp 3.1 with gmail smtp

Related

Laravel 8.74 - CloudLinux9 - MSSQL11 - ODBC Driver18 - Connection TrustCertificate=1 issue

I am having trouble to add options for laravael config/database.php;
TrustServerCertificate
Encryption
I am getting below error when trying to connect to MSSQL database;
SQLSTATE[08001]: [Microsoft][ODBC Driver 18 for SQL Server]SSL
Provider: [error:1416F086:SSL
routines:tls_process_server_certificate:certificate verify failed:self
signed certificate]
database information is below without TrustServerCertificate&Encryption
'000002' => [
'driver' => 'sqlsrv',
'odbc_driver' => '{ODBC Driver 18 for SQL Server}',
'host' => 'WWW.XXX.YYY.ZZZ',
'database' => 'database_name',
'username' => 'user_name',
'password' => 'pass_word',
'port' => '1433',
'prefix' => '',
'charset' => 'utf8',
]
but when I try with RAW php with below code including TrustServerCertificate it works;
<?php
$host = "WWW.XXX.YYY.ZZZ";
$user = "user_name";
$password = "pass_word";
$dbname="database_name";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_CASE => PDO::CASE_NATURAL,
PDO::ATTR_ORACLE_NULLS => PDO::NULL_EMPTY_STRING
];
try {
$connection = new PDO("sqlsrv:Server=$host,1433; Database=$dbname;TrustServerCertificate=1", $user, $password);
} catch(PDOException $e) {
die("Database connection failed: " . $e->getMessage());
exit;
}
echo"Connection Successful";
?>
How can I add this to Laravels database configuration information?
I tried multiple ways as below but didn't help;
TrustServerCertificate = 1,
TrustServerCertificate = '1',
TrustServerCertificate = 'true',
TrustServerCertificate = true,
trust_server_certificate = 1,
trust_server_certificate = '1',
trust_server_certificate = 'true',
trust_server_certificate = true,
Should it be under an options array?
What should be the setting for ODBC18 driver?
Thanks
If anyone bumps into this issue, just rollback to ODBC driver 17. ODBC driver 18 comes with SSL/Certificate and Encryption requirement as default. If you are able to acquire ssl licences for your servers, do use version 18.
Happy coding :)

SMTP server did not accept the connection or trying to connect to non TLS SMTP server using TLS on cakePHP

I get the following error on my cakePHP2.7 project when I move the code to my new server. The code works fine on all my existing servers.
we are using aws SES as mail service.
We created a sample file as instructed by https://docs.aws.amazon.com/ses/latest/dg/send-using-smtp-programmatically.html and that also works fine.
Any help is appriciated
code for your reference
public $mailarr = array(
'host'=>'email-smtp.us-west-2.amazonaws.com',
'port' => 587,
'username' => 'XXXXXXXXXXXXXXX',
'password' => 'XXXXXXXXXXX',
'tls'=>true,
'returnPath'=>'xxx#xxx.com',
'transport' => 'Smtp',
'from' => array('xxx#xxx.com' => 'Alert!'),
'emailFormat' => 'html',
'timeout' => 300,
);
I have tried to add the below as part of my mailarr but the error still persists
'context' => array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
),
)

cakephp 2.8 vs 1.3 smtp difference which would cause a 550 error

We've recently updated an application from Cakephp 1.3 to version 2.8.
We use the application to send order information email messages to customers via a separate SMTP server. There can be as many as 1,000 messages sent at one time.
Our 1.3 application used to bulk send happily without ever incurring a 550 Maximum recipient quota exceeded message, even for volumes that exceeded the maximum recipient level on the SMTP server. The SMTP server has not changed. The max recipients level is set to 500 per hour, as it always has been.
Cakephp version 2 has a new method of dealing with email (CakeEmail):
http://book.cakephp.org/2.0/en/core-utility-libraries/email.html
There must be a fundamental difference in the ways that Cakephp 1.3 and Cakephp 2.8 sends email via an SMTP method, but I'm really struggling to see it. Can anyone help please?
Relevant code is shown below
/// 1.3 Code
$config['smtp'] = array(
'port'=>'25',
'timeout'=>'30',
'host' => 'mail.smtpservername.com',
'username'=>'smtp#domainname.com',
'password'=>'password'
);
private function send_email($customer, $email_message) {
$this->Email->from = 'Sender Name <address#domain.com>';
$this->Email->smtpOptions = array(
'port'=>Configure::read('smtp.port'),
'timeout'=>Configure::read('smtp.timeout'),
'host' => Configure::read('smtp.host'),
'username'=>Configure::read('smtp.username'),
'password'=>Configure::read('smtp.password')
);
$this->Email->delivery = 'smtp';
$this->Email->to = $customer['first_name'].' '.$customer['last_name'].' <'.$customer['email_address'].'>';
$this->Email->subject = 'A nice subject line';
$this->Email->sendAs = 'text';
$this->Email->template = 'simple_message';
$this->set('email_content', $email_message);
if (!$this->Email->send()) {
CakeLog::write('error', "Sending email to customer {$customer['id']} failed.");
throw new EmailNotSentException("Sending email to customer {$customer['id']} failed.\n\n".$email_message);
}
$this->Email->reset();
return true;
}
/// 2.8 Code
$config['smtp'] = array(
'port' => '25',
'timeout' => '30',
'host' => 'mail.smtpservername.com',
'username' => 'smtp#domainname.com',
'password' => 'password',
'transport' => 'Smtp'
);
private function send_email($customer, $email_message)
{
$Email = new CakeEmail;
$Email->config(Configure::read('smtp'));
$from = array('address#domain.com' => 'Sender Name');
$recipient = array($customer['email_address'] => $customer['first_name'] . ' ' . $customer['last_name']);
$subject = 'A nice subject line';
$Email->from($from);
$Email->to($recipient);
$Email->subject($subject);
$Email->emailFormat('text');
$Email->template('simple_message');
$Email->viewVars(array('email_content' => $email_message));
if (! $Email->send()) {
CakeLog::write('error', "Sending email to customer {$customer['id']} failed.");
throw new EmailNotSentException("Sending email to customer {$customer['id']} failed.\n\n".$email_message);
}
$Email->reset();
return true;
}

CakeEmail and SMTP Error 535

I'm using cakephp 2.3.0 and i want send email using devocot + postfix. SMTP work fine because i have installed roundCube and hi received emails and sends emails.
class EmailConfig {
public $default = array(
'host' => 'poczta.example.com',
'port' => 25,
'auth' => 'plain',
'username' => 'username#poczta.example.com',
'password' => '**********',
'tls' => true,
'transport' => 'Smtp',
'from' => array('username#poczta.example.com' => 'Username'),
'returnPath' => 'username#poczta.example.com',
'layout' => false,
'emailFormat' => 'html',
'template' => 'only_text',
'charset' => 'utf-8',
'headerCharset' => 'utf-8',
);
}
Code for send emails:
try {
$oEmail->reset();
$oEmail->config('default');
$oEmail->to(preg_replace('/&#?[a-z0-9]+;/i', '' , trim($v['Email']['email']) ));
$oEmail->subject($subject);
$content = str_replace('${id}', md5($v['Email']['id']), $context);
$content = str_replace('queueId', $queueId, $content);
$oEmail->viewVars(array('email' => $content));
if($oEmail->send()) {
$last = $v['Email']['id'];
$send++;
}
}
catch(Exception $e) {
$this->out($e->getMessage());
}
And i see i console:
SMTP Error: 535 5.7.8 Error: authentication failed: Invalid authentication mechanism
So quick test with telnet:
telnet 127.0.0.1 25
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
220 poczta.example.com ESMTP Postfix (Debian/GNU)
ehlo testing
250-poczta.example.com
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-STARTTLS
250-AUTH PLAIN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
So please help me how i sent email using cake ?
EDIT: i have tls in email config file.
It is not tsl as you have it in your config, it is tls

Cakephp 2.0 SMTP settings on Email not working

I am using SMTP to send email in my CAKEPHP Project.
My email config as follows
class EmailConfig {
public $Smtp = array(
'transport' => 'Smtp',
'from' => array('contact#mydomainname.com' => 'domainname.com'),
'host' => 'myhostingserver',
'port' => 2525,
'timeout' => 60,
'username' => 'username#mydomainname.com',
'password' => 'secret',
'client' => null,
'log' => false
);
and my Mail Functionality code as follows
$email = new CakeEmail('Smtp');
$result = $email->template('welcome_mail','default')
->emailFormat('html')
->to($to_email)
->from('contact#mydomainname.com')
->subject('Welcome to my domain name')
->viewVars($contents);
if($email ->send('Smtp'))
{
echo ('success');
}
While i am sending mail its throwing following error SMTP timeout. My SMTP Server details are correct its working fine in an existing server. I don't know where I am wrong
Check the encryption type (if applicable), e.g. ssl or tls
Your host URL should look something like this in such case
'host' => 'ssl://myhostingserver'
or
'host' => 'tls://myhostingserver'
If your SMTP server has SSL,you have to enable php_openssl in php.ini to use this service.
You can use this code to test
if(!in_array('openssl',get_loaded_extensions())){
die('you have to enable php_openssl in php.ini to use this service');
}
beside what here already was sugested here that the module must be loaded. i found that some servers have some ports blocked. i used this script to test some servers:
<?php
if(!in_array('openssl',get_loaded_extensions())){
die('you have to enable php_openssl in php.ini to use this service');
} else {
echo "php_openssl in php.ini is enabled <br />";
}
// fill out here the smpt server that you want to use
$host = 'ssl://smtp.gmail.com';
// add here the port that you use for for the smpt server
$ports = array(80, 465);
foreach ($ports as $port)
{
$connection = #fsockopen($host, $port);
if (is_resource($connection))
{
echo $host . ':' . $port . ' ' . '(' . getservbyport($port, 'tcp') . ') is open.<br />' . "\n";
fclose($connection);
} else {
echo $host . ':' . $port . ' is not responding.<br />' . "\n";
}
}
?>

Resources