Skip to content

025 How to use email helper in your components

Amigo edited this page Jul 16, 2019 · 26 revisions

HOW TO USE EMAIL HELPER IN YOUR COMPONENTS

  • Example Of Email Helper Class

00:00:00

The Email Helper Class is a class that gets added to Components Helper area and is therefore available on every page, with which Emails can be send. For example: Take a look at the Helper Class by going to a component that has it included and at 'Helpers'. 00:00:25 The filename is usually the component name: 'Email'. As may be seen, it is in the basic abstract class. That takes Joomla's E-mailer, which is 'Jmail' and gets an instance of it, and adds it to mail and then loads in the variables that's required. 00:00:48

Setting Up Email Helper Class

00:01:02

If this feature is going to be used, the first thing to do when looking at a component (On Component Builder Dashboard) is to open 'Learning Manager'(In Editing the Component area) and go to Libs & Helpers. 00:01:22 It may be noticed that the 'add email helpers' has been set to 'on'. It places the Helper class in the component, but is not implemented anywhere else. That means that if 'yes' is clicked, it is still necessary to create fields, that need to get loaded to the config area.

Settings - Config Area

00:01:47

In 'Settings' is this 'config' area (Adding Custom Config Fields), which gives the ability to add configuration options to the components option area. 00:01:55 So if this 'config - add' is being clicked, scroll down till that mail configuration and a whole lot of fields may be seen. These fields correspond to the Joomla's default fields. If you would like to create these fields and some info is needed in order to create these fields, like what should their names and content be, then go and look at the Joomla Global Configuration XML and all the settings in that file may be seen. 00:02:39 There is DKIM implementation available.

In Code Field Names

00:02:45

It will be necessary to look in the code to get these field's names. There is a difference between 'name', 'label' and 'description'. 00:03:13 For instance 'name' is the value that will be used in the code. In the code for example, that word 'mailer' means that the name of the field is called 'Mailer' and the same applies to the name 'field' etc. These are all the fields that needs to be set.

Component Global Options - Mail Configuration

00:03:35

In the component there is a 'Options' area, there is a Mail Configuration and a DKIM area. 00:03:45 It has the 'mailer' status. It is either set to 'Off', then no emails will go out, or to 'On'.

Switch Global Mailer Options

00:03:55

If it is set to 'On' it may be decided whether there is need to override the global variables. The global variables are set in Joomla's components global area.

Joomla Global Configuration - Server

00:04:10

If Global Configurations is opened, and then the Server, and then Mail Settings. These are the main or the Global Settings that will be used in any component that does not add these settings in their config. 00:04:40

If those fields had not been created and added to the component, but the button was used in order to add the Helper class to the component, the result will be that it will fall back to these settings in the Joomla default area. These 'Main Settings' are the values that should be overrided.

Component Builder Email Switch Options

00:05:08

If in some way the component should use other PHP, Send mail or SMTP values than the Global it must be set on the Learning Management System Configuration page in the Mail Configuration area.(See video) That is the function of this area . If Global is used, it will fall back to the Joomla Global. Otherwise it can be overrided on this level and send out as you prefer. )

DKIM Settings (Encryption)

00:05:40

The DKIM area is the more advanced area which allows encryption and to authenticate emails that has been send. This is helpful to show people that receive these emails that it not spam. Because of the advanced nature of this some research on this will be helpful. These are the values that usually would be required: Private key, Public key etc.(see video). If this 'Enable DKIM' is set to 'No', it will not be used. Please ensure to add the values when it is set to 'Yes', otherwise it still will not be used. 00:06:25 So that values need to be created as Component Builder will not do it.

Default Global In Code

00:07:15

On the code level(See video), it gets the 'mailer' function from the configuration and checks whether it is Global. If it is, then it implements Joomla's values. If it's not global, it implements your components values. 00:07:38 Read through the code, the most important area is this 'send' area, there the various options in the signature to send out mail may be observed. 00:08:04 It is able to send large quantities of mail but nonetheless your servers limitations should be considered. To avoid spams to be send, this values has not been overrided. The Joomla default Helper 'jmailer' is simply been used which has been extended from another class.(See video) 00:08:35. More information about the 'jmailer' Class may be found at Joomla.

DKIM Values In Code

00:08:40

These(see video) are the DKIM values that would be needed in the component to be able to use the DKIM encryption. Do some research on this and check how this function implements these features. It gets added to the '$mailer' and most of the work is done in the '$mailer'. 00:09:05 It adds the data to the mailer and the mailer takes care of the rest and it is send out.

Error Checking For Emails In Code

00:09:12

If for instance an error occurred, then it verifies whether the component Helper Class, (this file 'learningmanagerhelper'). has a 'storemessage' variable or method. 00:09:31 It can be seen that there is the 'storemessage' method in the Helper Class. I had written this custom method. It may be written anywhere, but the signature should be the same. 00:10:00 These(See video) 'learningmanager' areas is dynamically updated to your component. It uses the component name 'learningmanager' and the component name 'learningmanager'. But this 'storemessage' cannot be changed, but it takes the 'send email', the 'recipient', the 'subject', the 'body', the 'text only', the 'mode' and indicates that it is 'email'. So there may be different types, and looking at the method, the type can be anything. 00:10:30 Different things need to be done on the various types. If the SMS didn't fail, the message needs to be stored. This kind of feature isn't only used when something didn't work, but also when it worked. If it had been send, it will be stored. 00:10:57 So it's a way to store the message, so that the users, if they login, can see messages sent to them. That is the function of the Store Message. This Custom Method can be updated and changed, (pause the video), copy it. If you had written it just make sure that that part 'storemessage' is the same. 00:11:35 That is implementing the Learning Manager Emailer in your component.

Implemented Example

00:11:39

Here follows a example of how it is implemented at least in one area. For instance here is this component, Job Tracking System. It has that function: Add Email Helper. Set it to 'On'.The view(Admin View) in which the email method is used, is the 'Job Order' view. In this case the client needs to be emailed when a job order had been created. 00:12:21

This is how it is implemented.(See video) Here is the Job Order. When Email is selected it may be updated to any preferred email. like for example: 'testing@vdm.io'. 00:12:45 Once the email has been sent, it will be confirmed in the top right. 00:13:11It is this button that sends the document to the client.

Above Example In Code

00:13:40

Go to the code.(Editting the Admin View) In the 'Job Order' is a JavaScript area. The Send Email function may be seen which collects a set of values and transfers it to the 'sendEmail server' function, which sends it as a 'json' request to the server. 00:14:20 The 'sendEmail' is the 'task' and when it gets a response, it gives the notification.

In the 'Job Order' is a PHP area. How to use the Ajax class had been explained in previous tutorials but just a brief touch on it again. 00:14:47 Here is the input(ajax input) where the controller has to be set up . In the controller there is 'sendEmail' class that was defined in this field (see video). 00:15:12 There are three variables, and should be filtered and are transfered to these methods and then should be logged in 'User' by selecting 'Yes'. Looking at the code, again this is the task 'sendEmail', it triggers the task and checks for those values, then transfers it to this method 'sendMail' which is in the model. Go to Models and to Ajax. Scroll down to the 'sendEmail'. 00:15:47 This is a Custom Script. In closing this, the Ajax method can be seen. This is another Custom method which I had written., it's called sendEmail, it gets the 'mail', the 'HTML' and the 'type'. It it does the necessary cleansing etc. 00:16:14 The Email Helper is requested. Then the 'Send' method is used and the variables are transfered. Then set the 'result'. If the result is 'true', the user is notified, otherwise he receives the 'error'. 00:16:38

This email body is of great help to build the email and it ensures that it contains the necessary HTML etc.00:17:15 The HTML and subject can be passed to it and it simply adds the subject to the email and the necessary places as well as the body($body[]_$html) in the necessary place, and gets send in a appropriate way. A custom method is passed which gets the data from the Ajax and then sends it. 00:17:43 That's a quick tutorial on how to implement sending emails through your component, using the Email Helper Class. The Email Helper Class is available on GitHub in the Component Builder.

Clone this wiki locally