Sending emails with CakePHP not works using CakeEmail and also SwiftMailer - php

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

Related

Cakephp 3 Unknown Email error

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.

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

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/#

Internal error when sending email with Cake's email class

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?

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.

Categories