Email message w/ NO contents when including with template()

hello, according to another post here I was trying to include an HTML template for a mail.

...
$headers[] = 'MIME-Version: 1.0';
            $headers[] = 'Content-type: text/html; charset=UTF-8';
            $headers[] = 'From: MY_NAME <info@example.com>';

            $title   = 'This is the title';
            $message = "Verifica la tua email cliccando su: <a href='$url'> ATTIVA EMAIL</a><br> Oppure incolla nel browser: $url";
            $data = ['message' => $message, 'title' => $title];
            $content = \App\template('email/email', $data);
 wp_mail($params['email'], 'Verifica la tua email', $content, $headers);
...

views/email/email.blade.php

<!DOCTYPE html PUBLIC>
<html>
<head>
  <title><?php echo esc_html( $data['title'] ); ?></title>
</head>
<body>
<p><?php echo $data['message']; ?></p>
</body>
</html>

unfortunately I keep receiving just an empty email “the message has no content” warning…
any hint?

Are you using Sage HTML Forms? I’m having the same problem using that. My forms submit fine and are being received thru MailHog but the subject and body are empty. However, they do post the submission in HTML Forms tables in the backend.

Nope.
I am calling the function in an Ajax.php controller

Maybe I am including the file from wrong path?

the issue seems connected either to the html content or to the way I am passing data to the email template.

as simplifying the HTML and not passing data, the email is correctly delivered…

When you pass a data array to a view, the array rows are converted to variables that take their names from the array keys. Your template is trying to access values in an array, not the correct variables, which is probably throwing an error and resulting in “no content.”

i.e. it should be

<p><?php echo $message; ?></p

not

<p><?php echo $data['message']; ?></p>
1 Like

yes, I updated the code this morning, but, It’s not working and still giving empty content.

trying to do something with buffer:

ob_start();
                \App\template('email.email', $data);
                $title   = 'This is the title';
                $message = "Verifica la tua email cliccando su: <a href='$url'> ATTIVA EMAIL</a><br> Oppure incolla nel browser: $url";
                $data = ['message' => $message, 'title' => $title];
                $content = ob_get_contents();
            ob_end_clean();

but it’s not working now :frowning:

I don’t know what you expect this code to do. The buffer functions only capture stuff that is put into the buffer and nothing in between ob_start/end_clean() outputs anything. Also the you call template() with a variable you don’t set until several lines after your call to template().

I would start by breaking this down to something that functions, then incrementally add in functionality until you find the part that breaks, then examine your logs to see if it’s throwing errors.

this is now working as expected:

$url = get_site_url()."/login";
            $url = add_query_arg(array(
                'a' => 'verify',
                'uid' => $user_id,
                'code' => $code
                ), $url);

            $headers[] = 'MIME-Version: 1.0';
            $headers[] = 'Content-type: text/html; charset=UTF-8';
            $headers[] = 'From: Direzione no profit <info@atravisio.com>';
            ob_start();
            $message = "Verifica la tua email cliccando su: <a href='$url'> ATTIVA EMAIL</a><br> Oppure incolla nel browser: $url";
            $mail = \App\template( 'email.email', array('message' => $message));
            print($mail);
            $content = ob_get_contents();
            ob_end_clean();

 wp_mail($params['email'], 'Verifica la tua email', $content, $headers);

This topic was automatically closed after 42 days. New replies are no longer allowed.