Latest News: (loading..)

20 posts in this topic

@Harald Ponce de Leon @burt

Could there be some way in 2.4 where we can easily choose a method to send emails, without altering core code?

The easiest way I'm guessing would be to override the tep_mail function in includes/functions/general.php.

Then we can create and use mail/newsletter modules. Possibilities such as:

  • An app which extends osC's native mail sending, such as Gergely's HTML emails
  • Mailchimp
  • Mandrill
  • Sendinblue
  • Sendgrid
  • Constant Contact
  • etc etc

In my vague memory I recall a feature in 2.4 which will override core files, but I'm not sure.

Share this post


Link to post
Share on other sites

That would be ideal! 

How about as simple as a hook in the tep_mail call ?

Share this post


Link to post
Share on other sites

Can you explain how that would work?

Share this post


Link to post
Share on other sites

I'm not sure it would, just thinking out loud...

function tep_mail($to_name, $to_email_address, $email_subject, $email_text, $from_email_name, $from_email_address) {
    if (SEND_EMAILS != 'true') return false;
    
    $OSCOM_Hooks->call('mail', 'DoExternal');

    // Instantiate a new mail object
    $message = new email(array('X-Mailer: osCommerce'));

And then have the DoExternal hook perform the (eg) Mailchimp send...

and exit or return null or such.

 

Share this post


Link to post
Share on other sites

i tried it with an hook and  use the default mailer, but it fails via globals but hardcoded works.

includes/functions/general.php

  function tep_mail($to_name, $to_email_address, $email_subject, $email_text, $from_email_name, $from_email_address) {
   global $OSCOM_Hooks;
    if (SEND_EMAILS != 'true') return false;

    $OSCOM_Hooks->call('mail', 'mailAction');

  }

includes/hooks/shop/mail/mail_hook.php

    class hook_shop_mail_mail_hook {    
        function listen_mailAction() {
          global $to_name, $to_email_address, $email_subject, $email_text, $from_email_name, $from_email_address;    
          /*$to_name = 'Henry Withoot';
          $to_email_address = 'xxx@gmail.com';
          $email_subject = 'bestelling';
          $email_text = 'dit is een test bestelling';
          $from_email_name = 'Henry icecat';
          $from_email_address = 'info@xxx.nl';    */    
          // Instantiate a new mail object
          $message = new email(array('X-Mailer: osCommerce'));

          // Build the text version
          $text = strip_tags($email_text);
          if (EMAIL_USE_HTML == 'true') {
            $message->add_html($email_text, $text);
          } else {
            $message->add_text($text);
          }

          // Send message
          $message->build_message();
          $message->send($to_name, $to_email_address, $from_email_name, $from_email_address, $email_subject);
        }
    }

Hard coded works, but is not passing the global's from the tep_mail function.

Edited by wHiTeHaT
code refactoring

Share this post


Link to post
Share on other sites

I'm not sure if sending extra parameters to an hook is desirable?

$parameters = array('to_name' => $to_name,
                    'to_email_address' => $to_email_address,
                    'email_subject' => $email_subject,
                    'email_text' => $email_text,
                    'from_email_name' => $from_email_name,
                    'from_email_address' => $from_email_address);

$OSCOM_Hooks->call('mail', 'mailAction', $parameters);

Edited by wHiTeHaT

Share this post


Link to post
Share on other sites

Yeah, the hook system needs an update.  It's too basic as it stands.

Share this post


Link to post
Share on other sites

Well, not really i think.

The problem is the mixture of functions and classes.

application_top.php

somewhere.....(but below   $OSCOM_Hooks = new hooks('shop'); )

Quote

  $OSCOM_Hooks->register('mail');
 

... following works perfect. (checkout_process.php)

	$to_name = $order->customer['firstname'] . ' ' . $order->customer['lastname'];
	$to_email_address = $order->customer['email_address'];
	$email_subject = EMAIL_TEXT_SUBJECT;
	$email_text = $email_order;
	$from_email_name = STORE_OWNER;
	$from_email_address = STORE_OWNER_EMAIL_ADDRESS;  
	
	$OSCOM_Hooks->call('mail', 'mailAction');

	//tep_mail($order->customer['firstname'] . ' ' . $order->customer['lastname'], $order->customer['email_address'], EMAIL_TEXT_SUBJECT, $email_order, STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS);

 

Edited by wHiTeHaT
code refactoring

Share this post


Link to post
Share on other sites

I come to the conclusion that must be the same as how @Gergely already did it for the email templates system. So prob better to carry on on that one.

Share this post


Link to post
Share on other sites

Hi boys,

There are a main problem with v2.3. I had to fight a lot with globals when email templates figured out.
Osc v2.3 use sequential codebase which is not object friendly so only globals could keep values.

First option to extend mail class with another services but its change only the mail sender.
Second option in v2.4 to hijack the sequential code to another way with hooks. In this case all functions should build again by own app logic. We maybe see good examples in payment classes before and after process methods.

Hooks could have an option but i dont see the sort order of alternative hooks. If you know Wordpress it can handle the priority very well for example. There are a lot of built in router for this reason.
(WP calls apply_filters https://developer.wordpress.org/reference/functions/apply_filters/  but this is WP and not OSC)

After running jobs side by side means the hook but can not handle alternative ways. What will happen when two mail hooks copy in? Which will be the first or the second?
How could you handle the sequencies when the first hook close the door and run to an alternative way? (Paypal doing this so I had to do Paypal mail modules in html email tremplate)

Share this post


Link to post
Share on other sites

Sounds confusing. Does that mean the base code architecture needs to be changed?

Share this post


Link to post
Share on other sites

@frankl yes, that is because of missing classes and usage of standard functions in html_output.php and general.php.

Changing core code is unavoidable, either "just 1 line" or many. 

Share this post


Link to post
Share on other sites
17 hours ago, Gergely said:

What will happen when two mail hooks copy in?

Perhaps off-topic but for that i think general logic/common sense comes in place, last-in/first-out or cascade way.

So if you already use a module that handles prices on specific rules and later install another module that have the same rule but with a different outcome. The last module should get the overhand, unless there's a switch in place beforehand or build-in into one or both hooks what inform you about the situation.

Edited by wHiTeHaT

Share this post


Link to post
Share on other sites

@frankl Paypal use different globals and I dont know what are in another payment modules. Basic case when only oscommerce core running in checkout this could be work but an alternative way (Paypal) wont be used the hook.

This is not impossible but we should care about alternatives.

I miss tep_mail() in v2.4

Probably this could help:

class Mail extends AlternativeMail


and AlternativeMail class could handle email sender hooks and we could use hooks there. But I dont see how could you apply templates to email body contents.

@wHiTeHaT globals informs about situations. WP use hundreds of globals by the way.

Share this post


Link to post
Share on other sites

Some information is missing to use hooks.

Mail class should have saved informations about products or notes and we could use it for templates.

content name (order, create_account and so on...)
contents (products, notes, attachments, payment footer, shipping info ...)

Share this post


Link to post
Share on other sites

Theoretical you not need to use any globals, the only thing what is required is to fetch out the data from the orders table, that's it. 

Share this post


Link to post
Share on other sites

We are talking about emails, orders and I think template usages. Orders table could use it when $orders_id is known (PayPal use it) but checkout use $insert_id and admin use $oID. :wacko:
Hooks cant reach globals even if globals variables are set before in parent function.
 

function tep_mail() {
globals $oID, $order_id, $inser_id;

hooks();
 
}

 

 

Share this post


Link to post
Share on other sites

Keep it simple, extend the core hooks with extra parameters and you can do all you want, like i mentioned earlier.

$parameters = array('orderId' => $insert_id,
                    'etc' => $etc,                    
                    'to_name' => $to_name,
                    'to_email_address' => $to_email_address,
                    'email_subject' => $email_subject,
                    'email_text' => $email_text,
                    'from_email_name' => $from_email_name,
                    'from_email_address' => $from_email_address);

$OSCOM_Hooks->call('mail', 'mailAction', $parameters);

Try to do something that just makes the MOST sense.

Or extend the hooks with  new methods to harvest $parameters even before checkout_process itself.

Something like:

$OSCOM_Hooks->setParameters('mail', 'mailAction', $parameters);
$OSCOM_Hooks->setParameters('mail', false, $parameters); //use parameters widely in the complete hook.

//inside the $hook use a __constructor to fill $parameters
var $parameters = array();

function getParameters(){
    return $this->parameters;
}
//Within the function listen_mailAction() you usee:
$this->getParameters();

//case closed... move on to the next problem

The idea for harvesting $parameters before using the hook i deserve a big FAT credit for lol, because especially in this case for the email you could put inside the order email additional content like, (based on back-end tracked visited products for that specific user) order before date: XX-XX-2017 and get an extra discount on product X and/or productY.

Where product X and Y represent most tracked product the customer visited.

Edited by wHiTeHaT

Share this post


Link to post
Share on other sites

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