Codeigniter sending out multiple emails - codeigniter

I am using codeigniters email class inside an Expression Engine plugin (EE runs off CI).
For some reason each time the plugin is run it sends out 2 emails, instead of 1. The emails are identical.
{exp:cdwd_emailer:questionnaire type="{segment_3}" entry_id="{segment_4}"}
Here is the function being called by the above.
public function questionnaire() {
$type = $this->EE->TMPL->fetch_param('type');
$typeLower = str_replace("-", " ", $type);
$typeUpper = ucwords($typeLower);
print_r($type);
$entry_id = $this->EE->TMPL->fetch_param('entry_id');
$subject = $typeUpper.' Questionnaire Submission';
$fromEmail = 'email#email.com';
$fromName = 'Test Name';
$toEmail = 'email#email.com';
$message = '
<p>A new '.$typeLower.' has been submitted.</p>
<p>Please click here to view this submission</p>
';
$this->EE->load->library('email');
$this->EE->email->set_mailtype("html");
$this->EE->email->from($fromEmail, $fromName);
$this->EE->email->to($toEmail);
$this->EE->email->subject($subject);
$this->EE->email->message($message);
$this->EE->email->send();
}
Can anyone tell me why? I can't figure it out. I printed out the contents of the type and entry_id params to check only 1 of each are being collected.
Thanks

I think you have to clear you email object after sending mail.
As per Expression Engine, you have to call:
ee()->email->clear();
For your case:
$this->EE->email->clear();

Related

Code Issue when clicks "Return to Merchant" Button of Paypal

I am using Codeigniter to integrate the PayPal payment gateway on my website.
//This is my buy method
public function buy($id){
$course = $this->course_model->fetch_course_details_for_buy($id);
$returnURL = base_url().'paypal/success'; //payment success url
$cancelURL = base_url().'paypal/cancel'; //payment cancel url
$notifyURL = base_url().'paypal/ipn'; //ipn url
$userID = $this->session->userdata('student_id'); //current user id
$_SESSION['course_id'] = $course->course_id;
$logo = base_url().'assets/images/test-logo.png';
$this->paypal_lib->add_field('return', $returnURL);
$this->paypal_lib->add_field('cancel_return', $cancelURL);
$this->paypal_lib->add_field('notify_url', $notifyURL);
$this->paypal_lib->add_field('item_name', $course->title);
$this->paypal_lib->add_field('custom', $userID);
$this->paypal_lib->add_field('item_number', $course->course_id);
$this->paypal_lib->add_field('amount', $course->amount);
$this->paypal_lib->image($logo);
$this->paypal_lib->paypal_auto_form();
}
This is my PayPal's class method of success
function success(){
//get the transaction data
$paypalInfo = $this->input->get();
$data['item_number'] = $paypalInfo['item_number'];
$data['txn_id'] = $paypalInfo["tx"];
$data['payment_amt'] = $paypalInfo["amt"];
$data['currency_code'] = $paypalInfo["cc"];
$data['status'] = $paypalInfo["st"];
//pass the transaction data to view
$this->load->view('paypal/success', $data);
}
This is my IPN method
function ipn(){
//paypal return transaction details array
$paypalInfo = $this->input->post();
$data['user_id'] = $paypalInfo['custom'];
$data['product_id'] = $paypalInfo["item_number"];
$data['txn_id'] = $paypalInfo["txn_id"];
$data['payment_gross'] = $paypalInfo["mc_gross"];
$data['currency_code'] = $paypalInfo["mc_currency"];
$data['payer_email'] = $paypalInfo["payer_email"];
$data['payment_status'] = $paypalInfo["payment_status"];
$paypalURL = $this->paypal_lib->paypal_url;
$result = $this->paypal_lib->curlPost($paypalURL,$paypalInfo);
//check whether the payment is verified
if(preg_match("/VERIFIED/i",$result)){
//insert the transaction data into the database
$this->product->insertTransaction($data);
}
}
My payment is successful done from the sandbox account. But When I click the return to merchant button after payment it's throwing error of undefined indexes as mention in the success method. I apply first a time payment gateway and don't know what is missing in my code. Any help is appreciable.
You should log what $paypalInfo contains.
If you have enabled PDT, information may be coming as a POST rather than a GET.
In any case, make your code be able to check for and handle the missing data/indices or put it in a try/catch equivalent; it should not error.

$_SESSION variables use in queries

I have spent nearly two days going in circles on this one.
I seem to have difficulty using $_SESSION or $_POST as strings in any query or converting them to strings to use.
I am using a simple hash approach to login to a site.
Extract from script is
<?php
session_start();
echo "******Running Authenticate<br>";
echo "data submitted<br>".$_POST['site_login']."<br>".$_POST['site_password']."<br><br>";
$SiteLogin = $_POST['site_login']
$_SESSION['site_login'] = $_POST['site_login'];
$_SESSION['site_password'] = $_POST['site_password'];
$_SESSION['session_id'] = session_id();
$_SESSION['Now_val'] = date('Y-m-d H:i:s');
//include 'showallvars.php';
include 'dbconfig.php';
// Prepare our SQL
if ($stmt = $con->prepare('SELECT site_index, site_password FROM web_sites WHERE site_login = ?')) {
// Bind parameters (s = string, i = int, b = blob, etc), hash the password using the PHP password_hash function.
$stmt->bind_param('s', $_POST['site_login']);
$stmt->execute();
$stmt->store_result();
// Store the result so we can check if the account exists in the database.
if ($stmt->num_rows > 0) {
$stmt->bind_result($id, $password);
$stmt->fetch();
echo "account exists";
}
else
{
header('Location: badindex.php');
}
if (password_verify($_POST['site_password'], $password)) {
// Verification success! User has loggedin!
echo "password good";
}
else
{
header('Location: badindex.php');
}
}
$_SESSION['loggedin'] = TRUE;
?>
that works fine
BUT there is another field ( 'site_name') in the record which i want to carry forward.
This should be easy !!
and there is a dozen ways of doing it
for example the "standard" example is something like
$name = $mysqli->query("SELECT site_name FROM web_sites WHERE site_login = 'fred'")->fetch_object()->site_name;
That works fine
but no matter how i try - concatenating or or ... I cannot get $_SESSION['site_login'] or $_POST['site_login'] to replace 'fred'.
There seems to be white space added in.
Assistance or guidance ?
It should be possible to as easy as doing the following:
So:
if ($stmt = $con->prepare('SELECT site_index, site_password
FROM web_sites WHERE site_login = ?')) {
becomes:
if ($stmt = $con->prepare('SELECT site_index, site_password, site_login
FROM web_sites WHERE site_login = ' . $SiteLogin)) {
Do note, it is bad practice to do directly parse $SiteLogin to a query, because now someone can SQL Inject this and hack your website. All they need to do is use your form and figure out that which field is responsible for $SiteLogin. You would need to escape your $SiteLogin. Assuming Mysqli, it would become:
if ($stmt = $con->prepare('SELECT site_index, site_password, site_login
FROM web_sites WHERE site_login = ' . $con->real_escape_string($SiteLogin))) {
Thank you for that BUT the instant I saw the curly brackets in your answer - it all came flooding back to me. I had forgotten that PHP has problems with the square brackets
$sql = ("SELECT site_name FROM web_sites WHERE site_login = '". $_SESSION{'site_login'} ."' LIMIT 1");
I KNEW it was easy !
Your comments on injection are of course correct but this was an edited code excerpt and $SiteLogin was just added in as a "temporary working variable if needed"

Ajax, PHP. How can i send multiple variable with _GET or anyother way to other file?

Im trying to send two variables with get to a php file that contains an xlm format
function process(){
if (xmlHttp){
try{
email = encodeURIComponent(document.getElementById("email").value);
if (email=="") {email="empty email"};
xmlHttp.open("GET", "user_check.php?email=" + email, true);
username = encodeURIComponent(document.getElementById("username").value);
if (username=="") {username="empty username"};
xmlHttp.open("GET", "user_check.php?username=" + username, true);
the php file looks like
echo '<response>';
echo '<username>';
$username = ( isset($_GET['username']) ? $_GET['username'] : "something empty");
echo $username;
echo '</username>';
echo '<email>';
$email = ( isset($_GET['email']) ? $_GET['email'] : "something empty");
echo $email;
echo '</email>';
echo '</response>';
when i echo those variables it shows only one, actually it sends only the username which is the second on list(from top to bottom) that is sent. How can i send both or later more variables
function handleResponse(){
message_email = xmlHttp.responseXML.documentElement.getElementsByTagName("email").item(0).firstChild.data;
next_email = document.getElementById("next_email");
next_email.innerHTML =message_email;
message_username = xmlHttp.responseXML.documentElement.getElementsByTagName("username").item(0).firstChild.data;
next_username = document.getElementById("next_username");
next_username.innerHTML =message_username;
}
the output of this is
email : something empty
username : "the value from input"
i want email to have its value from its input.
I hope i made my question clear.
As far as I can understand you, you have to put both of variables into one request, because now, you are sending two requests, after everything reading only second one.
To send two variables in one GET request you use something like:
http://example.com/response.php?var1=foo&var2=bar
resulting in PHP:
$_GET['var1'] = "foo";
$_GET['var2'] = "bar";
So your AJAX code should look like:
email = encodeURIComponent(document.getElementById("email").value);
if (email=="") {
email="empty email"
};
username = encodeURIComponent(document.getElementById("username").value);
if (username=="") {
username="empty username"
};
xmlHttp.open("GET", "user_check.php?email=" + email + "&username=" + username, true);
Important thing is that order of GET variables doesn't matter - you can put email or username first, they will both be loaded into $_GET[] array.

How to set Component parameters in J2.5?

I've created a J2.5 component with some config fields using config.xml in the admin folder of the component.
How can I set parameters in the config programatically?
I've tried the code bellow, but it obviously doesn't save the result to the DB:
$params = & JComponentHelper::getParams('com_mycomponent');
$params->set('myvar', $the_value);
Could anyone please show some examples of how to achieve this?
The safest way to do this would be to include com_config/models/component.php and use it to validate and save the params. However, if you can somehow validate the data params yourself I would stick with the following (much more simple solution):
// Get the params and set the new values
$params = JComponentHelper::getParams('com_mycomponent');
$params->set('myvar', $the_value);
// Get a new database query instance
$db = JFactory::getDBO();
$query = $db->getQuery(true);
// Build the query
$query->update('#__extensions AS a');
$query->set('a.params = ' . $db->quote((string)$params));
$query->where('a.element = "com_mycomponent"');
// Execute the query
$db->setQuery($query);
$db->query();
Notice how I cast the params to a string (when building the query), it will convert the JRegistry object to a JSON formatted string.
If you get any caching problems, you might want to run the following after editing the params:
From a model:
$this->cleanCache('_system');
Or, else where:
$conf = JFactory::getConfig();
$options = array(
'defaultgroup' => '_system',
'cachebase' => $conf->get('cache_path', JPATH_SITE . '/cache')
);
$cache = JCache::getInstance('callback', $options);
$cache->clean();
The solution is here...
http://www.webtechriser.com/tutorials/82-joomla-3-0/86-how-to-save-component-parameters-to-database-programmatically
You can replace in Joomla 2.5+ the
// check for error
if (!$table->check()) {
$this->setError('lastcreatedate: check: ' . $table->getError());
return false;
}
if (!$table->store()) {
$this->setError('lastcreatedate: store: ' . $table->getError());
return false;
}
with
if (!$table->save()) {
$this->setError('Save Error: ' . $table->getError());
return false;
}

why DataType.EmailAddress converted link automatically?

I'm using a model with DataType.EmailAddress. I would like to modify the address link in the run time, however it already has email link automatically that prevents my modification.
#{
var subject = "";
if (Model.Name.Length > 30)
{
subject = Model.Name.Substring(0, 30) + "...";
}
else
{
subject = Model.Name;
}
}
model => model.email
But I got
<a href="mailto:emailaddress">emailaddress</a>
instead of
emailaddress
Why the email address is converted into link form automatically? And how to stop it? I would like to keep the datatype to use validation though.
You're trying to print the value of the property: #model.Email.
DisplayFor is not what you want.
Also, you need to URL-encode the subject parameter, including the space after Re:.

Resources