CodeIgniter Forums

Full Version: Several questions concerning CI4 controller
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Here the (working) class:

PHP Code:
<?php namespace App\Controllers;
 
use 
CodeIgniter\Controller;
use 
App\Models\ContactModel;
 
class 
Contact extends Controller
{
    public function __construct() {
        helper('html''url''form');
    }

    public function index()
    {
        $data = [
            'title'  => 'Contact',
            'src'    => 'images/logo.jpg',
            'alt'    => 'site dot net site logo',
            'heading' => 'Contact',
            'success' => 'Thank you for contacting us - we will respond to you shortly'
        ];

        return view('contact/contact'$data);
    }
 
    public function dispatch() // invoked by the 'action-url' in contact.php
    {
        $data = [
            'title'  => 'Contact',
            'src'    => 'images/logo.jpg',
            'alt'    => 'site.net site logo',
            'heading' => 'Contact',
            'success' => 'Thank you for contacting us - we will respond to you shortly.',
            'mailerror' => 'Server error - please send an email direct to info@site.net, thank you.'
        ];
         
        $val 
$this->validate([ // data checking 'convenience method'
            'name' => 'required|alpha_space',
            'email' => 'required|valid_email',
            'message'  => 'required|min_length[40]|max_length[750]',
        ]);
 
        $model = new ContactModel();
        $email = \Config\Services::email();
        $sender $this->request->getVar('email'FILTER_SANITIZE_EMAIL);
        $message $this->request->getVar('message');
 
        if (!$val)
        {
            echo view('contact/contact'$data);
 
        }
        else
        {  // $request = \Config\Services::request();
           // To use the request-object outside the Controller we need to copy
           // it through the Service class
            $model->save([
                'name' => $this->request->getVar('name'),
                'email'  => $this->request->getVar('email'FILTER_SANITIZE_EMAIL),
                'message'  => $this->request->getVar('message'),
            ]);

            $config['protocol'] = 'smtp';
            $config['mailPath'] = '';
            $config['charset']  'iso-8859-1';
            $config['SMTPHost'] = '';
            $config['SMTPUser'] = '';
            $config['SMTPPass'] = '';
            $config['SMTPPort'] = '';
            $config['SMTPTimeout'] = '';
            $config['SMTPKeepAlive'] = TRUE;
            $config['SMTPCrypto'] = 'tls';

            $config['mailType'] = 'text';
            $config['wordWrap'] = TRUE;
            $email->initialize($config);

            $email->setFrom($sender);
            $email->setTo('info@site.net');
            $email->setCC('');
            $email->setBCC('');
            $email->setSubject('Contact form');
            $email->setMessage($message);

            if ($email->send(false))
            {
                echo view('contact/mailerror'$data);
            }
 
            echo view('contact/success'$data);
        }
    }

.

1.) I want to put the $data array outside the whole class or outside the functions (both possible in C/C++) and pass them as a parameter to the function so there is less code. How can I accomplish that in Codeigniter/php?

2.) My email-server (postfix) is running with TLS encryption. I do not see any possibility to use certificates in CI(4). Please elaborate...

3.) The error message is never evaluated when used in another form of configuration (mail/sendmail) in case testing it on my localhost at home which obviously fails:
PHP Code:
echo view('contact/mailerror'$data);. 
.

4.) Is this still needed after checking in the validation process?
PHP Code:
FILTER_SANITIZE_EMAIL
.


Any help is appreciated.


Cheers

G.
(03-24-2020, 09:03 AM)bitshifter Wrote: [ -> ]1.) I want to put the $data array outside the whole class or outside the functions (both possible in C/C++) and pass them as a parameter to the function so there is less code. How can I accomplish that in Codeigniter/php?

There is more than one way to clean up the $data code. One easy way is to create a class property.

PHP Code:
class Contact extends Controller
{
    public $data = [
 
    'title' => 'Contact',
 
    'src' => 'images/logo.jpg',
 
    'alt' => 'site dot net site logo',
 
    'heading' => 'Contact',
 
    'success' => 'Thank you for contacting us - we will respond to you shortly'
    ];

//  rest of class


To use the property instead of this code

PHP Code:
echo view('contact/contact'$data); 

You use the $this object reference which can be thought of as being roughly equivalent to the c++ "this" pointer.

PHP Code:
echo view('contact/contact'$this->data); 
Ok, thank you.
Anything concerning the issue with the Certificates for the email server?
I have no clue how to apply...
(03-25-2020, 08:22 AM)bitshifter Wrote: [ -> ]Anything concerning the issue with the Certificates for the email server?

The SSL certificates must be installed in Postfix which requires some edits to the Postfix configuration file. After that, the CodeIgniter config setting SMTPCrypto is the most important for your needs, e.g.

PHP Code:
public $SMTPCrypto 'tls'

And of course, you need to set all the other SMPT* config items as appropriate for your Postfix setup.
Yes sure, as you can see above I configged it already as TLS.
Despite of remote or not to my knowledge there is a certificate necessary to connect to the postfix server.
The or my postfix server won't even think about letting you connect without one as it is running 100% encrypted in either way.

How does CI establish the connection to the email server and how does CI establish the tls handshake and stuff.
That's what my quest is/was aiming on and so far I can judge it there is a gap to fill in the documentation isn't it?

Or is it there somewhere out there a 'holy grail of email' and I need to search for my coconut again and ride out to find it? :-)
CI doesn't handle TLS encryption that is a function of the mail transport application (Postfix, in your case).

Read about Postfix and TLS.

CI does not need to " establish the connection to the email server" because the mail server is literally a program residing on the same server as CI and that CI calls directly.

In terms of the connection between a remote mail client and the CI server, the use of TSL depends entirely on the user's email client setup. There isn't anything a remote server can do to influence how the client is set up.

Other information sources you might find useful.

https://kruyt.org/postfix-and-tls-encryption/

https://upcloud.com/community/tutorials/...s-encrypt/
Sorry for the late reply.
CI and other topics keep me actually busy.

While ago that I played with my mail-server(s).
Yeah, sure in case of running on the same server there should be no need for a certificate at all, agreed.
But why the heck at all configure for TLS?

Need some more nerd stuff here...

What I'd like to know is how CI establishes the connection to the (local) postfix server and how it uses its resources to write out into the web, read mails etc. pp.
(03-28-2020, 12:13 PM)bitshifter Wrote: [ -> ]What I'd like to know is how CI establishes the connection to the (local) postfix server and how it uses its resources to write out into the web, read mails etc. pp.

CI gathers the config setting and other inputs (e.g. to, from, subject, etc.) and passes them as arguments to the mail server executable (e.g. Postfix, Sendmail, Exim, etc.) installed on the computer hosting the webserver.  After that, it is all up to the mail server program. When the mail server exits it returns a status to CI that ultimately shows up as the true or false that $email->send() returns.
Quote:...gathers the config setting and other inputs (e.g. to, from, subject, etc.) and passes them as arguments to the mail server executable...


Hi,

sorry for the late reply and thanks for the elaboration...
Can you become a little bit more precise about what's stated above?

I'd like to get a grasp about how CI does handle these things or even better.... where... (code)?