CakePHP send email - php

I've got a problem with sending mail using CakePHP. Everythings giong well, but i didn't receive any single mail , i tired to send to 2 different emails .
//WebsitesController.php
App::uses('AppController','Controller');
App::uses('CakeEmail','Network/Email');
class WebsitesController extends AppController
{
public $helpers = array('Html','Form','Session');
public $components = array('Email','Session');
public function contact()
{
$this->set('dane', $this->Website->findById(4));
}
public function contact_email()
{ /* all data is taken from contact.ctp, I debuged all data below and it's correct */
$useremail = $this->data['Website']['useremail'];
$usertopic = $this->data['Website']['usertopic'];
$usermessage = $this->data['Website']['usermessage'];
$Email = new CakeEmail();
$Email->from(array($useremail => ' My Site'));
$Email->to('wigan#mail.com');
$Email->subject($usertopic); // all data is correct i checked several times
$Email->send($usermessage);
if($Email->send($usermessage))
{
$this->Session->setFlash('Mail sent','default',array('class'=>'alert alert-success'));
return $this->redirect(array('controller'=>'websites','action'=>'contact'));
}
$this->Session->setFlash('Problem during sending email','default',array('class'=>'alert alert-warning'));
}
}
//contact.ctp
<fieldset>
<?php
echo $this->Form->create('Website',array('controller'=>'websites','action'=>'contact_email'));
echo $this->Form->input('useremail',array('class'=>'form-control'));
echo $this->Form->input('usertopic',array('class'=>'form-control'));
echo $this->Form->input('usermessage',array('class'=>'form-control'));
echo $this->Form->submit('Send',array('class'=>'btn btn-default'));
echo $this->Form->end();
?>
</fieldset>
all seems to be fine, even if statement in function contact_email is approved.
configuration ( i'm working on localhost, xampp, netbeans 7.4)
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',
);

try this, you didn't set the config
public function contact_email()
{ /* all data is taken from contact.ctp, I debuged all data below and it's correct */
$useremail = $this->data['Website']['useremail'];
$usertopic = $this->data['Website']['usertopic'];
$usermessage = $this->data['Website']['usermessage'];
$Email = new CakeEmail();
$Email->config('smtp')
->emailFormat('html')
->from($useremail)
->to('wigan#mail.com')
->subject($usertopic); // all data is correct i checked several times
if($Email->send($usermessage))
{
$this->Session->setFlash('Mail sent','default',array('class'=>'alert alert-success'));
return $this->redirect(array('controller'=>'websites','action'=>'contact'));
} else {
$this->Session->setFlash('Problem during sending email','default',array('class'=>'alert alert-warning'));
}
}

Please follow the steps:
step 1: In this file (app\Config\email.php)
add this:
public $gmail = array(
'transport' => 'Smtp',
'from' => array('site#localhost' => 'Any Text...'),
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'timeout' => 30,
'username' => 'youremail#example.com',
'password' => 'yourPassword',
'client' => null,
'log' => false,
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
);
step 2: Add an email template (app\View\Emails\html\sample.ctp)
<body>
<h1>Email Testing: <?php echo $first_name?></h1>
</body>
step 3: Change the code in your method as shown below:
public function send_my_email() {
App::uses('CakeEmail', 'Network/Email');
$Email = new CakeEmail();
$Email->config('gmail'); //configuration
$Email->emailFormat('html'); //email format
$Email->to('receiveremail#ex.com');
$Email->subject('Testing the emails');
$Email->template('sample');//created in above step
$Email->viewVars(array('first_name'=>'John Doe' ));//variable will be replaced from template
if ($Email->send('Hi did you receive the mail')) {
$this->Flash->success(__('Email successfully send on receiveremail#ex.com'));
} else {
$this->Flash->error(__('Could not send the mail. Please try again'));
}
}

Related

send mail from other configuration if first config fails laravel

I want to create a code such that if one configuration fails to send mail the other config will kick in to send the mail, I have created two configurations, One in the provider which is set on page load the other is a helper function which can be set as we need.
if I set the helper function before sending any mail the function kicks in but if I keep it in try-catch block the helper doesn't work
try {
Mail::to($administrator->email)->send(new ResetPassword($data));
} catch (\Exception $e) {
$mailsenderr = true;
}
if ($mailsenderr) {
$newConfig = new SecondaryMailer;
$newConfig->setmailer();
try {
Mail::to($administrator->email)->send(new ResetPassword($data));
} catch (\Exception $e) {
return [
"status" => 0,
"message" => 'Error sending mail.'
// , "error" => $e->getMessage()
];
}
}
return ["status" => 1, "message" => "Mail sent successflly"];
Figured out how to
Just create a provider and set the code like
$mail = DB::table('mail_variables')->where('mailer_type', 'primary')->where('status', 'active')->first();
try {
$transport = new \Swift_SmtpTransport($mail->host, $mail->port, $mail->encryption);
$transport->setUsername($mail->username);
$transport->setPassword($mail->password);
$mailer = new \Swift_Mailer($transport);
$mailer->getTransport()->start();
} catch (\Swift_TransportException $e) {
$mail = DB::table('mail_variables')->where('mailer_type', 'secondary')->where('status', 'active')->first();
}
$config = array(
'driver' => $mail->driver,
'host' => $mail->host,
'port' => $mail->port,
'from' => array(
'address' => $mail->from_address,
'name' => $mail->from_name
),
'encryption' => $mail->encryption,
'username' => $mail->username,
'password' => $mail->password,
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
);
Config::set('mail', $config);

Email is not send in CakePHP

I am working with CakePHP version 1.3.13.
Here, I want to send mail. So I have written code like this :
function send_mail_to_client()
{
$text = "sending mail";
App::uses('CakeEmail', 'Network/Email');
$Email = new CakeEmail();
$Email->from(array('sachingarala.v2047#gmail.com' => 'My Site'));
$Email->to('nisargbhavsar24#gmail.com');
$Email->subject('Test mail');
$Email->send($text);
}
But, mail is not send to the destination. So what is the wrong in this code?
How can I resolve this problem?
Add the configs after initialization i.e. $Email = new CakeEmail(); like
$Email->config(array(
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username' => 'my#gmail.com',
'password' => 'secret',
'transport' => 'Smtp'
);

Sending different emails in cakephp

I need to send two different emails one for the admin and the other email is a confirmation email for the user to confirm that we have received his request. I don't know how can I exactly send different emails to different email addresses in the same action in cakephp.
Code :
Controller
$Email = new CakeEmail('notifications');
$result = $Email->to(array('admin#example.com'))
->subject(__("Request Notification))
->send($message);
if($result){
$this->redirect('/pages/thankyou');
$companymsg= "Dear,Thank you for you interest we will contact you soon."
$Email = new CakeEmail('usernotifications');
$Email->to(array($email))
->subject(__(" Request"))
->send($companymsg);
}
Email Configuration
public $notifications = array(
'transport' => 'Mail',
'from' => array('notifications-noreply#example.com' => '(Notification)'),
'charset' => 'utf-8',
'headerCharset' => 'utf-8',
'emailFormat' => 'html',
);
public $usernotifications = array(
'transport' => 'Mail',
'from' => array('no-reply#example.com' => 'My Project'),
'charset' => 'utf-8',
'headerCharset' => 'utf-8',
'emailFormat' => 'html',
);
Try:
$Email = new CakeEmail('notifications');
$result = $Email->to(array('admin#example.com'))
->subject(__("Request Notification))
->send($message);
$companymsg= "Dear,Thank you for you interest we will contact you soon."
$Email = new CakeEmail('usernotifications');
$Email->to(array($email))
->subject(__(" Request"))
->send($companymsg);
$this->redirect('/pages/thankyou');

how can i send email in cakephp - to gmail account from localhost

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

getting error Could not send email in cakephp

I've been trying to send emails with Pear on xampp through Gmail, and after spending hours setting it all up and figuring out all the errors I was getting, I thought I was so close, until I started getting this error:
controller action
public function automail() {
App::uses('CakeEmail', 'Network/Email');
$ret_msg = null;
try {
$is_call_email = true;
$subject = "case detail";
$comment = "Ready to Review";
$email_to = "exmaple#gmail.com";
if ($is_call_email == true) {
$email_to = str_replace(' ', '', $email_to);
$email_addresses = preg_split('/[;,]/', $email_to);
$this->log($is_call_email,'bool');
$email = new CakeEmail();
$email->from(array($this->Session->read('Auth.User.email') => $this->Session->read('Auth.User.name')))
->to($email_addresses)
->subject($subject)
->send($comment);
$this->log($subject,'subject');
}
} catch (Exception $ex) {
$ret_msg = $ex->getMessage();
$this->log($ex->getLine(), 'emailError');
}
$this->log('Return msg is = ' . $ret_msg, 'shared');
return;
in email.php
<?php
class EmailConfig {
public $default = array(
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username' => 'example#gmail.com', //example#gmail.com
'password' => 'secret',
'transport' => 'Smtp',
'from' => array('exampe#gmail.com' => 'Nam Email'),
'log' => true
);
}
from and to both are same email addresses because i was send in my account for testing...
please help me or any advice for how to send email using cakephp....
You need to specify $email->config. Like:
$email->config('default')
->from(array($this->Session->read('Auth.User.email') => $this->Session->read('Auth.User.name')))
->to($email_addresses)
->subject($subject)
->send($comment);

Categories