File_get_contents() equivalent ? (https issue)

Hello,

I’m trying to send HTML emails.
It worked perfectly until I switched for a secured https://. Now my file_get_contents returns an Unknow protocol error I can’t manage to overcome.

routines:SSL23_GET_SERVER_HELLO:unknown protocol

Basically my code :

  • Creates a page, populating a few fields

      $newClientMail = $pages->find('modeles-mail')->children()->create($id, 'mailclient', array(
        'title' => $id,
        'date'  => (...)
      ));
    
  • Get the html content of the page with a file_get_contents()

  • Set it as my mail’s body, and send it.

      $emailClient = email(array(
          'from'    => $pages->find('modeles-mail')->mailenvoi(),
          'to'      => $mail,
          'service' => 'html_email',
          'subject' => $pages->find('modeles-mail')->content($langue)->get('sujet') .  ' - ' . $id,
          'body'    => file_get_contents($url)
      ));
    

My question is : is there a file_get_contents equivalent in Kirby that could make it easier for me to fetch content ?
Or would it be no help at all anyway ?

Thanks !

In the past I have used a snippet:

// ...

$data = [
    'name'    => get('name'),
    'email'   => get('email'),
    'message' => get('message'),
];

// do validation

$email = email([
    'subject' => 'Message from ' . $data['name'],
    'replyTo' => $data['email'],
    'body'    => snippet('contact-email', $data, true),
]);

This way you don’t even need to create a page, unless you would need it later on.

Kirby ships with remote::get($url, $params = []) if you really want to make that request, check it out.

Oh nice, I didn’t think of a snippet. Thanks for your answer ! :slight_smile:

Turns out my https problem is an issue that OVH is aware of and they will deploy a fix soon.
I got it to work with an ugly str_replace("https://mydomain", "http://mydomain:443", $url); for now, but I’d rather manage it differently.

I’d also go with the snippet option.

It seems way more convenient but I wonder, how would you keep a log of the unsent mails ?

This way (creating pages) I create the page -> send the mail -> if successfully sent I delete the page, if not I don’t, and keep it at hand in an ‘unsent mails’ list so that I can reschedule it, for example, or just be informed of the error after the harm is done.

You could still create the page and then get the data for the snippet from the page instead of directly from the form, that was just an example. As an alternative, you can keep a log file.

Ok, it took its time to fully wrap my late-evening head around it, but I get the logic. Many thanks !

It almost works like a charm.

Emails are sent, snippets are called, pages are created, deleted, it works fine.

But my problem is, I have a multi-lang setup, and calling the snippet without any language context, all the l::get of my snippet return an empty string. I’ve made some attempts but nothing succeeded, how could I activate a language (from its code as an argument, or another way) once I’m on the snippet without prior context ?

I can’t try this right now but the first thought I had without seeing your code is to pass the language to the snippet along with the $data array.

$data = [
    'name'     => get('name'),
    'email'    => get('email'),
    'message'  => get('message'),
    'language' => l::get() // or l::$data
];

// …

Do you have access to the language data in the context above? Then you could use it on the template like so:

echo $language['email.name'];

The Language class is trick, I’ve had issues with it on plugins. Not sure what is happening in your case.

1 Like

I didn’t have access to the whole language data since I previously only needed to create a page url with the appropriate code.

Now what I’m doing is I’m getting the html tag’s lang attribute from the front, include it in the data I send in my Ajax request, then require the appropriate language file when I trigger the function :

require_once __DIR__ . '/../../' . 'languages' . DS . $language . '.php'; 

After that, passing l::get() to the snippet works wonder ! Thanks one more time for your help !