Get email authentification's info

Hello to the wonderful team!

For a client, I dev a contact form that send an email once the form is complete (following the instructions from Email contact form | Kirby CMS) and my question is,

in order to secure the authentification’s info (host name, user, password…), and give the possibility to my client to change info easily, is there a way to keep these infos in a user template? And paste them in config.php, after getting the right $user i.e.

return [
  'email' => [
    'transport' => [
      'type' => 'smtp',
      'host' => $user->host(),
      'port' => $user->port(),
      'security' => true,
      'auth' => true,
      'username' => $user->email(),
      'password' => $user->password(),
    ]
  ]
];

Thanks for your help!

Any config option that needs access to the kirby() or site() objects (which is the case when you want to get access to a user) needs to be put within the ready config option:

Ok, sorry, I didn’t see that, I’m not (yet) a real pro with php.

If I understand well, in config.php:

return [
    'ready' => function() {
        return [
          'email' => [
            'transport' => [
              'host' => kirby()->users()->role('email')->nth(0)->host(),
              'port' => kirby()->users()->role('email')->nth(0)->port()->toInit(),
              'username' => kirby()->users()->role('email')->nth(0)->email(),
              'password' => kirby()->users()->role('email')->nth(0)->password(),
            ]
            ]
        ];
    },
  'email' => [
    'transport' => [
      'type' => 'smtp',
      'security' => true,
      'auth' => true,
    ]
  ]

But after that, I have an error Object of class Kirby\Cms\Field could not be converted to int
At this point \src\Email\PHPMailer.php79

if ($mailer->SMTPSecure === true) {
                switch ($mailer->Port) {
                    case null:
                    case 587:
                        $mailer->SMTPSecure = 'tls';
                        $mailer->Port = 587;
                        break;
                    case 465:
                        $mailer->SMTPSecure = 'ssl';
                        break;
                    ...

I guess it’s the way you write “kirby()->users()->role(‘email’)->nth(0)->port()->toInit()”

Thanks!!

  1. You have to keep the email transport configuration together, do not separate it into two blocks
return [
	'ready' => function() {
		return [
			'email' => [
				'transport' => [
					'host' => '',
					'port' => '',
					'username' => '',
					'password' => '',
					'type' => 'smtp',
					'security' => true,
					'auth' => true,
				]
			]
		];
	},
// ...
];
  1. And then you probably need to get the values instead of using a field object, so
'host' => kirby()->users()->role('email')->first()->host()->value(),

etc.

Maybe better fetch the user by email or id, so that when for some reason multiple users with that role are created, it will still work?

Great!! That works fine, thank you so much!

Yes, I thought about it but there will be only one user with this template. The email could change because it is used for the smtp server authentification.

Last thing, user()->password() return the encrypted password, of course, so I don’t think there is a possibility to use it to authenticate to the email server? I have to create a new field to the user template with the clear password of the server?

No, of course not.

Ok, ok, now I understand what you are doing. You are “abusing” users to store authentication information. I thought you wanted to store this information inside an existing user to hide the information from other users. Misunderstanding on my side.

So I wonder if what you are doing there is the best way to go about this.

But in any case, you would have to create a separate field for the password.

Yes, but then the id would not change (the id is the folder name of the account folder)

Oh ok! No, my bad, that wasn’t clear. My idea was to allow my client to udpate information about the email server, on a easy way. Not to connect to the server and change these information directly in config.php

If you have a suggestion… I thought about .env file to store the password there but I heard it depends on the hosting policy of providers (i.e. shared hosting).

Anyway, thanks a lot!!