Value undefined - email

With my laravel project I'm trying to send an email with the following code:
$mailTo = "theemail#hotmail.com";
$mailToName = "hisname";
Mail::send('emails.message.showmessage', array( 'name' => $mailToName), function($message)
{
$message->to($mailTo, $mailToName)->subject('New message');
});
But I get the error:
Undefined variable: mailTo
How is this possible? I clearly set the variable with Undefined variable: mailTo, is it being unset in the Mail function?

To use a local variable inside a closure (aka anonymous function) you need to use use()
Mail::send('emails.message.showmessage', array( 'name' => $mailToName), function($message) use ($mailTo, $mailToName)
{
$message->to($mailTo, $mailToName)->subject('New message');
});
All variables specified in use will be available inside the closure all other won't.

Related

Unable to supply complex form values using WWW::Mechanize

I have an HTML form that I want to process with the WWW::Mechanize module. It seems that I can't POST an input value that contains a dollar sign $. I get this run time error message:
Can't call method "value" on an undefined value at script.pl line 599
My script looks like this
my $agent = WWW::Mechanize->new( autocheck => 1, ssl_opts => { verify_hostname => 0 }, );
my $formfiller = WWW::Mechanize::FormFiller->new();
$agent->env_proxy();
$agent->get('https://site.com/form.php');
$agent->form_number(1) if $agent->forms and scalar #{$agent->forms};
$agent->form_number(1);
{
local $^W;
$agent->current_form->value('page_contentleft_0$form_D69D9E5239FC412ABB92A81D73F690AD$field_F40A1785D7B3490CBD5E72EDBE6B966D', 'John Doe');
}; #// First NAme
{
local $^W;
$agent->current_form->value('page_contentleft_0$form_D69D9E5239FC412ABB92A81D73F690AD$field_E3316C46A4404C73ACBAE107DF6206D2', 'Bridgeport');
}; #// City
{
local $^W;
$agent->current_form->value('page_contentleft_0$form_D69D9E5239FC412ABB92A81D73F690AD$field_E5747EB507E74DC1937557F9285CB57C', 'CT');
}; #// state
{
local $^W;
$agent->current_form->value('page_contentleft_0$form_D69D9E5239FC412ABB92A81D73F690AD$field_A612569BE44C4BA3AD0AB3FBF8FB0553', '06604');
}; #// Zipcode
$agent->submit(Submit);
As far as I can see the problem is from the parameters to the value method, for instance
page_contentleft_0$form_D69D9E5239FC412ABB92A81D73F690AD$field_A612569BE44C4BA3AD0AB3FBF8FB0553
How can I make Perl pass through the $, or to fix it in another way?
Are you sure you have?
$agent->submit(Submit);
Try this code instead:
$agent->get('https://site.com/form.php');
$agent->submit_form(
with_fields => {
'page_contentleft_0$form_D69D9E5239FC412ABB92A81D73F690AD$field_F40A1785D7B3490CBD5E72EDBE6B966D' => 'John Doe',
'page_contentleft_0$form_D69D9E5239FC412ABB92A81D73F690AD$field_E3316C46A4404C73ACBAE107DF6206D2' => 'Bridgeport',
'page_contentleft_0$form_D69D9E5239FC412ABB92A81D73F690AD$field_E5747EB507E74DC1937557F9285CB57C' => 'CT',
'page_contentleft_0$form_D69D9E5239FC412ABB92A81D73F690AD$field_A612569BE44C4BA3AD0AB3FBF8FB0553' => '06604'},
button => "Submit",);

phpseclib createKey() using own primes

Is it possible to generate private and public key in PKCS#1 format using phpseclib and my own primes? I mean I already have p and q and I want to generate both keys. I am trying to do something like this:
$p = new Math_BigInteger(...);
$q = new Math_BigInteger(...);
$custom_primes = serialize(array('primes'=>array(1=>$p,2=>$q)));
extract($rsa->createKey(512,10,$custom_primes));
But this gives me the fatal error:
Fatal error: Call to a member function divide() on a non-object in /volume1/web/phpseclib/Crypt/RSA.php on line 705
I checked this and it is trying to divide:
list($temp) = $lcm['top']->divide($lcm['bottom']);
Obviously I am not setting up lcm in my $custom_primes structure as I only have my two primes. So the question is: is it possible at all in phpseclib?
It looks like you're trying to use phpseclib's partial key functionality to achieve this. Problem is that expects more than just primes. See this, for example:
return array(
'privatekey' => '',
'publickey' => '',
'partialkey' => serialize(array(
'primes' => $primes,
'coefficients' => $coefficients,
'lcm' => $lcm,
'exponents' => $exponents
))
);
$lcm is defined, initially, like this:
$lcm = array(
'top' => $this->one->copy(),
'bottom' => false
);
So maybe try doing that as well. You can probably strip out all of the calculation functions
from phpseclib, do them yourself and then pass $partial into phpseclib and let it generate a key in whatever format you want it generated in.

How to add parameters to controller redirect?

In Zend Framework 2 I want to pass an array of parameters from one action to another within the same controller which I did in ZF1 in the following manner:
$this->_helper->redirector->gotoSimple('foo', null, null, $params);
and in fooAction:
$params = $this->_request->getParams();
In ZF2, trying the various answers I have seen here on SO, I came up with the following:
$this->redirect()->toRoute('home/default', array(
'controller' => 'client',
'action' => 'foo',
'param' => 'bar'),
array('param' => 'bar'));
(trying both the $params and $options arguments of toRoute())
and in fooAction:
$param = $this->getEvent()->getRouteMatch()->getParams();
or
$param = $this->params()->fromRoute());
None works for me. Is there a simple way to achieve what I want (passing parameters with a redirect) or should I go the route of using a container, session or even global variables?
You could use the forward plugin:
http://framework.zend.com/manual/2.0/en/modules/zend.mvc.plugins.html#the-forward-plugin
public function someAction()
{
$returnValue = $this->forward()->dispatch('application/controller/index', array(
'action' => 'other'
));
return $returnValue;
}
public function otherAction()
{
return 99;
}
You will be able to pass parameters too
In the end, what I did was, instead of using route parameters, using query parameters, since the parameters I used where not route related. That solved the problem.

Cucumber (with watir-webdriver): undefined method `confirmation_token'

How cann I use confirmation token in cucumber? I need this to test a registration without to check an e-mail.
My code:
#b.text_field(:name => 'profile[prename]').set 'Kurt'
#b.text_field(:name => 'profile[surname]').set 'Russell'
#b.text_field(:id => 'profile_email').set 'user#trash-mail.com'
#b.text_field(:id => 'profile_password').set 'password'
#b.text_field(:name => 'profile[password_confirmation]').set 'password'
#b.button(:id => 'profile_submit').click
#ctoken = Profile.last.confirmation_token
#b.goto("http://localhost:3000/profiles/confirmation?confirmation_token=#{#ctoken}")
or:
#b.goto("http://localhost:3000/profiles/confirmation?confirmation_token=#{Profile.last.confirmation_token}")
I become: undefined method `confirmation_token' for nil:NilClass (NoMethodError)
Looks to me that Profile.last returns nil in your case, hence the error message undefined method confirmation_token for nil:NilClass (NoMethodError). Please share code where you define Profile.

Custom error message for input validators (using the array syntax)

ZF 1.11.2
I've tried most of the syntaxes. They didn't click.
$validators = array('product_name' => array('alnum'));
//...
$input = new Zend_Filter_Input($filters, $validators, $_POST);
How in the world do you set a custom error message for alnum with the syntax above? Using 'messages' => array('Not alnum!!')? Yeah, well... How? I must've tried 100 nested arrays.
Use the built in translator.
For example, configure the translator in your config file to use a simple array
; Translations
resources.translate.data = APPLICATION_PATH "/lang"
resources.translate.adapter = "Array"
resources.translate.options.scan = "directory"
resources.translate.options.disableNotices = "1"
This tells the Translate application resource plugin that you want to
keep your translations under APPLICATION_PATH/lang
use the Array adapter (simplest)
scan the translation directory for languages / locales
ignore errors about unknown translations (ie user preferes en_AU but you don't have a specific translation file for that language)
Now, create folders for any languages you want to support. At a minimum, you'll want application/lang/en. For example
application
lang
en
en_AU
en_US
In each language folder, create a translate.php file. This file will contain (and return) an array of key / value pairs for each translation. You can find the keys for each validator message in the validator class. Here's an example for the Alnum validator
<?php
// application/lang/en/translate.php
return array(
Zend_Validate_Alnum::NOT_ALNUM => 'Not alnum!!',
Zend_Validate_Alnum::INVALID => 'Not valid!!'
);
For all Zend validators, you can also use the %value% placeholder in your message, eg
Zend_Validate_Alnum::NOT_ALNUM => "'%value%' is not alpha-numeric"
If you are simply trying to change the validation messages for a form element, I have always done it like this (inside a class that extends Zend_Form):
$this->addElement('text', 'myTextField', array(
'label' => 'The Label',
'description' => 'The description for the field...',
'filters' => array(
'StringTrim',
// etc
),
'validators' => array(
array('NotEmpty', true, array(
'messages' => 'This field is required',
)),
array('AnotherValidator', true, array(
'messages' => 'Bad value',
)),
// etc
),
));
Are you saying that this didn't work? Or are you using your validator in a more general context, in which case #Phil Brown's (awesome!) answer will do the job.
Disabling the translator on the element will disable the translation of all the validator messages. It is not possible to use a translator on the form or element and overwrite just one validator message. When the element is validated the translator is injected to every validator. The validator will use the translator if it is set. Thereby the custom error message won't be used.
Zend_Validate_Abstract::_createMessage()
// $message is your custom error message
$message = $this->_messageTemplates[$messageKey];
if (null !== ($translator = $this->getTranslator())) {
// your custom error message gets overwritten because the messageKey can be translated
if ($translator->isTranslated($messageKey)) {
$message = $translator->translate($messageKey);
} else {
$message = $translator->translate($message);
}
}
I think it is only possible to use a custom error message by disable the translator on the element.
$element->setDisableTranslator(true)
Use setMessage and disable translator if you have one.
$alnum = new Zend_Validate_Alnum();
$alnum->setDisableTranslator(true);
$alnum->setMessage(
'Not alnum!!',
Zend_Validate_Alnum::NOT_ALNUM
);
$validators = array('product_name' => array($alnum));
If you use your validator on a form element, you have to disable the translator on the element.