PHP Warning: file_get_contents: failed to open stream: HTTP request failed!

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


namespace App\Services;

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

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 = '';
            //$toEmail = '';
            $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));


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

        $message = str_replace('HTMLMessage', $message, $templateMail);
        $mail = new PHPMailer;
        $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;
        if ($replyTo !== null) {
            $mail->addReplyTo(trim($replyTo), $replyToName);
        $mail->addAddress(trim($toEmail), $toName);
        if ($copyTo) {
            for ($i = 0; $i < sizeof($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

So this URL


works if you call it in a browser?


Where are you running this script?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.