How do I setup Cakephp email? - php

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.

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.

Email is not sending in cakephp 3.x in localhost

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";
}

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);

Email configuration in CakePHP

I'm trying to send mails through CakePHP, but I always got a 'Connection timed out' message. I've used two different configurations: gmail and 1and1, but result is the same.
This is my email.php file:
public $oneandone = array(
'host' => 'ssl://smtp.1and1.es',
'port' => 465,
'username' => 'my-address#my-domain.com',
'password' => '*****',
'transport' => 'Smtp',
'timeout' => 1);
public $gmail = array(
'host' => 'smtp.gmail.com',
'port' => 587,
'username' => 'my-address#gmail.com',
'password' => '****',
'timeout' => 1,
'tls' => true,
'transport' => 'Smtp');
And this is my remind function inside the UsersController.php file:
public function remind() {
App::uses('CakeEmail', 'Network/Email');
$this->set('url', $this->referer());
if ($this->request->is('post')) {
$Email = new CakeEmail('gmail');
$Email->from(array('my-address#my-domain.com' => 'Staff'));
$Email->to('destiny#hotmail.com');
$Email->subject('Password reminder');
$Email->send('Here is your current password: xxxxxx');
return $this->redirect(array('action' => 'login'));
}
}
The error I got is
if (!$this->_socket->connect()) {
throw new SocketException(__d('cake_dev', 'Unable to connect to SMTP server.'));
so I don't know why I'm not being able to connect. I've modified some params like tls, ssl but no results.
Within the same server (1and1) I can send emails with the standard mail() function, so I don't think it is a problem of PHP configuration. Correct me if I'm wrong.
Some ideas would be appreciated!
the following, worked me correctly. on Cakephp 2
public $gmail = array(
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username' => 'myEmail#gmail.com',
'password' => 'AwesomePass',
'transport' => 'Smtp'
);
Note that gmail uses port 465.
It is possible that gmail does not work if your site does not have ssl certification.
in this case you must use a smtp configuration
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',
);
with the following function
public function sendMail() {
App::uses('CakeEmail', 'Network/Email');
$templateVar = array(); //array variable to use in the template
$Email = new CakeEmail();
$Email->template('template_name', 'default') //View/Emails/html/template_name.ctp
->config('gmail')//gmail or smtp
->emailFormat('html')
->subject('Awesome subject')
->to('toUser#email.com')
->from(array('myEmail#gmail.com'=>'My name'))
->viewVars($templateVar)
->send();
}

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.

Categories