Skip to content

025 How to use email helper in your components

Amigo edited this page Jul 2, 2019 · 26 revisions

HOW TO USE EMAIL HELPER IN YOUR COMPONENTS

  • Example Of Email Helper Class

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, 00:00:48 and adds it to mail and then loads in the variables that's required.<<<<<<<<<<<<<<<<<<<<<<<

Setting Up Email Helper Class

If you look at a component, what you need to do first, if you are going to use this feature in any of your components. Let us open Learning manager. Then go to libraries and helpers. 00:01:22 You'll see that the add email helpers been set to on. It places the helper class in your component, that's all it does. It doesn't implemented anywhere else. At the moment that means that if you click yes here, you need to still create fields, that you are going to load your config area.

Settings - Config Area

Now if you don't know about the config yet, it means you've skipped some tutorials, but it's basically this(config) area here. 00:01:55 Which gives you the ability to add configuration options to your components option area. So if we click here(config - add) I scroll down you'll see that there is mail configuration and a whole lot of fields there. Now these fields correspond to the Joomla's default fields, if you didn't know. If you would like to create these fields and you need some info on what there should be in, what should be their names and everything. Then go look at the Joomla global configuration XML. And you'll see all the settings in that file. For our purposes I'm simply going to show you how the fields look like. 00:02:39 We also have the DKIM implementation available.

In Code Field Names

Even those fields you will need to look at the code to know what their names should be. So there's a difference of course between name and label and description. When we refer to name that is the value that will be used in the code. I'll show you what I mean. Go back to the code. For example that word mailer 00:03:13 means that the name of the field is called mailer. The name of the field is called that and that and so it goes on. So these are all the fields that needs to be set.

Component Global Options - Mail Configuration

In your component there is a options area, I'm going to open that. Once it's open your see there is a mail configuration and a DKIM area. 00:03:45 Let's open the mail. You will see that it has the mailer status. So it's either set to off, then no emails will go out, or to on.

Switch Global Mailer Options

If it set to on you can decide whether you want to override the global function, the global variables. The global variables are set in your Joomla's components global area. 00:04:10

Joomla Global Configuration - Server

So you would go to global configurations, and then you'd go to server, and then there's mail settings. Now these are the main or the the global settings that will be used in any component that does not add 00:04:40 these settings in their config. So if you didn't create those fields and added it to your componyou can see you passing the HTML on the subject to it. ent, but you ticked the button to add the helper class to your component, that means that it will fall back to these settings in your Joomla default area. And these(Main Settings) are the values that you need to override.

Component Builder Email Switch Options

If you want your 00:05:08 component in some way to use other php mail or other send mail or SMTP, values that of the global, you need to set it here. That is basically what this area can do. So you use global, it falls back to the Joomla global. Otherwise you can override it on this level. And send it out in a way that is 00:05:38 what you would like.

DKIM Settings (Encryption)

The DKIM area is the more advanced area which allows you to encrypt and secure email sent out from you. To authenticate that it was really you that send it. This is helpful to show people that receive these emails that it's not spam, and it's really comes from you. It's more advanced. I would encourage you to read up on it. These are the values usually would be required to added a private key, a public key and these type of areas here(see video). If this is set to no, it will not be used, if it set to yes, please ensure to add the values. 00:06:25 Otherwise it still will not be used. So that's basically the values that you need still to create. Component does not do this for you. You need to do it. I could possibly foresee that in the future we might add this, if you tick the helper class it adds these values for you. But even then if you have done it manually, we'll make sure that it doesn't add in twice. 00:06:51 As long as these values have the same names, we will identify that, and do not add them dynamically to avoid clashes. For the meantime it's not automatically being added and so you need to do that manually if you want it to be there. If you don't, like I've said before it will fall back to the Joomla global. 00:07:15

Default Global In Code

To explain to you on the code level, basically it gets from the configuration, the mailer function. Then it checks whether it's global. If it is global, then it implements Joomla's values. If it's not global, it implements your components values. 00:07:38 That is basically what it will do. Now you can read through the code, but the most important area really is this(send) area. To know what is all the various options that you have in the signature to send out mail. So it's able to really send out a lot of mail but nonetheless you're also need to consider 00:08:04 your servers own limitations. Since we don't want you to spam people. We do not override those values. We are simply using the Joomla default Helper jmailer. And it is been extended from another class. There are a lot of features in here. If you want to know more about them you can read up on the jmailer 00:08:35 Class from Joomla. As well as read through this code here and see how we implemented it.

DKIM Values In Code

These(see video) are the dkim values that you would need in your component to be able to use the dkim encryption. But you could also read up on this and check out this function here, up till there, in its way that it's implementing these features. It obviously is adding it to the mailer and most of the work is done in the mailer. 00:09:05 It's basically adding the data to the mailer and the mailer takes care of the rest. Here it sends it off.

Error Checking For Emails In Code

And if there is an error, let's say for some reason it didn't work out well. Then it checks whether your component helper class, your component has a helper class, it's this file here(learningmanagerhelper). 00:09:31 If it have a storemessage variable or method. So if we were to go check this. You see that there is the storemessage method in the helper class. This is a custom method that I wrote. You can write it anywhere you like, but this signature should be the same. 00:10:00 So basically this area that(learningmanager) and that(learningmanager) is dynamically updated to your component. It uses your component name here(learningmanager) and your component name there(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 it's says that it's emailed. So you can have different types, so if you look at the method, the type can be anything. 00:10:30 We want to do different things on different types. If it send an SMS and it didn't fail, you want to store the message. This kind of feature isn't only used when something didn't work, but also if it worked. If it was sent, it's going to store it. So it's a way to store the message,myou can 00:10:57 let your user go, if they login, they can see messages sent to them and this kind of stuff. This is what the store message does. You will see that it's whole huge custom method that I wrote. You can update it and change it, pause the video, copy it down. If you realize this is actually something you write up yourself you just need to make sure that that part is the same. The rest is up to you how you deal with it. That is implementing the learning manager emailer 00:11:35 in your component.

Implemented Example

Maybe I should show you where I implemented at least in one area. I have this component called job tracking system. I open it up, you'll see that it actually has that function: Add email helper. Set it to on. The view in which we are using the email method is the job order view. Basically we want to email the client, it did a job order 00:12:21 when it's created. So let me show you how I implemented it there. So here is the job order. I would basically then click on email. I can update it to whatever email I like, 00:12:45 testing@vdm.io or something. Just send it in to nothing. Then once the email has been sent, it let's me know. In the top right it says email was sent successfully. That's basically what the emailer does. It just this button that sends the document 00:13:11 to my client. If I want to send it to my store, the people that does the job, I could also do that here. I'm not going to explain to you the reality of taking this content, adding it to the email and sending it. That I would suspect you need to know and you need to learn or study to do that yourself. I'm simply explaining to you how to use the email helper class.

Above Example In Code

So we will go to the code and I'll show you how I do it and 00:13:45 I'll give you some pointers. I won't go into detail about how that is done. Let's go to the code. We are in the job order, first thing here is some JavaScript. There's the send email function and gets a set of values. And then it gives it over to the send email server function, which sends it as a 00:14:20 json request to the server. The send email is the task and when it gets a response that's when you get the notification. That's simply the JavaScript of it. If we go to php, in previous tutorials we explain how to use the Ajax class.

I'm going to touched on it briefly. 00:14:47 You have your input here(ajax input). This is where you set up your controller. So you'll see in your controller there is send email class what was defined in this field here(see video). And you see that there is three variables, and they 00:15:12 should be filtered in this way and they are pass over to these methods and you should be login user. So if you look at the code, again this is the task(sendEmail), down here it triggers the task. It checks for those values and then passes it over to this method(sendmail) which is in the model. We go to models, Ajax. Here is the the model(see video). Let's just scroll down to the sendemail. Here is the sendemail and this again 00:15:47 is a custom script. So if I was to go back here, I close this, you'd see there's the ajax method, there is a lot, I mean look at that, you can see that scroll down way down is a lot of other custom methods. This is another custom method I've wrote, it's called sendemail, it gets the mail, the HTML, the type. It it does the necessary cleansing and whatever. 00:16:14

Over here(see video) we're calling the email helper. We using the send method, we passing the valuables, and set the result in here. If the results is true, 00:16:38 we will let the user know, otherwise we give him the error. I'm also using this email body to help me build the email because I want to make sure that it got all the necessary HTML and stuff. You can pause the video and type that out if you like. This is the email body that I usually use. It simply adds the subject to the email, 00:17:15 the necessary places as well as the body in the necessary place that it gets send in a way that's more appropriate. That all is happening right here. That is what I'm passing in here. A custom method which gets the data from the ajax and then sends it off. 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. If you feel that our implementation lacks some professional help then please do make a commit to quest or send me an email. We'll gladly update an improved this class, although I think we followed all the necessary standard and requirements to make it useful to everyone.

Clone this wiki locally