Could not instantiate mail function in joomla 2.5.4 - joomla

I am trying to send mail from my component when the user registers. I tried all three i.e PHPMail, Sendmail and SMTP but none works. I am testing it in localhost (WampServer 2.2) and the Joomla version is 2.5.4. I am not sure this is WampServer problem or Joomla or my own code. This is my code:
function postmail($name, $receiver){
jimport('joomla.mail.helper');
$mailer =& JFactory::getMailer();
$config =& JFactory::getConfig();
$sender = array($config->getValue( 'config.mailfrom'),$config->getValue( 'config.fromname' ) );
$mailer->setSender($sender);
$recipient = $receiver;
$mailer->addRecipient($recipient);
$body = "<body><br><p>";
$body .= "<strong>$name,</strong> ";
$body .= "</p><br></body>";
$mailer->isHTML(true);
$mailer->Encoding = 'base64';
$mailer->setBody($body);
$send =& $mailer->Send();
if ( $send != true ) {
//echo 'Error sending email: ' . $send->message;
return false;
} else {
//echo 'Mail sent';
return true;
}
}
It gets the error 'could not instantiate mail function'.

i am sure there are many reasons causing this problem. but to test you can always create a mail server of your own by installing this tool here http://www.toolheap.com/test-mail-server-tool/
you don't have to change anything except that you put localhost as the smtp host in joomla mail settings and then in your webserver's php.ini, add the smtp=localhost. thats it, it should work.
this is just to test whether your code is correct and you are using the correct mail settings in joomla. if this doesn't work then, i am sure the problem is not in your code but in your mail settings.

To understand what the problem is you really need to get the error message.
Change
//echo 'Error sending email: ' . $send->message;
to
echo 'Error sending email:'.$send->get('message');
then run it again. The error that you get should tell us why it isn't instantiating. Let us know what it says so we can help you out.

Related

Google recaptcha when posting to different server

I have a form with recaptcha V2:
https://www.fisherwallace.com/pages/do-you-qualify-to-use-the-device
It posts to a different server and then redirects back to a different page on the server with the recaptcha form.
Recaptcha site says: "We detected that your site is not verifying reCAPTCHA solutions." I assume it's due to posting to the different server.
NOTE: You'll see I have a clumsy workaround at the moment to address the fact that the recaptcha does not challenge automatically. Without the workaround, the recaptcha is there but nothing happens on submit.
I found some sample PHP code for the server side...
$email;$comment;$captcha;
if(isset($_POST['email'])){
$email=$_POST['email'];
}
if(isset($_POST['comment'])){
$comment=$_POST['comment'];
}
if(isset($_POST['g-recaptcha-response'])){
$captcha=$_POST['g-recaptcha-response'];
}
if(!$captcha){
echo '<h2>Please check the the captcha form.</h2>';
exit;
}
$secretKey = "Put your secret key here";
$ip = $_SERVER['REMOTE_ADDR'];
// post request to server
$url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($secretKey) . '&response=' . urlencode($captcha);
$response = file_get_contents($url);
$responseKeys = json_decode($response,true);
// should return JSON with success as true
if($responseKeys["success"]) {
echo '<h2>Thanks for posting comment</h2>';
} else {
echo '<h2>You are spammer ! Get the #$%K out</h2>';
}
Clearly lots of this code is moot since the user does not actually load the POST server page.
But will this part get the callback recaptcha needs?
$secretKey = "Put your secret key here";
$ip = $_SERVER['REMOTE_ADDR'];
// post request to server
$url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($secretKey) . '&response=' . urlencode($captcha);
$response = file_get_contents($url);
$responseKeys = json_decode($response,true);
// should return JSON with success as true
If the IP is a mismatch, can i hard code it using the IP from the originating server?
IP is optional
You need to do file_get_contents 'method' => 'POST' for siteverify

mail() on https

I setup a website that uses PHP mail(). I can get it to successfully run if I hit the script on http:// but if I switch to https:// it does not work! I use Godaddy for hosting and have a certificate purchased through them. I am not sure if I need to setup anything in the php.ini file or not. My mailscript looks like this:
$from_respondent_email = "calendar#mydomain.com" ;
$headers_respondent_email = "From: $from_respondent_email";
$subject_respondent_email = "Technical Support Request";
$body_respondent_email = "We received a support ticket from mydomain.com:\n\n";
$body_respondent_email .= "Test block of text";
$send = mail("myname#gmail.com", $subject_respondent_email, $body_respondent_email, $headers_respondent_email);
$to ="myname#gmail.com";
$subject = "Technical Support Request";
$message = "We received a support ticket from mydomain.com:\n\n";
$from = "calendar#mydomain.com";
$headers = "From: $from_respondent_email";
mail($to,$subject,$message,$headers);
Try this. It'll work. I used this in my website and its working :)

CodeIgniter set_message()

When I am trying to set the custom message for an error under from_validation library in CodeIgniter using the following statement..
$this->form_validation->set_message("is_unique", "%s already registered. Try a different %s");
Instead of getting Username already registered. Try a different Username I'm getting Username already registered. Try a different users.username
While the documentation says If you include %s in your error string, it will be replaced with the "human" name you used for your field when you set your rules.
Please get me out of the trap.
Looking at your problem it seems that the language file you are using does not have an index user.username. Define it in the user language file and see what happens.
Here is the Reference to language
Codeigniter Language Class
And
Codeigntier Language Helper
I faced same problem , while i am searching i found this answer which mentioned the line in Codeigniter that builds the error message (located in file system/libraries/form_validation.php) near line 673:
// Build the error message
$message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
I edited it to :
// Build the error message
if($rule == 'is_unique') { //my code here
$vars_num = substr_count($line, '%s'); //my code here
for($i = 0; $i < $vars_num; $i++){ //my code here
$params[] = $this->_translate_fieldname($row['label']); //my code here
} //my code here
$message = vsprintf($line, $params); //my code here
} else { //my code here
$message = sprintf($line, $this->_translate_fieldname($row['label']), $param);
}
and it works fine for me .
i know it is not best practice to edit system libraries but maybe it will help you.
Try this:
$this->form_validation->set_message("is_unique", "{field} already registered. Try a different {field}");
You can set Custom Message with Message library of codigniter
For Example
in Model Coding Like this
if(duplicate_user_name ==true){
$this->messages->add('$user already registered. Try a different $user ', 'error');
}
and Print this message with following View
$messages = $this->messages->get();
if (is_array($messages)):
foreach ($messages as $type => $msgs):
if (count($msgs > 0)):
foreach ($msgs as $message):
echo ('<div class="' . $type .'">' . $message . '</div>');
endforeach;
endif;
endforeach;
endif;

Codeigniter catch database errors

I'm looking for a way to catch all the database errors (if and when they occur) and sent an error report my email. For regular php error I extended the CI_Exceptions class and added my email sending code in addition to logging. But the database errors don't go trough CI_Exceptions but instead are logged directly from CI_DB_Driver->query() and I don't want to modify any file in the System folder.
Also I don't want to write logging code around every query the app does.
I'd be tempted to extend CI_Exceptions show_error method to catch any errors passed with the 'error_db' template rather than hunting through the db driver.
As you've already extended CI_Exceptions with email code this would seem like the best practice.
function show_error($heading, $message, $template = 'error_general', $status_code = 500)
{
set_status_header($status_code);
$message = '<p>'.implode('</p><p>', ( ! is_array($message)) ? array($message) : $message).'</p>';
if ($template == 'error_db')
{
// do email send here with $message
}
if (ob_get_level() > $this->ob_level + 1)
{
ob_end_flush();
}
ob_start();
include(APPPATH.'errors/'.$template.'.php');
$buffer = ob_get_contents();
ob_end_clean();
return $buffer;
}

CodeIgniter flashdata

I'm running into a problem I just can't seem to fix. I'm working on kind of a CMS kind of thing. And one of the functions is making image slideshows.
In one controller, I check if a slideshow with a certain ID exists, and if it exists, it should take the data and work with it, otherwise, set an error message (CodeIgniter flashdata) and redirect to the homepage.
The code I use for this is the following:
if($this->Mslideshow->slideshowExists($showid) === FALSE){
echo 'I\'m getting here';
$this->session->set_flashdata('error',$showid);
redirect('admin/index','refresh');
}else{
echo 'Slideshow exists';
}
And the code of the slideshowExists() function is:
public function slideshowExists($showid)
{
$this->db->where('id',$showid)
->limit(1);
$query = $this->db->get('slideshows');
if($query->num_rows() < 1){
return FALSE;
}
$this->currentquery = $query;
return TRUE;
}
And the problem is this, very strange actually. If the result I get back is FALSE, everything goes as planned. Error message gets set and redirect goes to 'admin/index'.
But if what I get back is TRUE, then the stange thing happens. I do get an echo with 'Slideshow exists', but I also get the error message.
I've tried everything, deleted cookies. Reset all sessions etc.
And even stranger is that when I tried to put the $showid I use to check into the error message, all of a sudden $showid is ' img '. While everywhere else is '1' or '2'...
Hope someone can help. Thanks!
=====EDIT=====
I tried to edit the code and simplify it. Right now I have this code in my Controller:
public function slideshow($showid){
$query = $this->db->where('id',$showid)->get('slideshows');
if($query->num_rows() < 1){
$this->session->set_flashdata('error','Slideshow doesn\'t exist.');
redirect('admin/index','refresh');
}
$data['page'] = 'slideshow';
$data['title'] = 'Slideshows';
$this->scripts->load_scripts(array());
$this->scripts->load_functions(array());
$this->load->view('admin/dashboard_template.php',$data);
}
When I run this with a $showid, that doesn't exist, I get the error message after being redirected to 'admin/index'. When I use a $showid that does exist, I do get the error, but no redirect but just the rest of the code.
I think you have to read your flash data in your view:
$error = $this->session->flashdata('error');
var_dump($error);
or in your Controller:
$error = $this->session->flashdata('error');
if(isset($error)) {
var_dump($error);
}
Also you can read this question: CodeIgniter "flashdata" doesn't work
you can get the flash data by following way
$error = $this->session->flashdata('error');
if($error)
{
echo $error;
}
You can try
if($this->session->flashdata('msg') != "")
This works for me.

Resources