Please how to set copy email status (order done, payment,..) to the administrator?
Override function send() from class Mail.
Set $bcc parameter and call the parent function:
public static function send(
$idLang,
$template,
$subject,
$templateVars,
$to,
$toName = null,
$from = null,
$fromName = null,
$fileAttachment = null,
$mode_smtp = null,
$templatePath = _PS_MAIL_DIR_,
$die = false,
$idShop = null,
$bcc = null,
$replyTo = null,
$replyToName = null
) {
$bcc = 'your#mail.com';
parent:: send($idLang, $template, $subject, $templateVars, $to, $toName, $from, $fromName, $fileAttachment, $mode_smtp, $templatePath, $die, $idShop, $bcc, $replyTo, $replyToName);
}
Related
I've been using this library for sending mail
https://github.com/PHPMailer/PHPMailer
But I'm not getting the Email Send
Invalid address: (From): XXX
Array ( [error] => 1 [message] => Mailer Error: Invalid address: (From): XXX )
What I did : Disable 2 Step verification into Gmail
And Enabled Allow Less Secure Apps On
PagesController.php
<?php
namespace App\Controller;
class PagesController extends AppController
{
public $components = array("Email");
public function index()
{
$to = 'myemail#gmail.com';
$subject = 'Hi buddy, i got a message for you.';
$message = 'Nothing much. Just test out my Email Component using PHPMailer.';
try {
$mail = $this->Email->send_mail($to, $subject, $message);
print_r($mail);
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
exit;
}
}
EmailComponent.php
<?php
namespace App\Controller\Component;
use Cake\Controller\Component;
use Cake\Core\App;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require ROOT. '/vendor/phpmailer/phpmailer/src/Exception.php';
require ROOT. '/vendor/phpmailer/phpmailer/src/PHPMailer.php';
require ROOT. '/vendor/phpmailer/phpmailer/src/SMTP.php';
class EmailComponent extends Component {
public function send_mail($to, $subject, $message)
{
// date_default_timezone_set('Asia/Calcutta');
$sender = "XXX"; // this will be overwritten by GMail
$header = "X-Mailer: PHP/".phpversion() . "Return-Path: $sender";
$mail = new PHPMailer();
$mail->SMTPDebug = 2; // turn it off in production
$mail->IsSMTP();
$mail->Host = "smtp.gmail.com";
$mail->SMTPAuth = true;
$mail->Username = "XXX";
$mail->Password = "XXX";
$mail->SMTPSecure = "tls"; // ssl and tls
$mail->Port = 587; // 465 and 587
$mail->SMTPOptions = array (
'tls' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
),
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->From = $sender;
$mail->FromName = "From Me";
$mail->AddAddress($to);
$mail->IsHTML(true);
$mail->CreateHeader($header);
$mail->Subject = $subject;
$mail->Body = nl2br($message);
$mail->AltBody = nl2br($message);
// return an array with two keys: error & message
if(!$mail->Send()) {
return array('error' => true, 'message' => 'Mailer Error: ' . $mail->ErrorInfo);
} else {
return array('error' => false, 'message' => "Message sent!");
}
}
}
Fixed I had to replace
thoses three lines in EmailComponent
$sender = "xxx#gmail.com";
$mail->Username = "xxx#gmail.com";
$mail->Password = "GMAIL_PASSWORD";
I am working in cakephp and want to send confirmation link on user signup but i do not know much about SMTP.
Here is What i have written I am using Token to confirm email which will expire next time if user hit the same confirmation link.
Here is usercontroller/signup method:
public function signup()
{
$this->layout = 'main';
if ($this->request->is('post')) {
$this->User->create();
$this->request->data['User']['password'] = AuthComponent::password($this->request->data['User']['password']);
$hash = sha1($this->request->data['User']['username'] . rand(0, 100));
$this->request->data['User']['tokenhash'] = $hash;
if ($this->User->validates()) {
$this->User->save($this->request->data);
$ms = 'Click on the link below to complete registration ';
$ms .= 'http://localhost/FindTutor/users/verify/t:' . $hash . '/n:' . $this->data['User']['username'] . '';
$ms = wordwrap($ms, 70);
$this->Email->from = 'usman.jamil0308#gmail.com';
$this->Email->to = $this->request->data['User']['email'];
$this->Email->subject = 'Confirm Registration..';
$this->Email->send($ms);
$this->Session->setFlash('Please Check your email for validation Link');
$this->redirect('/users/login');
}
}
}
Here is users/verify method to confirm if user hit the confirmation link.
public function verify(){
//check if the token is valid
if (!empty($this->passedArgs['n']) && !empty($this->passedArgs['t'])){
$name = $this->passedArgs['n'];
$tokenhash = $this->passedArgs['t'];
$results = $this->User->findByUsername($name);
if ($results['User']['activate']==0){
//check the token
if($results['User']['tokenhash']==$tokenhash)
{
$results['User']['activate']=1;
//Save the data
$this->User->save($results);
$this->Session->setFlash('Your registration is complete');
$this->redirect('/users/login');
exit;
}
else{
$this->Session->setFlash('Your registration failed please try again');
$this->redirect('/users/register');
}
}
else {
$this->Session->setFlash('Token has alredy been used');
$this->redirect('/users/register');
}
}else
{
$this->Session->setFlash('Token corrupted. Please re-register');
$this->redirect('/users/register');
}
}
Error is somthing like this:
mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()
try to use mailjet and configure your sever (wamp or xampp)the directorie sendmail and configure your app.php like that
'EmailTransport' => [
'default' => [
'className' => 'Mail',
// The following keys are used in SMTP transports
'host' => 'localhost',
'port' => 25,
'timeout' => 30,
'username' => '',
'password' => '',
'client' => null,
'tls' => null,
],
'mailjet' => [
'className' => 'smtp',
// The following keys are used in SMTP transports
'host' => 'in-v3.mailjet.com',
'username' => 'copy that from your account mailjet',
'password' => 'copy that from your account mailjet',
'port' => 587,
'timeout' => 3000,
'client' => null,
'tls' => null,
],
],
'Email' => [
'default' => [
'transport' => 'mailjet',
'from' => 'xxxxxx#gmail.com',
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
],
],
public $smtp = array(
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username' => 'your email id',
'password' => 'your password',
'transport' => 'Smtp',
);
App::uses('CakeEmail', 'Network/Email');
$Email = new CakeEmail('smtp');
$Email->from('info#email.com');
$Email->to($email);
$message = "hello";
$Email->send($message);
First Include Email Component in your App Controller like this :
public $components = array(
'Email'
);
Create a sendMail function in your App controller like this
public function _sendMail( $to, $from, $replyTo, $subject, $element,$parsingParams = array(),$attachments ="", $sendAs = 'html', $bcc = array()){
$port = '';
$timeout = '';
$host = '';
$username = '';
$password = '';
$client = '';
$toAraay = array();
if (!is_array($to)) {
$toAraay[] = $to;
} else {
$toAraay = $to;
}
$this->Email->smtpOptions = array(
'port' => "$port",
'timeout' => "$timeout",
'host' => "$host",
'username' => "$username",
'password' => "$password",
'client' => "$client"
);
$this->Email->delivery = 'smtp';
foreach ($parsingParams as $key => $value) {
$this->set($key, $value);
}
foreach ($toAraay as $email) {
$this->Email->to = $email;
$this->Email->subject = $subject;
$this->Email->replyTo = $replyTo;
$this->Email->from = $from;
if(!empty($bcc)){
$this->Email->cc = $bcc[0];
}
if ($attachments!="") {
$this->Email->attachments = array();
$this->Email->attachments[0] = $attachments ;
}
$this->Email->template = $element;
$this->Email->sendAs = $sendAs;
$this->Email->send();
$this->Email->reset();
}
}
Create a sendmail.ctp file in View/Emails/html
Add this content in file and also add your header or footer
<?php echo $message; ?>
Now whenever you want to send email call this function like this :
$this->_sendMail($to, $from, $replyTo, $subject, 'sendmail', array('message' => $message), "", 'html', $bcc = array());
Now you can implement your logic for verification email like this :
$message = 'Click on the link below to complete registration ';
$message .= 'http://localhost/FindTutor/users/verify/t:' . $hash . '/n:' . $this->data['User']['username'] . '';
$from = 'usman.jamil0308#gmail.com';
$to = $this->request->data['User']['email'];
$subject = 'Confirm Registration..';
$replyTo = 'usman.jamil0308#gmail.com';
$this->_sendMail($to, $from, $replyTo, $subject, 'sendmail', array('message' => $message), "", 'html', $bcc = array());
I want to apply this function to all posted data in zend frame work to prevent XSS attacks.
static function safe_against_xss($argument) {
$HtmlEntities_Filter = new Zend_Filter_HtmlEntities ( array ('quotestyle' => NULL, 'charset' => 'UTF-8' ) );
$argument = $HtmlEntities_Filter->filter ( $argument );
return $argument;
}
I use this code in my controller
$requests = $request->getPost() ;
foreach ($requests as $key => $value)
{
$requests[$key]=Functions::safe_against_xss($value);
}
It's worked,but i want to apply this function to all posted data in all controllers. automatically.
Sincerely
I write this codes:
$this->setRequest(Functions::safe_request($this->getRequest()));
In init of controllers
Then in Functions:
static function safe_against_xss($argument) {
// $HtmlEntities_Filter = new Zend_Filter_HtmlEntities ( NULL, 'UTF-8'
// );
$HtmlEntities_Filter = new Zend_Filter_HtmlEntities ( array ('quotestyle' => NULL, 'charset' => 'UTF-8' ) );
if (is_array($argument))
{
foreach($argument as $key => $value) {
$argument[$key] = $HtmlEntities_Filter->filter ( $value );
}
}
else
{
$argument = $HtmlEntities_Filter->filter ( $argument );
}
return $argument;
}
static function safe_post_params($params)
{
$safePostParams = array();
foreach($params as $key => $value) {
$safePostParams[$key] = self::safe_against_xss($value);
}
return $safePostParams;
}
static function safe_request($params)
{
$params->setParams(Functions::safe_post_params($params->getParams()));
$params->setPost(Functions::safe_post_params($params->getPost()));
return $params;
}
I'm having problems sending a mail with cakephp2, I know I can send emails because I have my postfix configured, and I can send e-mails with command line or php. So please, can you send me an example of cakephp2 sending emails.
This is the error message
Invalid email: "you#localhost"
Error: An Internal Error Has Occurred.
I've also tried with the ssl via gmail and it doesn't work either, and it's giving me a really hard time.
thanks guys
by the way, I'm trying the exact example of this url http://book.cakephp.org/2.0/en/core-utility-libraries/email.html
Your app/config/Email.
class EmailConfig {
public $gmail = array(
'port' => '465',
'timeout' => '300',
'host' => 'ssl://smtp.gmail.com',
'username' => '<your_email>#gmail.com',
'password' => '<you_password>',
'transport' => 'Smtp'
); }
your file = app/controller/appController.php insert this function
public function sendEmail($type, $options){
try {
$Email = new CakeEmail($type);
$Email->config($options);
$Email->template = "email_confirmation";
$Email->emailFormat('html');
//$this->idCrudRash = $options;
$Email->send();
} catch (SocketException $e) {
die('Erro ao enviar email:'. $e->getMessage());
$this->log(sprintf('Erro ao enviar email: %s', $e->getMessage()));
}
}
for user: app/controller/contato.php
$options = array(
'emailFormat' => 'html',
'from' => array(
$config['email_noanswer'] => $config['site_name']
),
'subject' => 'Confirmação de Cadastro',
'to' => $this->request->data['User']['email'],
//'template' => 'default',
'template' => 'email_confirmation',
'viewVars' => array(
'title_for_layout' => 'Confirmação de Email ' . $config['site_name'],
'name' => $this->request->data['User']['name'],
'email' => $this->request->data['User']['email'],
//'cpf' => base64_encode($this->request->data['User']['cpf']),
'site_name' => $config['site_name'],
),
);
$this->sendEmail('gmail', $options);
In your email.php file, please remove the default 'from' value, it overrides your passed param.
public $default = array(
'transport' => 'Mail',
'from' => 'you#localhost', // remove this line
...
);
In app/config/Email
public $smtp = array(
'transport' => 'Smtp',
'from' => array('no-reply#xyz.com' => 'no-reply#xyz.com'),
'host' => 'ssl://smtp.abc.com',
'port' => 465,
'timeout' => 30,
'username' => 'username',
'password' => 'password',
'client' => null,
'log' => false,
);
In Your Controller
App::uses('AppController', 'Controller');
App::uses('CakeEmail', 'Network/Email');
public function index()
{
$this->layout = 'layout';
$this->set('title', "Title");
if ($this->request->is('Post')) {
if (!empty($this->request->data)) {
if ($this->Modal->Save($this->request->data)) {
$to = 'test#anc.com';
$subject = 'Your_subject';
$message = $this->request->data;
if ($this->sendmail($to, $subject, $message)) {
echo"sent";die;
}
} else {
echo"wrong";die;
}
}
}
}
public function sendmail($to = null, $subject = '', $messages = null, $ccParam = null, $from = null, $reply = null, $path = null, $file_name = null)
{
$this->layout = false;
$this->render(false);
$name = $messages['Modalname']['name'];
$email = $messages['Modalname']['email'];
$Email = new CakeEmail();
$Email->config('smtp');
$Email->viewVars(array('name' => $name, 'email' => $email));
$Email->template('comman_email_template', 'comman_email_template');
return $Email->emailFormat('html')
->from(array('no-reply#xyz.com' => 'no-reply#xyz.com'))
->to($to)
->subject($subject)
->send();
}
Create a layout and view for email template.and add the data value that have been sent.
I dont know why this particular bit of code is simply not updating despite using this style of coding elsewhere succesfully.No exception is being thrown.
CREATE TABLE `accounts` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`accounts_username` varchar(60) DEFAULT NULL,
`accounts_fullname` varchar(600) DEFAULT NULL,
`accounts_email` varchar(600) DEFAULT NULL,
`accounts_password` varchar(600) DEFAULT NULL,
`accounts_status` varchar(600) DEFAULT NULL,
`accounts_roles` varchar(600) DEFAULT NULL,
`accounts_comments` text,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1
In my form I have the code
public function __construct($options = null)
{
parent::__construct($options);
$optionsrole = array(
'guest' => 'guest',
'user' => 'user',
'writer' => 'writer',
'admin' => 'admin'
);
$optionsstatus = array(
'active' => 'active',
'pending' => 'pending'
);
$this->setName('account');
$id = new Zend_Form_Element_Hidden('id');
$username = new Zend_Form_Element_Text('accounts_username');
$username->setLabel('Username')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$fullname = new Zend_Form_Element_Text('accounts_fullname');
$fullname->setLabel('Fullname')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$email = new Zend_Form_Element_Text('accounts_email');
$email->setLabel('Email')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty')
->addValidator(new Zend_Validate_EmailAddress());
$password = new Zend_Form_Element_Password('accounts_password');
$password->setLabel('Password')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$status = new Zend_Form_Element_Select('accounts_status');
$status->setLabel('Status')
->setRequired(true)
//->addMultiOptions(array('Active','Pending'));
->addMultiOptions($optionsstatus);
$role = new Zend_Form_Element_Select('accounts_roles');
$role->setLabel('Role')
->setRequired(true)
//->addMultiOptions(array('guest','user','writer','admin'));
->addMultiOptions($optionsrole);
$comments = new Zend_Form_Element_Textarea('accounts_comments');
$comments->setLabel('Comments')
//->setAttrib('size',200)
->setAttrib('rows',20)
->setAttrib('cols',50);
$submit = new Zend_Form_Element_Submit('submit');
$submit->setAttrib('id', 'submitbutton');
$this->addElements(array($id, $username, $fullname,$email,
$password,$status,$role,$comments,$submit));
}
In my accounts controller
public function editAction()
{
$this->view->title = "Edit User Account";
$this->view->headTitle($this->view->title, 'PREPEND');
$form = new Form_Account();
$form->submit->setLabel('Save');
$accounts = new Model_DbTable_Account();
$this->view->form = $form;
if ($this->getRequest()->isPost()) {
$formData = $this->getRequest()->getPost();
if ($form->isValid($formData)) {
/*
if($accounts->checkUnique($formData['accounts_username'])){
$this->view->errorMessage = "Username already taken. Please choose another one.";
return;
}
*/
$id = (int)$form->getValue('id');
$username = $form->getValue('accounts_username');
$fullname = $form->getValue('accounts_fullname');
$email = $form->getValue('accounts_email');
$password = $form->getValue('accounts_password');
$status = $form->getValue('accounts_status');
$roles = $form->getValue('accounts_roles');
$comments = $form->getValue('accounts_comments');
//$accounts = new Model_DbTable_Account();
$accounts->updateaccount($username, $fullname,$email,
$password,$status,$roles,$comments);
$this->_redirect('/account');
} else {
$form->populate($formData);
}
} else {
$id = $this->_getParam('id', 0);
if ($id > 0) {
$account = new Model_DbTable_Account();
$form->populate($account->getaccount($id));
}
}
}
In my model
public function updateaccount($id,$username, $fullname, $email,
$password,$status,$roles,$comments)
{
$data = array(
'accounts_username' => $username,
'accounts_fullname' => $fullname,
'accounts_email' => $email,
'accounts_password' => md5($password),
'accounts_status' => $status,
'accounts_roles' => $roles,
'accounts_comments' => $comments,
);
$this->update($data, 'id = '. (int)$id);
}
Am i missing something?
I did find the problem.I was not passing the $id creteria and therefore no record was been updated.
$accounts->updateaccount($username, $fullname,$email,
$password,$status,$roles,$comments);
It should have instead read
$accounts->updateaccount($id,$username, $fullname,$email,
$password,$status,$roles,$comments);