I'm trying to send email via cakephp3.
This is my app email config:
'EmailTransport' => [
'default' => [
'className' => 'Mail',
// The following keys are used in SMTP transports
'host' => 'localhost',
'port' => 25,
'timeout' => 30,
'username' => 'user',
'password' => 'secret',
'client' => null,
'tls' => null,
],
],
And this is controller :
public function index() {
$this->autoRender = false;
$email = new Email('default');
if (
$email->from(['mymail#gmail.com' => 'My Site'])
->to('mymail#gmail.com')
->subject('About')
->send('My message')) {
print 'ok';
}
}
And now, if i run this function, result is printed 'ok' on monitor.
But no mail is on my testing email box, even span, nothing.
My question is, why cakephp tells me that sending was done, but no mail is present on my box ?
Thank You.
Your configuration is incorrect. cakePHP attempts to send the e-mail via smtp to your localhost.
Most likely you do not have an MTA (ie. exim, dovecot) installed locally and the request gets dropped. This should be visible as an error in you logs (if enabled).
An easy solution is to change the configuration to a working email service for testing, for example Gmail.
Example:
Email::configTransport('gmail', [
'host' => 'smtp.gmail.com',
'port' => 587,
'username' => 'my#gmail.com',
'password' => 'secret',
'className' => 'Smtp',
'tls' => true
]);
Note that the host and port point to Gmail. This example is indicative and was taken from the official documentation.
More information on configuring email transports in cakePHP 3.0 here: http://book.cakephp.org/3.0/en/core-libraries/email.html#configuring-transports
Related
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
app.php
'EmailTransport' => [
'default' => [
//**'className' => MailTransport::class,
/*
* The following keys are used in SMTP transports:
*/
'host' => 'ssl://smtp.gmail.com',
'port' => 567,
//'timeout' => 30,
'username' => 'abc#gmail.com',
'password' => 'abc',
'className' => 'Smtp',
// 'client' => null,
'tls' => true,
//'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
],
],
'Email' => [
'default' => [
'transport' => 'default',
'from' => 'abc#gmail.com',
],
],
Controller class
public function mail()
{
$session = $this->request->session();
$id = $session->read('req_id');
$email = new Email();
$email->transport('default');
$email->from(['NO-REPLY.formcr1#abc.com.au' => 'abc REPLY']);
$email->sender(['NO-REPLY.formcr1#abc.com.au' => 'abc NO-REPLY']);
$email->to('abc#gmail.com'); /** This must be changed to abc's confirmed email */
$email->subject('abc Request Number : '.$id);
//THIS PATH NEEDS TO BE CHANGED DURING DEPLOYMENT
$path = 'C:/xampp/htdocs/request_form/webroot/pdfresults/';
$email->attachments($path. 'abc Cost Estimate Request Information_'.$id.'_'.'v.3online'.'.pdf');
$email->send('Please look for the attachment to see the form. Cheers!');
}
enter image description here
email credential are correct. and tried turning off the firewalls as well but still not working
The error means exactly what it says - the service you’re trying to connect to doesn’t exist or is not responding.
This is explained two ways. Either the service you’re connecting to is indeed down, or you’re trying to connect to the wrong server or port.
In this case, it’s the latter. You’re trying to connect to an implicit TLS SMTP service on a port not associated with that service.
Change this:
'host' => 'ssl://smtp.gmail.com',
'port' => 567,
To
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
Or
'host' => 'tls://smtp.gmail.com',
'port' => 587,
My web servers have disabled mail for security purposes I now need to reconfigure my cakephp code to send the emails via SMTP as recommended by the host.
My code runs fine on localhost with php mail enabled
use Cake\Mailer\Email;
class LoansController extends AppController
public function sendtestemail(){
$email = new Email();
$email->setViewVars(['name' => 'test test', 'subject'=>'subject test',
'message'=>'testit']);
$email
->template('bulkemail')
->emailFormat('html')
->to('info#test.co.ke')
->from('info#test.co.ke')
->subject($subject)
->send();
}
error:
Could not send email: mail() has been disabled for security reasons
Cake\Network\Exception\SocketException
My code runs fine on localhost with php mail enabled
It works fine in localhost but not in your remote hosting because your hosting company disabled it and you probably do not have much control over it.
To send an email in cakephp use Email class of Cakephp 3. In app.php under config folder, add a new entry in the table EmailTransport.
In your case ‘Smtp’. Specify host, port, username and password in it:
'EmailTransport' => [
'default' => [
'className' => 'Smtp',
// The following keys are used in SMTP transports
'host' => 'localhost',
'port' => 25,
'timeout' => 30,
'username' => 'user',
'password' => 'secret',
'client' => null,
'tls' => null,
'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
],
‘mail’=> [
'host' => 'smtp.gmail.com',
'port' => 587,
'username' =>xxxxx', //gmail id
'password' =>xxxxx, //gmail password
'tls' => true,
'className' => 'Smtp'
]
],
Now in Controller, the function to send email uses above-written entry in transport() function as below.
Add path in controller- use Cake\Mailer\Email:
function sendEmail()
{
$message = "Hello User";
$email = new Email();
$email->transport('mail');
$email->from(['Sender_Email_id' => 'Sender Name'])
->to('Receiver_Email_id')
->subject(‘Test Subject’)
->attachments($path) //Path of attachment file
->send($message);
}
Also have in mind that many hosting companies also block default smtp ports. ( I'm aware that digital ocean does it, for example ). So, you might have to change that port or contact them to get it opened for you ( usually after some sort of verification ).
Some reference about what I just answered: https://www.digitalocean.com/community/questions/digital-ocean-firewall-blocking-sending-email
What worked for me is from https://book.cakephp.org/2/en/core-utility-libraries/email.html
Edit /app/Config/email.php
public $smtp = array(
'transport' => 'Smtp',
'from' => array('xxxxxxxxxx#gmail.com' => 'Betting site'),
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username' => 'xxxxxxxxxx#gmail.com',
'password' => 'xxxxxxxxxxpass',
'client' => null,
'log' => true
);
I wanted to ask about sending emails in cakephp3.
I am using cakephp3 docs, and configured everything as example shows.
But, when I try to send mail, this error appears:
Could not send email: unknown
//app.php
'EmailTransport' => [
'default' => [
'className' => 'Mail',
// The following keys are used in SMTP transports
'host' => 'smtp.gmail.om',
'port' => 465,
'timeout' => 30,
'username' => 'mymail#gmail.com',
'password' => 'password',
'client' => null,
'tls' => null,
],
],
ContactController:
public function contact() {
if (isset($this->request->data) AND ($this->request->is('post'))) {
$email = new Email('default');
if ($email->from(['mymail#gmail.com' => 'My Site'])->to('othermail#gmail.com')->subject('Hello')->send('Message')) {
//pr( 'ok');
}
}
}
Is this a generic error message (, which may have many reasons, in my opinion)? it has no value in context of debug.
You want to use an SMTP server, but you've configured to use the Mail transport!
The className option should be set to Smtp. The host should probably also be different (ssl:// prefixed), or you should enable TLS, please be sure that you read through the questions/answers found with the search linked below.
See also
Cookbook > Email > Configuring Transports
https://stackoverflow.com/search?q=[cakephp]+gmail
The host name in default configuration is incorrect.
it should be
'host' => 'smtp.gmail.com',
instead of
'host' => 'smtp.gmail.om',
One thing that's always forgotten is the Email profiles
`'Email' => [
'default' => [
'transport' => 'gmail', //this allows the email class to use the gmail settings
'from' => 'youremail#gmail.com',
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
],
],`
And by the way you can set up multiple profiles for the like testing, development etc
use Cake\Mailer\Email;
Email::configTransport('gmail', [
'host' => 'smtp.gmail.com',
'port' => 587,
'username' => 'my#gmail.com',
'password' => 'secret',
'className' => 'Smtp',
'tls' => true
]);
Yous this code : Replace My#gmail.com and Secret with you credentials offcourse and after that use you code it will work.
I'm trying to use a Gmail account to send emails using CakePHP's email library.
In app/Config/email.php I have the following entry:
public $default = array(
'host' => 'smtp.gmail.com',
'port' => 465,
'username' => 'testaccount#gmail.com',
'password' => 'mypaswsword',
'transport' => 'Smtp',
'tls' => true
);
and in my controller I've put App::uses('CakeEmail', 'Network/Email'); and this in my action:
$email = new CakeEmail();
$email->from(array('me#example.com' => 'My Site'))
->to('you#example.com')
->subject('About')
->send('My message');
When I load the page I get a very undescriptive error message of Error: An Internal Error Has Occurred., which doesn't give me the slightest clue about what's gone wrong. I know the Gmail account settings are correct, and I'm using it just like the Cake documentation tells me to. The stack trace printed to the page tells me the error comes from this line in CORE\Cake\Network\Email\MailTransport.php:
$this->_mail($to, $email->subject(), $message, $headers, $params);
Does anyone know what could be going wrong here?
Edit: I've also tried using the following config:
public $default = array(
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username' => 'testaccount#gmail.com',
'password' => 'mypassword',
'transport' => 'Smtp'
);
but to no avail. I get exactly the same error message.
Did you check your connection and that GMAIL info is correct?
I've been using AWS SES with a CakePHP app for a few months, everything was working fine but we had a server problem meaning I had take the site off and get the server restored. Once I put the site back on I noticed emails wouldn't send and causing CakePHP to error.
The Error
2013-01-14 14:50:02 Error: [SocketException] SMTP Error: 535 Incorrect authentication data
#0 /public_html/lib/Cake/Network/Email/SmtpTransport.php(132): SmtpTransport->_smtpSend('QWhRZ2F0azQyTnh...', '235')
#1 /public_html/lib/Cake/Network/Email/SmtpTransport.php(61): SmtpTransport->_auth()
#2 /public_html/lib/Cake/Network/Email/CakeEmail.php(1059): SmtpTransport->send(Object(CakeEmail))
#3 /public_html/app/Controller/UsersController.php(1945): CakeEmail->send()
What I've done so far
I've checked both the domain and email are verified.
I regenerated new SMTP details just incase they where wrong.
Made sure my server time was correct using NTP
In the meantime I set up SMTP with a google business apps.
My Configuration (Some details altered for security)
public $smtp = array(
'transport' => 'Smtp',
'from' => array('no.reply#company.com' => 'Company'),
'host' => 'ssl://email-smtp.us-east-1.amazonaws.com',
'port' => 465,
'timeout' => 10,
'username' => 'AKIAICH5321NNDR2CMA',
'password' => 'AhQgat12Nx21c5e78S9Ufku0+4fw9LnRpuMTGZwjXT',
'client' => null,
'log' => false,
);
My Configuration for Google Business Apps (which works)
public $smtp = array(
'transport' => 'Smtp',
'from' => array('no.reply#company.com' => 'Company'),
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'timeout' => 10,
'username' => 'all#company.com',
'password' => 'b1t306b',
'client' => null,
'log' => false
);
Could I be missing some module of my server or has AWS changed something that happened round the same time as my server change? I'd really appreciate any help or advice. Thanks Jason
I've found the issue. Under WHM there is an option SMTP Restrictions, I disabled it.