Jump to content

Use of Send::Mail on external php module file


jonsecu

Recommended Posts

Hi... I am developing a module that uses a payment gateway that delivers notifications to a file located inside the module. Inside the module I have a file called handleResponse.php. This file is open to the internet and listens for notifications. When some conditions are met, I want to send a mail message using the method Mail::Send, and I cant do this. I've been debugging and found that Mail::Send was not included on the file, so It cannot be executed. I read a bit and learned that I must include prestashop config files but still is not working.

 

This is my structure:

 

File handleResponse.php available at

http://site.com/modules/modulename/handleResponse.php

is this one:

<?php
include(dirname(__FILE__).'/../../config/config.inc.php');
include(dirname(__FILE__).'/../../classes/Mail.php');

include(dirname(__FILE__).'/tools.php');

echo ("ok");
sendMails($arg1,$arg2,$arg3...);  
?>

I read that including config.inc.php would allow me to use prestashop functions but it is not working. I tried including the Mail.php class but still is not working.

 

When I direct the browser to the url it displays ok, did this just to check everything was ok. Then sendMails() is a function on a file called tools.php which contains this:

<?php
function sendMails(
    $langID, $templateName, $subject, $data, 
    $inputEmail, $inputName, $from, $fromName, 
    $var1, $var2, $templatesDir, $var3, $var4,
    $copyAdmin = true
    ) {
    saveToLog('people.log','sendMails|'.$inputEmail."|".$subject);
    
    if (Mail::Send(
      $langID, $templateName, $subject, $data, 
      $inputEmail, $inputName, $from, $fromName, 
      $var1, $var2, $templatesDir, $var3, $var4
      )) {
      saveToLog('people.log','sendMails|'.'user mail sent');
    }
    else {
      saveToLog('people.log','sendMails|'.'error user');  
    }
?>

I know this function works well because when called from inside the module it sends the emails properly, but when called from the web it doesnt work. I took good care of the params required by Mail::Send.

 

My question is... how can I use the Mail::Send method from a file inside the module that is called from the internet at the address shown above?

 

Thank you very much... any help is appreciated.

 

 

Link to comment
Share on other sites

Hi, you dont need include Tools or Mail classes. But init.php

include(dirname(__FILE__).'/../../config/config.inc.php');
include(dirname(__FILE__).'/../../init.php');

Hope this help you.

 

Best regards

 

Hi... thanks for answering. I tried with init.php, exactly as you state. However even when the mail is being sent now when I do, my payment gateway stops working for reason.

 

Is there a way to just include the Mail class?

 

Any other idea please?

Link to comment
Share on other sites

So I did a simple test assuming the following

 

There is a module named test and this module has 2 files

/modules/test/test.php

/modules/test/tools.php

 

So I created those 2 files in the /modules/test/ directory with the content below.  I get the following expected output

before sendMails
sendMails executed
after sendMails

I ran this test in PS 1.6, 1.5 and 1.4 and it all worked properly. 

 

 

 

test.php

<?php

include(dirname(__FILE__).'/../../config/config.inc.php');

//this contains your sendMails function
include(dirname(__FILE__).'/tools.php');

echo ("before sendMails<br>");

sendMails();

echo ("after sendMails<br>");

?>

tools.php

<?php
function sendMails()
{
    echo "sendMails executed<br>";
    return;
}

?>
Link to comment
Share on other sites

 

So I did a simple test assuming the following

 

There is a module named test and this module has 2 files

/modules/test/test.php

/modules/test/tools.php

 

So I created those 2 files in the /modules/test/ directory with the content below.  I get the following expected output

before sendMails
sendMails executed
after sendMails

I ran this test in PS 1.6, 1.5 and 1.4 and it all worked properly. 

 

 

 

test.php

<?php

include(dirname(__FILE__).'/../../config/config.inc.php');

//this contains your sendMails function
include(dirname(__FILE__).'/tools.php');

echo ("before sendMails<br>");

sendMails();

echo ("after sendMails<br>");

?>

tools.php

<?php
function sendMails()
{
    echo "sendMails executed<br>";
    return;
}

?>

 

Hi Bellini thanks for answering, the structure works as you state. Files are called properly. The issue comes when I want to send mails using Send::Mail method.

 

The external file (in your example test.php) is accesed through the internet by an API that posts some data. Then test.php retrieves that data and calls tools.php, passing an int as a parameter. Tools then does some processing (retrieving info from database such as the mail, message... ) and calls Mail::Send method via sendMail(). Here a mail is supposed to be sent. The issue is:

 

- If I write the test.php url address on my browser (including the files below on test.php) the mail is sent through the whole process.

include(dirname(__FILE__).'/../../config/config.inc.php');
include(dirname(__FILE__).'/../../init.php');
include(dirname(__FILE__).'/tools.php');

- However, if I test the API (Called MecadoPago IPN 2.0), an error is thrown saying "Error 503: Service Unavailable". If I remove the init.php line, the API sends an "Ok Status", however the mails are not sent. 

 

For some reason calling init.php causes the payment gateway to stop working, however I need it to send the mails. Is there some workaround to use Mail::Send without calling init.php?

 

Thanks again!

Link to comment
Share on other sites

you don't need to use init.php, if you look at the file you will see it is deprecated in v1.5

 

do you have maintenance mode turned on by any chance?

 

Indeed, I was developing on a mirror site of my store in maintenance mode with my ip added on the back office. Now both the gateway notification system and Mail::Send work perfectly.

you don't need to use init.php, if you look at the file you will see it is deprecated in v1.5

 

do you have maintenance mode turned on by any chance?

 

Thank you very much bellini!

Link to comment
Share on other sites

  • 1 year later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...