Email is not sending in cakephp 3.x in localhost - php

Here I am trying to send the email from localhost.I have configured the smtp server setting in php.ini and sendmail.ini file as well.And my code changes are
app.php
'EmailTransport' => [
'default' => [
'className' => 'Smtp',
// The following keys are used in SMTP transports
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'timeout' => 30,
'username' => '******',
'password' => '********',
// 'client' => null,
// 'tls' => null,
],
],
'Email' => [
'default' => [
'transport' => 'default',
'from' => '**********',
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
],
],
Controller code.
$email = new Email('default');
try {
$res = $email->from('*****#**.**')
->to(['*****#**.**' => 'My Website'])
->subject('Tsting')
->send("hiii this is for testing ");
} catch (Exception $e) {
echo 'Exception : ', $e->getMessage(), "\n";
}

Related

SMTP server did not accept the password. on upgrade to cakephp3.6

I upgraded my cakephp3.2 to cakephp3.6 . The emailing function doesnt work and i copied the same code in app file from the working emails in cakephp3.2 to the app file in cakephp3.6. The passwords exist and work fine. I edited them here for security. What has changed in 3.6?
It says the "SMTP server did not accept the password."
//in model
public function sendemail($to,$from,$subject,$message) {
$to='xxxx#gmail.com';
$Email = new Email('default');
// $Email->config('gmail3');
$Email->from(['xxx#gmail.com' => 'My Email'])
->to($to)
->subject($subject)
->send($message);
}//public
//in app file
'EmailTransport' => [
'default' => [
'className' => 'Smtp',
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username'=>'xx#gmail.com',
'password'=>'xx',
'log' => true,
'context' => [
'ssl' => [
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
]
]
],
],
'Email' => [
'default' => [
'transport' => 'default',
'from' => 'xx#gmail.com',
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
],
],
'default' => [
'className' => 'Smtp',
'host' => 'smtp.gmail.com',
'port' => 587 //or 465,
'timeout' => 30,
'username' => 'email',
'password' => 'pass',
'client' => null,
'tls' => true,
],
This config works for me.

CakePHP: Transport config "gmail" is missing

I'm using CakePHP 3.0.15 so I had to use Cake\Network\Email\Email; instead of use Cake\Mailer\Email;. Anyways, I have my EmailTransport in app.php configured like this:
'EmailTransport' => [
'gmail' => [
'className' => 'Smtp',
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'timeout' => 30,
'username' => 'examplesender#gmail.com',
'password' => 's2d5f8t9',
'client' => null,
'tls' => null,
]
],
And have this in my controller:
$email = new Email();
$email->transport('gmail')
->to('examplereveiver#gmail.com', 'Example Receiver')
->from('examplesender#gmail.com', 'Example Sender')
->subject('Test Subject')
->send('Message!!!!!');
Then it gives me the error:
Transport config "gmail" is missing.
However, when I configure the transport in my controller, just before using it, like this:
Email::configTransport('gmail', [
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username' => 'examplesender#gmail.com',
'password' => 's2d5f8t9',
'className' => 'Smtp'
]);
It works and sends the email. Still, I'd like to configure the transport in app.php so I would be able to use the same transport config multiple times.
Thanks!!!

Error: Transport config "Smtp" is missing in cakephp 3.x

Transport config "Smtp" is missing in cakephp 3.x
I have tried some of the configuration which are as follows:
'EmailTransport' => [
'default' => [
'className' => 'Smtp',
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username' => 'xxxxxxxxx#gmail.com',
'password' => 'xxxxx',
],
],
'Email' => [
'default' => [
'from' => array('site#localhost' => 'Data Mining'),
'transport' => 'Smtp',
'charset' => 'utf-8',
'headerCharset' => 'utf-8',
],
],
And i have used below code to send email.
$mail = new Email('default');
$mail->emailFormat('html');
$mail->template($template, null)->viewVars(array('body' => $mailBody));
$mail->to($email_to);
$mail->subject($subject);
$mail->replyTo(Configure::read('config.NOREPLY_EMAIL'));
$headers = array(
'X-MC-MergeVars' => '{"NAME": "Khushang", "REGARDS":"Khushang"}',
'X-MC-Template' => 'test-by-Khushang'
);
$mail->setHeaders($headers);
$mail->send();
Thank you so much...
You are giving transport config as Smtp in Email but you have not defined it in EmailTransport config.
Either set 'transport' => 'Smtp', to 'transport' => 'default',
OR
Set 'default' under 'EmailTransport' to 'Smtp'
Cakephp 3x set to app.php in config folder
'EmailTransport' => [
'default' => [
'className' => 'Mail',
// The following keys are used in SMTP transports
'host' => 'localhost',
'port' => 25,
'timeout' => 600,
'username' => 'user',
'password' => 'secret',
'client' => null,
'tls' => null,
'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
],
'mailjet' => [
'host' => 'smtp.xxxxxxx.com',
'port' => 465,//your ssl or none ssl port no
'username' => 'xxxxxxxxxx', //your smtp mail id
'password' => '***************', //your email password
'timeout' => 60,
'secure' => 'ssl',
'tls' => false,
'className' => 'Smtp'
]
],
Your Controller page
$email = new Email();
$email->transport('mailjet');
$email->from($from)
//->attachments(TICKET_PDF . $file)
->to($to)
->emailFormat('html')
->subject($subject)
->viewVars(array('msg' => $message))
->send($message);

Send Email from SMTP remote server CakePHP 3

I'm facing some problems configuring my app to send Mails from my smtp server, which is located in one remote hosting with 1and1.
I don't know if I'm missing something:
I've changed my app.php to the values given by my hosting provider:
'EmailTransport' => [
'default' => [
'className' => 'Mail',
'host' => 'smtp.1and1.com',
'port' =>'587' ,
'timeout' => 30,
'username' => 'me#dns.com',
'password' => '******',
'client' => null,
'tls' => null,
],
],
'Email' => [
'default' => [
'transport' => 'default',
'from' => '#localhost',
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
],
],
Here you can see the instructions given by my hosting provider to connect to their smtp server.
I don't get any result.
Does anybody have an Idea what may I be missing?
I got it working setting classname as 'Mail' and the rest of the parameters as default. Please, do as follows:
'EmailTransport' => [
'default' => [
'className' => 'Mail',
// The following keys are used in SMTP transports
'host' => 'localhost',
'port' => 25,
'timeout' => 30,
'username' => 'user',
'password' => 'secret',
'client' => null,
],
],
In my case only was problem in className.
For using gmail or office365 server it should be so:
'EmailTransport' => [
'default' => [
'className' => 'Mail',
// The following keys are used in SMTP transports
'host' => 'smtp.office365.com',
'port' => 587,
'timeout' => 30,
'username' => 'my#email.com',
'password' => 'myPassword',
'client' => null,
'tls' => true,
'url' => env('EMAIL_TRANSPORT_DEFAULT_URL', null),
],
],
Please do as following:
//Adding Smtp information for sending mail through smtp instead of php mail function on local server / live server
'EmailTransport' => [
'default' => [
'className' => 'Smtp',
'host' => 'smtp.1and1.com',
'port' =>'587' ,
'timeout' => 30,
'username' => 'me#dns.com',
'password' => '******',
'client' => null,
'tls' => null,
],
],
You can also enable tls to 'tls' => true if required.

How do I setup Cakephp email?

I was wondering about getting this cakephp email thing to work. I have not much knowledge but was wondering what I would need to use. I will be using my website email for this so something like admin#website.com but It is able to log in through gmail because it is setup that way. Thank you.
<?php
class EmailConfig {
public $mail = array(
'transport' => 'Mail',
'from' => 'test#test.com',
'charset' => 'utf-8',
'headerCharset' => 'utf-8',
);
public $smtp = array(
'transport' => 'Smtp',
'from' => array('site#localhost' => 'My Site'),
'host' => 'localhost',
'port' => 25,
'timeout' => 30,
'username' => 'user',
'password' => 'secret',
'client' => null,
'log' => false,
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
);
public $gmail = array(
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username' => 'test#test.com',
'password' => 'myPass',
'transport' => 'Smtp'
);
public $fast = array(
'from' => 'you#localhost',
'sender' => null,
'to' => null,
'cc' => null,
'bcc' => null,
'replyTo' => null,
'readReceipt' => null,
'returnPath' => null,
'messageId' => true,
'subject' => null,
'message' => null,
'headers' => null,
'viewRender' => null,
'template' => false,
'layout' => false,
'viewVars' => null,
'attachments' => null,
'emailFormat' => null,
'transport' => 'Smtp',
'host' => 'localhost',
'port' => 25,
'timeout' => 30,
'username' => 'user',
'password' => 'secret',
'client' => null,
'log' => true,
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
);
}
Here is an example config that works for Gmail / Google Apps hosted emails. You'll obviously need to fill it in with your own email address and password.
class EmailConfig {
public $default = array(
'transport' => 'Smtp',
'from' => array('example#domain.com' => 'Joe Bloggs'),
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'timeout' => 30,
'username' => 'example#domain.com',
'password' => 'YOUR_GMAIL_PASS_GOES_HERE'
);
}
Unless there's a specific SMTP server you wish to connect to, to send email, you don't need to change the default email.php. Just make sure you include a from() and to() in your call.
App::uses('CakeEmail', 'Network/Email');
$Email = new CakeEmail();
$Email->from(['noreply#host.com' => 'No Reply'])
->to('recipient#otherhost.com')
->subject('My subject')
->send('The body of the email');
Just using the default config will use whatever mail functionality you have set up in PHP. On a UNIX box, the machine will usually be set up to relay mail. On a Windows machine, you may need to set up a mail relay, unless it is an SMTP server.
If it's your development environment and Windows, then I would recommend setting up smtp4dev. smtp4dev is a very handy tool that listens on port 25, and behaves like an SMTP server. This will ensure that any locally generated emails from your development environment do not make it to the outside world.

Categories