I'm trying to find out how to use CakeEmail. To this end I've created a view called email-tester.ctp, using the Pages controller. I access it by calling the page directly (mydomain/mypath/pages/email-tester).
The view itself just holds the standard code from the documentation, more or less:
<?php
App::uses('CakeEmail', 'Network/Email');
$Email = new CakeEmail();
$Email->from(array('myaccount#gmail.com' => 'My Gmail Address'))
->to('my#email-address')
->subject('About')
->send('My message');
?>
<p>Email sent...</p>
I have also created an email config file (email.php) as follows:
class EmailConfig {
public $gmail = array(
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username' => 'myaccount#gmail.com',
'password' => 'mypassword-for-gmail',
'transport' => 'Smtp',
);
}
When I run this page I just get an internal server error:
Error: An Internal Error Has Occurred.
Stack Trace
CORE/Cake/Network/Email/MailTransport.php line 51 → MailTransport->_mail(string, string, string, string, null)
CORE/Cake/Network/Email/CakeEmail.php line 1158 → MailTransport->send(CakeEmail)
APP/View/Pages/email-tester.ctp line 8 → CakeEmail->send(string)
CORE/Cake/View/View.php line 948 → include(string)
CORE/Cake/View/View.php line 910 → View->_evaluate(string, array)
CORE/Cake/View/View.php line 471 → View->_render(string)
CORE/Cake/Controller/Controller.php line 954 → View->render(string, null)
APP/Controller/PagesController.php line 69 → Controller->render(string)
[internal function] → PagesController->display(string)
CORE/Cake/Controller/Controller.php line 490 → ReflectionMethod->invokeArgs(PagesController, array)
CORE/Cake/Routing/Dispatcher.php line 191 → Controller->invokeAction(CakeRequest)
CORE/Cake/Routing/Dispatcher.php line 165 → Dispatcher->_invoke(PagesController, CakeRequest)
APP/webroot/index.php line 108 → Dispatcher->dispatch(CakeRequest, CakeResponse)
Can anyone point out to me where I'm going wrong, please? Do I need to have a mail server running on my local development machine where I'm working? Or is the problem something more fundamental?
From other research I saw some suggestions that the php.ini file should contain a line for the extension php_openssl, however this just causes an error message to appear saying that the extension is not found: probably because it's included via gnutls (I'm on Ubuntu 14.10).
Any help would be much appreciated.
Peter
If you are working from a local server such as wampp, xampp, etc, this function wont work, yo do need to have a mail server setup for this to work. Once you have on, or if you can test in a real server this might help:
In:
Views->Layouts->Emails->html
I have an html template for my emails called "clientsreports":
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
<head>
<title><?php echo $title_for_layout;?></title>
</head>
<body>
<?php echo $content_for_layout;?>
</body>
</html>
Also your var in the EmailConfig file should be named smtp for it to work, check how mine is setup
class EmailConfig {
public $smtp = array(
'transport' => 'Smtp',
'from' => array('myaddress#gmail.com' => 'Senders name'),
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'timeout' => 30,
'username' => 'myaddress#gmail.com',
'password' => 'mypassword',
'client' => null,
'log' => false
);
}
Finally this is a simple example of how i send out emails:
$email = new CakeEmail('smtp');
$email->template('clientsreport', 'clientsreport');
$email->emailFormat('html');
$email->viewVars(array('message' => "This is the body of the message"));
$email->from(array('from#gmail.com' => 'Senders name'));
$email->to('whoisthemailfor#gmail.com');
//Only if neccesary this is how to carbon copy someone
$email->cc(array('carbon#one.com','carbon#two.com'));
$email->subject('Subject for the email');
$email->send();
I hope that helps, good luck!
Related
I start in cakephp, my worry is that when I try to send a mail I have the following error:
Network is unreachable
Error: An Internal Error Has Occurred.
Stack Trace
CORE/Cake/Network/Email/SmtpTransport.php line 155 → CakeSocket->connect()
CORE/Cake/Network/Email/SmtpTransport.php line 95 → SmtpTransport->_connect()
CORE/Cake/Network/Email/CakeEmail.php line 1173 → SmtpTransport->send(CakeEmail)
APP/Controller/OnlinesController.php line 114 → CakeEmail->send(string)
[internal function] → OnlinesController->felicitation()
CORE/Cake/Controller/Controller.php line 491 → ReflectionMethod->invokeArgs(OnlinesController, array)
CORE/Cake/Routing/Dispatcher.php line 193 → Controller->invokeAction(CakeRequest)
CORE/Cake/Routing/Dispatcher.php line 167 → Dispatcher->_invoke(OnlinesController, CakeRequest)
APP/webroot/index.php line 111 → Dispatcher->dispatch(CakeRequest, CakeResponse)
Here is my controller
<?php
App::uses('AppController', 'Controller');
App::uses('CakeEmail', 'Network/Email');
class ContactController extends AppController {
public function felicitation()
{
$email = new CakeEmail('default');
$email->transport('smtp');
$email
->from('xxxxxxx#xxxxxx.org')
->to(['xxxxxx#xxxxx.com' => 'My Website'])
->subject('Contact')
->send('test mail');
}
}
Then my email.php file
<?
public $default = array(
'transport' => 'SMTP',
'host' => 'myhost',
'port' => 465,
'timeout' => 30,
'username' => 'domaine#domaine.com',
'password' => '*********',
'charset' => 'utf-8',
'headerCharset' => 'utf-8',
);
?>
I think I have all try, I do not know what to do now so thank you in advance for your help
This may not be a problem with MailGun as I was unable to send via Gmail aswell.
The error i'm getting as seen below you can see where the domain should be passed but hasn't.
POST https://api.mailgun.net/v3//messages.mime
the domain should be
POST https://api.mailgun.net/v3/domin/messages.mime
I know I have Guzzle installed, I have restated the web server and i know my details are correct. I'v created a test project to do only mail aswell to no avail.
Could it be something todo with my host computer (macbook air) or that fact i'm using the development web server
php artisan serve
I'm new to Laravel so i'm unsure of anything else I can do.
services.php
'mailgun' => [
'domain' => env('sandbox*****.mailgun.org'),
'secret' => env('key-**************'),
],
mail.php
'driver' => env('MAIL_DRIVER', 'mailgun'),
'host' => env('MAIL_HOST', 'smtp.mailgun.org'),
'port' => env('MAIL_PORT', 587),
'from' => ['address' => null, 'name' => null],
'encryption' => env('MAIL_ENCRYPTION', 'tls'),
'username' => env('postmaster#sandbox***********.mailgun.org'),
'password' => env('sandboxpassword'),
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => env('MAIL_PRETEND', false),
A've stopped using the env file so it defaults to the mail.php, but when the attributes are the same details it's the same outcome. And yeah just incase its asked i'm aware you need to restart the server when you've changed the .env and just to be on the safe site i've been doing it when changing the mail.php or services.php
TestController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Mail;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class TestController extends Controller
{
public function index() {
Mail::raw('Text to e-mail', function ($message) {
$message->from('us#example.com', 'Laravel');
$message->to('dksnowdon#gmail.com');
});
return view('welcome');
}
}
the exact error
ClientException in RequestException.php line 107:
Client error: `POST https://api.mailgun.net/v3//messages.mime` resulted in a `404 NOT FOUND` response:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>404 Not Found</title>
<h1>Not Found</h1>
<p>The requested (truncated...)
in RequestException.php line 107
at RequestException::create(object(Request), object(Response)) in Middleware.php line 65
at Middleware::GuzzleHttp\{closure}(object(Response)) in Promise.php line 199
at Promise::callHandler('1', object(Response), array(object(Promise), object(Closure), null)) in Promise.php line 152
at Promise::GuzzleHttp\Promise\{closure}() in TaskQueue.php line 60
at TaskQueue->run(true) in Promise.php line 240
at Promise->invokeWaitFn() in Promise.php line 217
at Promise->waitIfPending() in Promise.php line 261
at Promise->invokeWaitList() in Promise.php line 219
at Promise->waitIfPending() in Promise.php line 62
at Promise->wait() in Client.php line 129
at Client->request('post', 'https://api.mailgun.net/v3//messages.mime', array('auth' => array('api', null), 'multipart' => array(array('name' => 'to', 'contents' => 'dksnowdon#gmail.com'), array('name' => 'message', 'contents' => 'Message-ID: <9975c6b7d34f1fc93864bf7ff15f702a#localhost> Date: Wed, 09 Dec 2015 03:08:38 +0000 From: Laravel <us#example.com> To: dksnowdon#gmail.com MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Text to e-mail', 'filename' => 'message.mime')))) in Client.php line 87
at Client->__call('post', array('https://api.mailgun.net/v3//messages.mime', array('auth' => array('api', null), 'multipart' => array(array('name' => 'to', 'contents' => 'dksnowdon#gmail.com'), array('name' => 'message', 'contents' => 'Message-ID: <9975c6b7d34f1fc93864bf7ff15f702a#localhost> Date: Wed, 09 Dec 2015 03:08:38 +0000 From: Laravel <us#example.com> To: dksnowdon#gmail.com MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Text to e-mail', 'filename' => 'message.mime'))))) in MailgunTransport.php line 79
at Client->post('https://api.mailgun.net/v3//messages.mime', array('auth' => array('api', null), 'multipart' => array(array('name' => 'to', 'contents' => 'dksnowdon#gmail.com'), array('name' => 'message', 'contents' => 'Message-ID: <9975c6b7d34f1fc93864bf7ff15f702a#localhost> Date: Wed, 09 Dec 2015 03:08:38 +0000 From: Laravel <us#example.com> To: dksnowdon#gmail.com MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Text to e-mail', 'filename' => 'message.mime')))) in MailgunTransport.php line 79
at MailgunTransport->send(object(Swift_Message), array()) in Mailer.php line 85
at Swift_Mailer->send(object(Swift_Message), array()) in Mailer.php line 395
at Mailer->sendSwiftMessage(object(Swift_Message)) in Mailer.php line 181
at Mailer->send(array('raw' => 'Text to e-mail'), array(), object(Closure)) in Mailer.php line 133
at Mailer->raw('Text to e-mail', object(Closure)) in Facade.php line 219
at Facade::__callStatic('raw', array('Text to e-mail', object(Closure))) in TestController.php line 17
at Mail::raw('Text to e-mail', object(Closure)) in TestController.php line 17
at TestController->index()
You need to leave the services.php config as default:
'mailgun' => [
'domain' => env('MAILGUN_DOMAIN'),
'secret' => env('MAILGUN_SECRET'),
],
Then in the .env you need to put:
MAILGUN_DOMAIN=yourdomain
MAILGUN_SECRET=yoursecret
Alternatively to kEpEx's answer, you can remove the env() part from services.php config
'mailgun' => [
'domain' => 'sandbox******.mailgun.org',
'secret' => 'key-****',
],
In case someone is struggling with this, I found out after a while that my host was blocking all external mail servers.
They only allowed me to use their own mail servers. Might be worth checking if you're really bashing your head against the wall like I did.
I am trying to send email with attachment in Cakephp 2.6 via Mailgun. It normally working fine unless I add 'attachments($attachments)'. My code is below.
$attachments = SITE_URL . '/img/red-hyphen.png';
$data = array (
'unique_secret_key' => $unique_secret_key,
'WAYDSiteUrl' => $siteUrl
);
$Email = new CakeEmail ( 'mailgun' );
$Email->template ( 'forgotpassword', 'default' )
->emailFormat ( 'html' )
->viewVars ( $data )
->subject ( 'Forgot Password' )
->to ( $chek_exists_record ['Member'] ['email'] )
->attachments($attachments)
->send ();
I am getting error
Error: An Internal Error Has Occurred.
Stack Trace
APP\Controller\MembersController.php line 255 → CakeEmail->attachments(string)
'http://localhost/GYB_develop/gyb_source/img/red-hyphen.png'
[internal function] → MembersController->forgotpassword()
CORE\Cake\Controller\Controller.php line 490 → ReflectionMethod->invokeArgs(MembersController, array)
CORE\Cake\Routing\Dispatcher.php line 193 → Controller->invokeAction(CakeRequest)
CORE\Cake\Routing\Dispatcher.php line 167 → Dispatcher->_invoke(MembersController, CakeRequest)
APP\webroot\index.php line 118 → Dispatcher->dispatch(CakeRequest, CakeResponse)
You should try this Cakephp send email with attachment
email.php file
public $gmail = array(
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username' => 'abc#gmail.com',
'password' => 'xxxxxxxx',
'transport' => 'Smtp'
);
In your Controller At the top import the cakeemail function
App::uses('CakeEmail', 'Network/Email');
After that your function/Action of your controller
$client_email='sharmavikram054#gmail.com'
$Email = new CakeEmail();
$Email->template('doperesult');
$Email->viewVars(array( 'unique_secret_key' => $unique_secret_key,'WAYDSiteUrl' => $siteUrl ));
$Email->emailFormat('html');
$Email->to($client_email);
$Email->from('admin#capeprofileconsultancy.com');
$Email->attachments(array(
'red-hyphen.png' => array(
'file' => WWW_ROOT.'img/red-hyphen.png',
'mimetype' => 'image/png',
'contentId' => 'my-unique-id'
)
));
$Email->subject('Dummy test');
if($Email->send('Send Mail')){
$this->Session->setFlash('Your Result is successfully send','default',array('class'=>'alert alert-success'));
return $this->redirect('/');
}
you can visit the cakephp email link Cakephp email
I am using CakeEmail to send mails on my website, below are my config file info
public $default = array(
'host' => 'smtpout.asia.secureserver.net',
'port' => 25,
'username' => 'xxxxxx',
'password' => 'xxxxxx',
'transport' => 'smtp',
// "tls"=>true
);
It is working fine in my local computer but it is giving the below error on godaddy shared hosting.
Class "smtpTransport" not found.
Error: An Internal Error Has Occurred.
Stack Trace
CORE/Cake/Network/Email/CakeEmail.php line 1265 → CakeEmail->transportClass()
CORE/Cake/Network/Email/CakeEmail.php line 1133 → CakeEmail->_applyConfig(string)
APP/Controller/AppController.php line 46 → CakeEmail->config(string)
APP/Controller/ServicesController.php line 48 → AppController->sendMail(string, string, string, string, array)
[internal function] → ServicesController->consultnow()
CORE/Cake/Controller/Controller.php line 490 → ReflectionMethod->invokeArgs(ServicesController, array)
CORE/Cake/Routing/Dispatcher.php line 191 → Controller->invokeAction(CakeRequest)
CORE/Cake/Routing/Dispatcher.php line 165 → Dispatcher->_invoke(ServicesController, CakeRequest)
APP/webroot/index.php line 108 → Dispatcher->dispatch(CakeRequest, CakeResponse)
Thanks
According to CakePhp's DOC the class is SmtpTransportnot smtpTransport
Autoloader is case sensitive
Reference: http://api.cakephp.org/2.5/class-SmtpTransport.html
I developed a site with CakePHP version 1.2.5 and everything worked fine by then. But now, I'm trying to re-implement my site with CakePHP version 2.2.1.
But the connection to the database (firebird) is not working anymore.
Here is my cake/app/Config/database.php file:
class DATABASE_CONFIG {
var $default = array(
'driver' => 'firebird',
'persistent' => false,
'host' => 'localhost',
'login' => 'SYSDBA',
'password' => 'masterkey',
'database' => 'd:\\IBDatabase\\DATA.GDB',
'port' => '3050',
'connect' => 'ibase_connect'
);
var $test = array(
'driver' => 'firebird',
'persistent' => false,
'host' => 'localhost',
'login' => 'SYSDBA',
'password' => 'masterkey',
'database' => 'd:\\IBDatabase\\DATA.GDB',
'prefix' => '',
);
}
And here is the error I'm getting:
Missing Datasource
Error: Datasource class could not be found.
Notice: If you want to customize this error message, create app\View\Errors\missing_datasource.ctp
Stack Trace
CORE\Cake\Model\ConnectionManager.php line 97 → ConnectionManager::loadDataSource(string)
CORE\Cake\Model\Model.php line 3158 → ConnectionManager::getDataSource(string)
CORE\Cake\Model\Model.php line 1092 → Model->setDataSource(string)
CORE\Cake\Model\Model.php line 3180 → Model->setSource(string)
CORE\Cake\Model\Model.php line 1301 → Model->getDataSource()
CORE\Cake\Model\Model.php line 1389 → Model->schema()
CORE\Cake\Controller\Component\PaginatorComponent.php line 354 → Model->hasField(string)
CORE\Cake\Controller\Component\PaginatorComponent.php line 125 → PaginatorComponent->validateSort(Professionals, array, array)
CORE\Cake\Controller\Controller.php line 1082 → PaginatorComponent->paginate(null, array, array)
APP\Controller\ProfessionalsController.php line 12 → Controller->paginate()
[internal function] → ProfessionalsController->index()
CORE\Cake\Controller\Controller.php line 485 → ReflectionMethod->invokeArgs(ProfessionalsController, array)
CORE\Cake\Routing\Dispatcher.php line 186 → Controller->invokeAction(CakeRequest)
CORE\Cake\Routing\Dispatcher.php line 161 → Dispatcher->_invoke(ProfessionalsController, CakeRequest, CakeResponse)
APP\webroot\index.php line 92 → Dispatcher->dispatch(CakeRequest, CakeResponse)
Does anyone know what I should do?
Thanks!!
After a lot of research, I've found that Cake 2.x is not compatible with firebird.