I was using the following code snippet to send mails, but moving to another host messed everything up:
public function forgetpwd(){
//code omitted
if($this->User->saveField('token_hash',$fu['User']['token_hash'] )){
//============Email================//
/* SMTP Options */
$this->Email->smtpOptions = array(
'host' => 'smtp.gmail.com',
'port'=>'465',
'transport' => 'Smtp',
'username'=>'email#gmail.com',
'password'=>'secret',
'tls' => true
);
$this->Email->template = 'resetpw';
$this->Email->from = 'noreply#email.com';
$this->Email->to = $fu['User']['name'].'<'.$fu['User']['email'].'>';
$this->Email->subject = __('Recover your password');
$this->Email->sendAs = 'both';
$this->Email->delivery = 'smtp';
$this->set('ms', $ms);
$this->Email->send();
$this->set('smtp_errors', $this->Email->smtpError);
$this->Session->setFlash(__('Check your email to recover your password'));
}
//============EndEmail=============//
So there is this view forgetpwd.ctp where user is asked to enter his/her email address, and when trying to send the email, I am given the two following errors:
Error: The view for UsersController::forgetpwd() was not found.
Error: Confirm you have created the file: /home/public_html/development/app/View/Emails/text/resetpw.ctp
I am sure that the resetpw.ctp exists in the right place.
I have edited the email config and tried to adopt the 2.3 cakePHP way, and this is what I have ended up with:
if($this->User->saveField('token_hash',$fu['User']['token_hash'] )){
App::uses('CakeEmail', 'Network/Email');
$Email = new CakeEmail('default');
$Email->to($fu['User']['email'])
->subject('Reset your password')
->message($key)
->send();
$this->Session->setFlash(__('Check your mail to reset your password'));
$this->redirect(array('controller'=>'users','action'=>'reset'));
But now, when I try to send an email, I get the following error:
Call to a member function send() on a non-object
change
'host' => 'smtp.gmail.com',
to
'host' => 'ssl://smtp.gmail.com',
also put your config info in email.php inside App/Config/ as referenced here:
http://book.cakephp.org/2.0/en/core-utility-libraries/email.html#configuration
Related
We've recently updated an application from Cakephp 1.3 to version 2.8.
We use the application to send order information email messages to customers via a separate SMTP server. There can be as many as 1,000 messages sent at one time.
Our 1.3 application used to bulk send happily without ever incurring a 550 Maximum recipient quota exceeded message, even for volumes that exceeded the maximum recipient level on the SMTP server. The SMTP server has not changed. The max recipients level is set to 500 per hour, as it always has been.
Cakephp version 2 has a new method of dealing with email (CakeEmail):
http://book.cakephp.org/2.0/en/core-utility-libraries/email.html
There must be a fundamental difference in the ways that Cakephp 1.3 and Cakephp 2.8 sends email via an SMTP method, but I'm really struggling to see it. Can anyone help please?
Relevant code is shown below
/// 1.3 Code
$config['smtp'] = array(
'port'=>'25',
'timeout'=>'30',
'host' => 'mail.smtpservername.com',
'username'=>'smtp#domainname.com',
'password'=>'password'
);
private function send_email($customer, $email_message) {
$this->Email->from = 'Sender Name <address#domain.com>';
$this->Email->smtpOptions = array(
'port'=>Configure::read('smtp.port'),
'timeout'=>Configure::read('smtp.timeout'),
'host' => Configure::read('smtp.host'),
'username'=>Configure::read('smtp.username'),
'password'=>Configure::read('smtp.password')
);
$this->Email->delivery = 'smtp';
$this->Email->to = $customer['first_name'].' '.$customer['last_name'].' <'.$customer['email_address'].'>';
$this->Email->subject = 'A nice subject line';
$this->Email->sendAs = 'text';
$this->Email->template = 'simple_message';
$this->set('email_content', $email_message);
if (!$this->Email->send()) {
CakeLog::write('error', "Sending email to customer {$customer['id']} failed.");
throw new EmailNotSentException("Sending email to customer {$customer['id']} failed.\n\n".$email_message);
}
$this->Email->reset();
return true;
}
/// 2.8 Code
$config['smtp'] = array(
'port' => '25',
'timeout' => '30',
'host' => 'mail.smtpservername.com',
'username' => 'smtp#domainname.com',
'password' => 'password',
'transport' => 'Smtp'
);
private function send_email($customer, $email_message)
{
$Email = new CakeEmail;
$Email->config(Configure::read('smtp'));
$from = array('address#domain.com' => 'Sender Name');
$recipient = array($customer['email_address'] => $customer['first_name'] . ' ' . $customer['last_name']);
$subject = 'A nice subject line';
$Email->from($from);
$Email->to($recipient);
$Email->subject($subject);
$Email->emailFormat('text');
$Email->template('simple_message');
$Email->viewVars(array('email_content' => $email_message));
if (! $Email->send()) {
CakeLog::write('error', "Sending email to customer {$customer['id']} failed.");
throw new EmailNotSentException("Sending email to customer {$customer['id']} failed.\n\n".$email_message);
}
$Email->reset();
return true;
}
I can't send an attachment with the email. I don't get an error and I do send the message so the email works but no attachment.
Is my filepath not correct as the file exists in this file? Is it because I am using windows with file paths?
This is just a test email below to see if this function actually works for an attachment but it isn't working for me. I checked other answers and this seems to be the way to construct this.
http://book.cakephp.org/2.0/en/core-utility-libraries/email.html
public function sendEmailattach($to,$message,$subject,$attach) {
$Email = new CakeEmail();
$Email->config('gmail3');
$Email->filePaths = array('D:\crm5\app\Attachments');
$Email->attachments =array('Ch9-anna tax.docx');
$to='jXXXXX#gmail.com';//testing real email account
// $Email->from( array('admin#a.com.au' => 'A'));
$Email->from( array('jxxxxx#gmail.com' => 'test'));
$Email->to($to);
$Email->subject($subject);
$Email->send();
// $Email->send($message);
}//public
UPDATE
Tried all 3 methods and no error and no attachment?
// $Email->attachments('D:\AA-website design\crm5\app\Attachments\Ch9-anna tax.docx') ;
$Email->attachments(array('Ch9-anna tax.docx' => array(
'file' => 'D:\AA-website design\crm5\app\Attachments\Ch9-anna tax.docx',
) ));
// $Email->attachments(array('D:\AA-website design\crm5\app\Attachments\Ch9-anna tax.docx'));
$email = new CakeEmail('default');
$attachment = [
'file.pdf' => [
'file' => '/my/absolute/path/on/server/file.pdf',
'mimetype' => 'application/pdf',
'contentId' => uniqid()
]
];
$variables = ['emailHeader' => 'Hello'];
$email->attachments($attachment);
$email->from(['info#example.com' => 'Example'])
->to('recipient#example.com')
->subject('Subject')
->template('template')
->emailFormat('both')
->viewVars($variables)
->send();
Just read the manual instead just looking at the links, honestly I have doubt's you read it at all:
http://book.cakephp.org/2.0/en/core-utility-libraries/email.html#sending-attachments
It clearly shows that attachments is a method and not a property. It even has examples of what the method accepts.
Hello Everyone,
I am trying to send email through cakephp framework,but unfortunately email are not getting delivered. But when i use social email service provider such as google, outlook and rediffmail e-mail gets delivered.
My SMTP port is 465. I am using below cakephp mail()function:
public $gmail = array(
'host' => 'ssl://smtp.techphant.com',
'port' => 465,
'username' => 'xyz#abc.com',
'password' => 'xxxxxx',
'transport' => 'smtp',
);
Also i have tried port number 2525 and 25 but to no avail.
Please let me know your suggestions.
Thanks in advance.
Try to send eamil with cake Email component not with mail() function.
$this->set(
'content',
'<h1>Hi </h1>
<p>Thanks for contact us. We will respond you soon</p>
'
);
$this->Email->smtpOptions = array(
'host' => 'ssl://smtp.techphant.com',
'port' => 465,
'username' => 'xyz#abc.com',
'password' => 'xxxxxx',
'timeout' => 30,
'client' => null
);
$this->Email->to = 'abc#gmail.com';
$this->Email->subject = 'Thanks for contact us';
$this->Email->from = 'no_reply#abc.com';
$this->Email->template = 'default';
$this->Email->sendAs = 'html';
$this->Email->send();
Thanks..!
I'm trying to send an email from AppController in my CakePHP 2.0 app. It works fine if I send it from the PagesController, but I need to be able to send from AppController as well.
I have:
function sendSystemEmail($to = EMAIL_CONTACT, $from = EMAIL_FROM, $subject = null, $body = null, $view = null, $vars = null) {
App::uses('CakeEmail', 'Network/Email');
$email = new CakeEmail();
$email->viewVars(array(
'body' => $body,
'vars' => $vars
));
$email->template($view)
->emailFormat('html')
->from($from)
->to($to)
->subject($subject)
->send();
return;
}
When I use this, I don't get any errors, but the email doesn't arrive. I can't see that there's any different between this and the code I have in PagesController, so I'm assuming that there's something that AppController doesn't have access to maybe? I can't figure out what though!
Sharon, you're not putting the configuration method.. this parameter can put in the constructor of classEmail too, this way..
$email = new CakeEmail('pop3'); //can be smtp or the protocole you are using..
or
$email->config('pop3')
and you need to define the connection here..
./app/Config/email.php
class EmailConfig {
public $pop3 = array(
'transport' => 'Mail',
'from' => 'you#localhost',
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
);
In Cake 1.3, the EmailComponent did what it should do. The new Cake Email class in 2.0 turned out to be a frustration....No emails sent, No errors....vague documentation...
I have tried all possible variants, tried it with my SMTP, Mail() and Gmail, nothing happens. Hereby my latest attempt:
Controller snippet:
//on top of page
App::uses('CakeEmail', 'Network/Email');
//in method
$email = new CakeEmail();
$email->template('contact_email')
->emailFormat('text')
->to('my#gmail.com')
->from('other#gmail.com')
->send();
Email.php Config file:
class EmailConfig {
//It also does not work with a constructor
public $gmail = array(
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username' => 'my#gmail.com',
'password' => '***',
'transport' => 'Smtp'
);
Can someone please post WORKING code for the Email Class. Many thanks
I think you have to load your config from Config/email.php explicitly, it is not loaded automatically, not even the default config:
$email = new CakeEmail();
$email->config('default');
//or in constructor::
$email = new CakeEmail('default');
In my opinion you should use this:
$email = new CakeEmail('gmail');
This is my email config file . I didnt do any change here
class EmailConfig {
public $default = array(
'transport' => 'Mail',
'from' => 'Admin <no-reply#example.com>',
'charset' => 'utf-8',
'headerCharset' => 'utf-8',
);
}
This is how i send the mail
$email = new CakeEmail();
$result = $email->template('welcome')
->emailFormat('text')
->to($NewUser['email'])
->from('admin#example.com')
->send();
var_dump($result);