I'm working on a project built with php slim, where I have a registration screen that when clicking on register triggers the /register route of the parent route "/clients"

This is the error:

PHP Warning: file_get_contents(http://localhost:8080/assets/mail/template.html): failed to open stream: HTTP request failed!

`$templateMail = file_get_contents(self::$templateMail);

$message = str_replace('HTMLMessage', $message, $templateMail);`

This is the line where the error occurs

The complete file is called Email.php

`<?php

namespace App\Services;

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

use PHPMailer\PHPMailer\PHPMailer;

class Email
{
    
    private static $HOST = EMAIL_HOST;
    private static $LOGIN = EMAIL_LOGIN;
    private static $PASS = EMAIL_PASS;
    private static $PORT = EMAIL_PORT;
    private static $SMTPSecure = EMAIL_SMTP_SECURE;
    private static $SMTPAuth = EMAIL_SMTP_AUTH;
    private static $templateMail = BASEURL . 'assets/mail/template.html';
    
    public static function send(String $toEmail, String $toName, String $subject, String $message, String $replyTo = null, String $replyToName = null, $attachments = null, $copyTo = null)
    {
        if (ENV == 'local') {
            $toEmail = 'caleksitch@hotmail.com';
            //$toEmail = 'lucasdgimenez@gmail.com';
            $subject = "Teste Fundamenta - {$subject}";
        }

        /*$curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, self::$templateMailURL);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        $templateMail = curl_exec($curl);

        if ($templateMail === false) {
            throw new \Exception("Failed to fetch template using cURL: " . curl_error($curl));
        }

        curl_close($curl);*/

        
        $templateMail = file_get_contents(self::$templateMail);

        $message = str_replace('HTMLMessage', $message, $templateMail);
        
        $mail = new PHPMailer;
        $mail->isSMTP();
        $mail->CharSet = 'UTF-8';
        $mail->SMTPDebug = 0; //2 para modo debug
        $mail->Host = self::$HOST;
        $mail->Port = self::$PORT;
        $mail->SMTPSecure = self::$SMTPSecure;
        $mail->SMTPAuth = self::$SMTPAuth;
        $mail->Username = self::$LOGIN;
        $mail->Password = self::$PASS;
        $mail->SMTPOptions = array(
            'ssl' => array(
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
            )
        );
        $mail->setFrom(self::$LOGIN, 'Fundamenta');
        $mail->Subject = $subject;
        $mail->Body = $message;
        $mail->IsHTML(true);
        if ($replyTo !== null) {
            $mail->addReplyTo(trim($replyTo), $replyToName);
        }
        $mail->addAddress(trim($toEmail), $toName);
        if ($copyTo) {
            for ($i = 0; $i < sizeof($copyTo); $i++) {
                $mail->addCC($copyTo[$i]);
            }
        }
        if ($attachments) {
            foreach ($attachments as $attachment) {
                $mail->addAttachment($attachment[0], $attachment[1]);
            }
        }
        if (!$mail->send()) {
            throw new \Exception($mail->ErrorInfo);
        }
    }
    
    
}`

I've already tried to google this problem and most indicate curl's solution instead of file_get_contents, the problem is that when trying to implement this curl solution (which is commented in the code above) the system runs an infinite loading, I don't know exactly which one that could be the problem.

The php version is 7.4 and the apache version is 2.4.52

My first thought is: why would you want to fetch a local file via HTTP? It seems like it would be easier and more efficient to just grab it via the file system, either a relative path from the current file or a full file path.

    lucasdgimenez
    Have you checked to see if the file system folder is shared and has the appropriate READ/WRITE properties?

      23 days later

      The error message you're encountering, failed to open stream: HTTP request failed!, indicates that file_get_contents is unable to fetch the content from the URL specified in self::$templateMail. In your code, self::$templateMail is constructed as BASEURL . 'assets/mail/template.html'.

      To fix this issue, you should ensure that the URL you are trying to fetch is valid and accessible. Here are some steps to troubleshoot and resolve the issue:

      Check the URL: Make sure that BASEURL . 'assets/mail/template.html' is a valid URL and that it points to the correct location of the template.html file on your web server. You can test this by manually entering the URL in a web browser to see if it loads the file.

      Verify Permissions: Ensure that the web server has the necessary permissions to access the template.html file. Check the file's permissions and make sure that the web server user has read access to it.

      Error Handling: Add error handling to your code to handle cases where file_get_contents fails to fetch the content. You can use file_get_contents with a URL like this:

      php
      Copy code
      $templateMail = @file_get_contents(self::$templateMail);

      if ($templateMail === false) {
      throw new \Exception("Failed to fetch template using file_get_contents: " . error_get_last()['message']);
      }
      This will capture the error message when file_get_contents fails and include it in the exception message for easier debugging.

      Debugging: If the issue persists, you can use debugging tools to inspect the self::$templateMail URL and the HTTP request being made. You can use tools like curl or PHP's var_dump to print out variables and check their values during runtime.
      By following these steps and ensuring that the URL is correct and accessible, you should be able to resolve the issue with file_get_contents failing to fetch the template content. [Mod: spam link deleted]

        Write a Reply...