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

**URL:** https://discourse.roots.io/t/email-message-w-no-contents-when-including-with-template/20801
**Category:** sage
**Tags:** sage9
**Created:** 2021-05-18T21:46:02Z
**Posts:** 9

## Post 1 by @matteo_maria_ambrogi — 2021-05-18T21:46:02Z

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?

---

## Post 2 by @joshb — 2021-05-18T22:47:09Z

Are you using [Sage HTML Forms](https://github.com/Log1x/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.

---

## Post 3 by @matteo_maria_ambrogi — 2021-05-19T05:26:46Z

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

Maybe I am including the file from wrong path?

---

## Post 4 by @matteo_maria_ambrogi — 2021-05-19T09:57:11Z

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…

---

## Post 5 by @alwaysblank — 2021-05-19T13:50:04Z

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>
```

---

## Post 6 by @matteo_maria_ambrogi — 2021-05-19T17:12:47Z

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:

---

## Post 7 by @alwaysblank — 2021-05-19T18:08:42Z

> [@matteo_maria_ambrogi](#):
>
> ```
> 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();
> ```

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.

---

## Post 8 by @matteo_maria_ambrogi — 2021-05-19T18:26:32Z

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);
```

---

## Post 9 by @system — 2021-06-29T21:46:04Z

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