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
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'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!
i'm begginer in this and i found with this problem...i wan to send email from my localhost to a gmail account (this last can change for a hotmail), but first i want to prove for a gmail account.
i had configure my email.php and it seems like this:
public $gmail = array(
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username' => 'xxxxx#gmail.com',
'password' => 'xxxx',
'transport' => 'Smtp'
);
and in my controller i have this
public function compras()
{
$Email = new CakeEmail();
$Email->config('gmail');
$this->loadModel('Soya');
$this->paginate = array(
'conditions' => array('Grupo.categoria' => 'Soya','Grupo.subcategoria' => 'Productor de Oleaginosas'),
'limit' => 25
);
$this->set('soyas', $this->paginate('Soya'));
$this->Email->to = 'xxxx#gmail.com';
$this->Email->subject = 'Include your subject';
$this->Email->from = 'xxxx#gmail.com';
//$this->Email->template = 'template'; // file name template.ctp will be included in /views/elements/email/text/template.ctp
$this->Email->delivery = 'smtp';
if ($this->Email->send()
) {
return true;
} else {
echo $this->Email->smtpError;
}
}
but when i compile the errors appears
**Indirect modification of overloaded property SoyasController::$Email has no effect [APP/Controller/SoyasController.php, line 124]
Error: Call to a member function send() on a non-object **
please help!!!thanks a lot!!!
public function compras()
{
$Email = new CakeEmail();
$Email->config('gmail');
$this->loadModel('Soya');
$this->paginate = array(
'conditions' => array('Grupo.categoria' => 'Soya','Grupo.subcategoria' => 'Productor de Oleaginosas'),
'limit' => 25
);
$this->set(array('soyas', $this->paginate('Soya')));
$Email->to('xxxx#gmail.com');
$Email->subject('Include your subject');
$Email->from('xxxx#gmail.com');
if ($Email->send())
{
return true;
} else {
echo $this->Email->smtpError;
}
}
I recently uploaded a CakePHP 2.3.8 app to a Ubuntu 12.04 EC2 instance and now I cannot send emails when using $Email->send() but I can do it with the "fast" method of CakeEmail::deliver('to#gmail.com', 'Subject', 'Content');, however I have a layout that I want to use.
When I try to send an email I get an error stating "Could not send email.
Error: An Internal Error Has Occurred."
Here is my code for sending the email.
App::uses('CakeEmail', 'Network/Email');
$Email = new CakeEmail();
$Email->from(array('myemailaddress#gmail.com' => 'Me'));
$Email->to('person#gmail.com');
$Email->subject('Test Email');
$Email->template('layout_1');
$Email->emailFormat('html');
$testvalues = array('item1' => 'test1', 'item2' => 'test2');
$Email->viewVars(array('tesvalues'=> $testvalues));
$Email->send();
$this->Session->setFlash('Email has been sent');
$this->redirect($this->referer(), null, true);
In /App/Config/email.php here is what I have for smtp
public $smtp = array(
'transport' => 'Smtp',
'from' => array('me#gmail.com' => 'Me'),
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'timeout' => 30,
'username' => 'me#gmail.com',
'password' => '****',
'client' => null,
'log' => false,
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
);
The exact line that the stack trace flags is in CORE/Cake/Network/Email/MailTransport.php
$this->_mail($to, $email->subject(), $message, $headers, $params);
I checked the error log and it says
Error: [SocketException] Could not send email.
I am going to shoot in the dark here.
It seems to me that you are not setting the layout correctly, according to the documentation you need to tell cake the layout and the view you want to use, for example:
$Email = new CakeEmail();
$Email->template('welcome', 'fancy')
->emailFormat('html')
->to('bob#example.com')
->from('app#domain.com')
->send();
The above would use app/View/Emails/html/welcome.ctp for the view, and app/View/Layouts/Emails/html/fancy.ctp for the layout.
Anyways, I recommend taking a look at Cake logs (app/tmp/logs) and see the cause of your error