Email configuration in CakePHP - php

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

Related

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.

cakePHP- Email client: Reading gmail inbox on localhost

I am currently doing a project, but I am still working on the local machine. The problem is that I can't seem to connect the gmail mailbox using this
plugin
The real problem is, that I do not know the code for connecting with gmail account on localhost using the plugin. I have this in my config :
public $emailTicket = array(
'datasource' => 'ImapSource',
'server' => 'localhost',
'connect' => 'imap/tls/novalidate-cert',
'username' => '************#gmail.com',
'password' => '*********',
'port' => '143', //incoming port
'ssl' => false,
'encoding' => 'UTF-8',
'error_handler' => 'php',
'auto_mark_as' => array(
'Seen',
// 'Answered',
// 'Flagged',
// 'Deleted',
// 'Draft',
),
);
Then cake returns an error : Error: Unable to get imap_thread after 4 retries. 'Can't connect to **localhostName**,143: Refused
Anyone knows the correct way to do it? Or if its possible that I continue working on this on the localmachine, if so, how?
[EDIT]
Within the plugin code, this is how it prepares the parameters for php's imap_open() :
case 'imap':
$this->_connectionString = sprintf(
'{%s:%s%s%s}',
$this->config['server'],
$this->config['port'],
#$this->config['ssl'] ? '/ssl' : '',
#$this->config['connect'] ? '/' . #$this->config['connect'] : ''
);
break;
$retries = 0;
while (($retries++) < $this->config['retry'] && !$this->thread) {
$this->Stream = imap_open($this->_connectionString, $this->config['username'], $this->config['password']);
$this->thread = #imap_thread($this->Stream);
}
You need to use the Gmail incoming email imap server settings:
public $emailTicket = array(
'datasource' => 'ImapSource',
'server' => 'imap.gmail.com',
'connect' => 'imap/tls/novalidate-cert',
'username' => '************#gmail.com',
'password' => '*********',
'port' => '993', //incoming port
'ssl' => true,
'encoding' => 'UTF-8',
'error_handler' => 'php',
'auto_mark_as' => array(
'Seen',
// 'Answered',
// 'Flagged',
// 'Deleted',
// 'Draft',
),
);
And ofcourse enable imap on your gmail account...
class EmailConfig {
public $gmail = array(
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username' => 'my#gmail.com',
'password' => 'secret',
'transport' => 'Smtp'
);
}
for Controller action
App::uses('CakeEmail', 'Network/Email');
$email = new CakeEmail('gmail');
For Helping Link
http://book.cakephp.org/2.0/en/core-utility-libraries/email.html
http://www.shahariaazam.com/send-email-from-localhost-in-cakephp-using-cakeemail/#

zf2: How can I store my zend mail transport config in a mail.local.php file?

I tried this, but for me doesn't worked.
https://stackoverflow.com/questions/10537318/how-to-use-the-di-for-zend-mail-transport-smtp
I use zend framework 2.0.3dev
For me this worked with a google app mail address
config/autoload/mail.local.php
return array(
'mail' => array(
'transport' => array(
'options' => array(
'host' => 'smtp.gmail.com',
'connection_class' => 'plain',
'connection_config' => array(
'username' => 'example#example.org',
'password' => '',
'ssl' => 'tls'
),
),
),
),
);
and in Module.php
public function getServiceConfig()
{
return array(
'factories' => array(
'mail.transport' => function (ServiceManager $serviceManager) {
$config = $serviceManager->get('Config');
$transport = new Smtp();
$transport->setOptions(new SmtpOptions($config['mail']['transport']['options']));
return $transport;
},
),
);
}
and in the controller
$transport = $this->getServiceLocator()->get('mail.transport');
I hope that this code will be usefull for somebody :D
Try:
$objEmail = new \Zend\Mail\Message();
$objEmail->setBody('Message here');
$objEmail->setFrom('from#domain.com', 'From');
$objEmail->addTo('to#domain.com', 'To');
$objEmail->setSubject('Subject here');
// Setup SMTP transport using PLAIN authentication over TLS
$transport = new \Zend\Mail\Transport\Smtp();
$options = new \Zend\Mail\Transport\SmtpOptions(array(
'name' => 'smtp.gmail.com',
'host' => 'smtp.gmail.com',
'port' => 587, // Notice port change for TLS is 587
'connection_class' => 'plain',
'connection_config' => array(
'username' => '',
'password' => '',
'ssl' => 'tls',
),
));
$transport->setOptions($options);
$transport->send($objEmail);

CakePHP CakeMail sending email to a TLS Microsoft Exchange Server 2010 (Hosted Exchange in Godaddy)

I've been trying several configuration options to send email using CakePHP (CakeMail) to a Microsoft Exchange 2010 Server. This is my current configuration:
public $default = array(
'transport' => 'Smtp',
'from' => array('email#example.com' => 'Me'),
'host' => 'smtp.ex3.secureserver.net',
'port' => 587,
'timeout' => 30,
'username' => 'verifiedUserName',
'password' => 'verifiedPassword',
'client' => null,
'log' => true,
'delivery' => 'smtp'
);
And this is my testing function:
public function test_email() {
App::uses('CakeEmail', 'Network/Email');
$email = new CakeEmail();
$email->config('default');
debug($email->config());
$result = $email->template('checkout')
->from('email#example.com')
->emailFormat('text')
->to('another#example.com')
->subject('TEST EMAIL ')
->send();
}
I'm getting a
SMTP Error: 504 5.7.4 Unrecognized authentication type
If i change the host to 'ssl://smtp.ex3.secureserver.net' or 'tls://smtp.ex3.secureserver.net' i'm getting a
Unable to connect to SMTP server.
The server is configured to use TLS.
Any ideas ?
(from the book)
http://book.cakephp.org/2.0/en/core-utility-libraries/email.html#configuration
As of 2.3.0 you can also enable TLS SMTP using the tls option:
<?php
class EmailConfig {
public $gmail = array(
'host' => 'smtp.gmail.com',
'port' => 465,
'username' => 'my#gmail.com',
'password' => 'secret',
'transport' => 'Smtp',
'tls' => true
);
}
ref to feature pull request here > https://github.com/cakephp/cakephp/pull/734
You should use "tls"=>true in your $default config.

Sending emails with CakePHP not works using CakeEmail and also SwiftMailer

I'm trying to send emails from CakePHP but without success. I'm trying with CakeEmail and this code:
$email = new CakeEmail();
$email->from(array('reynierpm#gmail.com' => __('Recruitment Job App')))
->to('reynierpm#gmail.com')
->subject(__('Recruitment Status Update'))
->send(__('Dear, ReynierPM this is a testing email'));
And doesn't work because no emails is send. The file /app/Config/email.php have this configuration:
class EmailConfig {
public $default = array(
'transport' => 'Debug',
'host' => 'smtp.gmail.com',
'port' => 25,
'timeout' => 30,
'username' => 'mlrepemi#gmail.com',
'password' => 'secret_password',
'charset' => 'utf-8',
'headerCharset' => 'utf-8',
);
}
I've try also this http://bakery.cakephp.org/articles/sky_l3ppard/2009/11/07/updated-swiftmailer-4-xx-component-with-attachments-and-plugins but in this case I get this error:
Fatal error: Class 'testemailView' not found in /var/www/html/jobapp/app/Controller/Component/swift_mailer.php on line 245
I'm using CakePHP 2.0.6 and SwiftMailer 4.1.5, any help?
Cheers and thanks in advance
You are leaving out some important parts to enable email. You should have something like this:
App::uses('CakeEmail', 'Network/Email');
$email = new CakeEmail('default');
Then in your email.php config, your default configuration for gmail should looke like this:
public $default = array(
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'timeout' => 30,
'username' => 'my#gmail.com',
'password' => 'secret',
'transport' => 'Smtp'
);

Categories