I am finding it difficult to work out how to incorporate PHPMailer into my trongate app?
If anyone had managed to accomplish this I would appreciate some pointers.
Anyone managed to use PHPMailer with Trongate
3 years ago
3 years ago
#1
3 years ago
#2
It's okay I have managed to resolve this problem by making a few tweaks to the latest release of PHPMailer so that it can be included in and called by reference to the module that I have placed it in.
This definitely shows the benefit of the modular construction within Trongate.
I removed the references to name spaces and altered the way the classes were included. This now allows the PHPMailer class to be used and called from anywhere in my Trongate application which is a huge relief and means I can move on to the next thing on my list!
This issue can now be closed.
This definitely shows the benefit of the modular construction within Trongate.
I removed the references to name spaces and altered the way the classes were included. This now allows the PHPMailer class to be used and called from anywhere in my Trongate application which is a huge relief and means I can move on to the next thing on my list!
This issue can now be closed.
3 years ago
#3
Hello Tony,
I did this early on. Check it out in the Module Market.
It is not up to date with current php mailer.
Search for Mailman on the Module Market.
Dan
I did this early on. Check it out in the Module Market.
It is not up to date with current php mailer.
Search for Mailman on the Module Market.
Dan
3 years ago
#4
I was waiting for Dan to chime in :) and knew about that great module of his.
Not sure if you knew Tony, but you can also use Composer to pull in the PHPmailer library and then use vendor autoload to use it. Let me know if you would like instructions on doing that.
Not sure if you knew Tony, but you can also use Composer to pull in the PHPmailer library and then use vendor autoload to use it. Let me know if you would like instructions on doing that.
3 years ago
#5
Hi Simon,
Thanks for offering to provide instructions I think that would be really useful information to have although I got the impression that as Trongate developers we were shying away from composer and autoload?
However I am sure there are other newbies to Trongate who would also benefit from having that process explained so yes please go ahead and give me the low down on that process.
I did manage to get the phpMailer library working by removing the namespace references and "requiring" the classes and housed the whole lot in its own module with a controllers and assets folder.
This does allow it to be called and used.
I am using my "basemodule" module to house functions that get called to send emails, create pdf file and also check on a user credentials, a bit like general helper functions library as used in the CodeIgniter 4 setup. Seems to work quite well and gives me a module that can be transferred between applications easily.
Thanks for offering to provide instructions I think that would be really useful information to have although I got the impression that as Trongate developers we were shying away from composer and autoload?
However I am sure there are other newbies to Trongate who would also benefit from having that process explained so yes please go ahead and give me the low down on that process.
I did manage to get the phpMailer library working by removing the namespace references and "requiring" the classes and housed the whole lot in its own module with a controllers and assets folder.
This does allow it to be called and used.
I am using my "basemodule" module to house functions that get called to send emails, create pdf file and also check on a user credentials, a bit like general helper functions library as used in the CodeIgniter 4 setup. Seems to work quite well and gives me a module that can be transferred between applications easily.
3 years ago
#6
Hi Tony,
Outside of the core Trongate framework, third-party packages are fine; as long as you know and accept the security implications of just grabbing any old library and plugging it into your app. DC was correct in removing all third-party libraries from Trongate. It not only eliminates other people's rewrite schedules, but it also gives YOU full control of the code in your app. If you look at libraries on 'Packagist', you will see many anonymous authors contributing with lots of dependencies that are open to vulnerabilities. Do a search on Google News for 'Packagist' and be VERY mindful of what you include in your app.
I like your idea of having a 'basemodule' you can call upon to include in other Trongate apps - I too have a general-purpose module that I use to put common methods I like to bring with me to other projects.
Getting back to the subject of how to use 'Composer' to load in a library like 'PHPMailer' from 'Packagist'; First, ensure you have 'Composer' installed on your computer and let's assume you have already created a new Trongate app and have installed the 'Simple Members' module from the 'Module Market' - (for this example, my app is called 'A5').
Open a terminal or command prompt and 'CD' to the root of your app (I'm using Windows with VS Code and PHP v8.1.17, so all I do is open the app in VSC and hit Ctrl+Shift+` to open a terminal).
Enter:
At the time of writing, we have now downloaded 'v6.8.0 of PHPMailer' into the 'vendor' folder of the root of your app from 'Packagist'. You should see the following folder structure ('tree vendor /f'):
There were also 2 files created in the root of your app:
Both of which contain the version we are using and the dependencies required to use PHPMailer. Also, by looking at the instruction on 'Packagist' you can swap to a different version of 'PHPMailer' and there are detailed instructions on how to use it.
For our use case with Trongate, all you need to do is create a new module or integrate the code into 'Members.php' - for this demo, I'll try to stay in line with what you did and use a general-purpose module like your 'basemodule', but I would have used camel case - 'base_module' for it's name.
On a side note - I have also loaded libraries inside of modules to keep them truly modular. For example, here is one I used to create invoices using a PDF library - where all vendor files reside in the assets folder:
Now we will follow DC's instructions inside the Members module 'README.txt'
The following 2 lines are added to the above methods in 'Members.php' to send the email:
And the controller in 'basemodule' would look like this - I'm using Gmail in this example for the email server - replace with yours:
Note: I am not using the SMTP class and I have used the global variables from 'site_owner.php' - so please replace them with yours.
I hope this helps in using PHPMailer via Composer and Autoloading it.
Please let me know if you have any questions.
Cheers,
Si
Outside of the core Trongate framework, third-party packages are fine; as long as you know and accept the security implications of just grabbing any old library and plugging it into your app. DC was correct in removing all third-party libraries from Trongate. It not only eliminates other people's rewrite schedules, but it also gives YOU full control of the code in your app. If you look at libraries on 'Packagist', you will see many anonymous authors contributing with lots of dependencies that are open to vulnerabilities. Do a search on Google News for 'Packagist' and be VERY mindful of what you include in your app.
I like your idea of having a 'basemodule' you can call upon to include in other Trongate apps - I too have a general-purpose module that I use to put common methods I like to bring with me to other projects.
Getting back to the subject of how to use 'Composer' to load in a library like 'PHPMailer' from 'Packagist'; First, ensure you have 'Composer' installed on your computer and let's assume you have already created a new Trongate app and have installed the 'Simple Members' module from the 'Module Market' - (for this example, my app is called 'A5').
Open a terminal or command prompt and 'CD' to the root of your app (I'm using Windows with VS Code and PHP v8.1.17, so all I do is open the app in VSC and hit Ctrl+Shift+` to open a terminal).
Enter:
At the time of writing, we have now downloaded 'v6.8.0 of PHPMailer' into the 'vendor' folder of the root of your app from 'Packagist'. You should see the following folder structure ('tree vendor /f'):
There were also 2 files created in the root of your app:
Both of which contain the version we are using and the dependencies required to use PHPMailer. Also, by looking at the instruction on 'Packagist' you can swap to a different version of 'PHPMailer' and there are detailed instructions on how to use it.
For our use case with Trongate, all you need to do is create a new module or integrate the code into 'Members.php' - for this demo, I'll try to stay in line with what you did and use a general-purpose module like your 'basemodule', but I would have used camel case - 'base_module' for it's name.
On a side note - I have also loaded libraries inside of modules to keep them truly modular. For example, here is one I used to create invoices using a PDF library - where all vendor files reside in the assets folder:
Now we will follow DC's instructions inside the Members module 'README.txt'
The following 2 lines are added to the above methods in 'Members.php' to send the email:
And the controller in 'basemodule' would look like this - I'm using Gmail in this example for the email server - replace with yours:
Note: I am not using the SMTP class and I have used the global variables from 'site_owner.php' - so please replace them with yours.
I hope this helps in using PHPMailer via Composer and Autoloading it.
Please let me know if you have any questions.
Cheers,
Si
3 years ago
#7
Simon,
Thanks so much for taking the time and trouble to provide such a detailed response and I am sure it will help others besides me get more familiar with how to do this sort of thing.
I am making real progress with Trongate now and finding ways to incorporate my own way of working within its confines. The modular approach is a good one and coming from CodeIgniter 4 I am finding it a very useful concept. I think I just need to completely build a full solution with Trongate now and then I will be a lot happier.
Thanks again for your help with this particular stumbling block.
Tony.
Thanks so much for taking the time and trouble to provide such a detailed response and I am sure it will help others besides me get more familiar with how to do this sort of thing.
I am making real progress with Trongate now and finding ways to incorporate my own way of working within its confines. The modular approach is a good one and coming from CodeIgniter 4 I am finding it a very useful concept. I think I just need to completely build a full solution with Trongate now and then I will be a lot happier.
Thanks again for your help with this particular stumbling block.
Tony.
3 years ago
#8
Hi Tony, could you please revert the winning answer back to Dan, my intention here was to show a more general way of using Composer to autoload a library and not to steal Dan's thunder.
I'm so glad you are enjoying Trongate and coming from a Codeigniter 4 background I'd like to hear what you like about Trongate and why you are now using it over Codeigniter.
Cheers,
Si
Ps. I made an error above. Don't name any modules with 'base_module', as '_module' is the default trigger for the assets folder, unless you change it in the config.
I'm so glad you are enjoying Trongate and coming from a Codeigniter 4 background I'd like to hear what you like about Trongate and why you are now using it over Codeigniter.
Cheers,
Si
Ps. I made an error above. Don't name any modules with 'base_module', as '_module' is the default trigger for the assets folder, unless you change it in the config.
3 years ago
#9
Hi Simon,
I decided to give your example a go this morning however my attempt failed at the first hurdle!!
This is what I got in my terminal using VS Code:
I do have the openssl extension enable in my PHP version 7.4.33 so it seems to be a strange one - any ideas?
Tony
I decided to give your example a go this morning however my attempt failed at the first hurdle!!
This is what I got in my terminal using VS Code:
I do have the openssl extension enable in my PHP version 7.4.33 so it seems to be a strange one - any ideas?
Tony
3 years ago
#10
Hi Tony,
That is a strange error you are getting. I just checked my php.ini and I have that disabled:
I'm using PHP 8.1.17
Factory.php is part of Composer.
I asked ChatGPT about your error and it says to enable the openssl PHP extension. Mine is not enabled, so my thoughts are it's not the thing causing you grief. It did suggest the following:
My gut feeling is there is something wrong with your installation of Composer - what version are you on?
I'm on Composer version 2.3.10 2022-07-13 15:48:23 which I will now update to the latest version in an administrator command prompt:
Now I'll try again - no error for me with the latest version:
Also, what stack are you using? I'm using Xampp for Windows
Have you tried grabbing a different library from Packagist?
If all else fails you can disable tls in Composer:
That is a strange error you are getting. I just checked my php.ini and I have that disabled:
I'm using PHP 8.1.17
Factory.php is part of Composer.
I asked ChatGPT about your error and it says to enable the openssl PHP extension. Mine is not enabled, so my thoughts are it's not the thing causing you grief. It did suggest the following:
My gut feeling is there is something wrong with your installation of Composer - what version are you on?
I'm on Composer version 2.3.10 2022-07-13 15:48:23 which I will now update to the latest version in an administrator command prompt:
Now I'll try again - no error for me with the latest version:
Also, what stack are you using? I'm using Xampp for Windows
Have you tried grabbing a different library from Packagist?
If all else fails you can disable tls in Composer: