Mailer: html content missing and html/text content missing

Hello,

I have two problems with sending emails.
I use Yii 2.0.32 and yii\swiftmailer\Mailer with SMTP.

  1. My first problem is maybe related to the second. I have problems with the html emails, if I use
    $m = $m->compose($view, $params);
    then the content is complete missing. But it is rendered, if I believe the logs.
    If I use
    $m = $m->compose([‘html’ => $view, ‘text’ => ‘text/’.$view], $params);
    then the content is only text, the html part is missing.

  2. The second problem is, that if I try to send two emails (one after the other) the first email has content, but the content in the second email is complete missing.
    In the log there is logged that the views for both emails are rendered, but in the eml file of the second email there is only the header and no content.

Thank you very much for your help.

My Mailer class:

<?php

namespace project\helpers;

use Yii;
use yii\base\Component;

/**
 * Mailer for sending e-mails to the user.
 */
class Mailer extends Component
{
    /** @var string Path to the view files. */
    public $viewPath = '@project/helpers/views/mail';

    /**
     * Send a message (mail).
     * @param string $to
     * @param string $subject
     * @param string $view
     * @param array  $params
     * @param array|null  $attachment
     * @param string|null $bcc 
     */
    public function sendMessage($to, $subject, $view, $params = [], $attachment  = null, $bcc = null)
    {
        /** @var \yii\mail\BaseMailer $mailer */
        $mailer = Yii::$app->mailer;
        $mailer->viewPath = $this->viewPath;
        $mailer->getView()->theme = Yii::$app->view->theme;
        if (is_null($params)) {
            $params = [];
        }

        $m = $mailer;
        $m = $m->compose(['html' => $view, 'text' => 'text/'.$view], $params);
        //$m = $m->compose($view, $params); // BUG empty content, this compose is only html with auto non-html, but html does not work in the other stmts so here not too => bam empty content?
        // BUG then content is complete missing in email! the file is rendered (log): 
        //$m = $m->compose(['html' => $view], $params);
        // BUG Rendering view file: /home/yanick/public_html/dev_env_yii2-all/vendor/projects/yii2-helpers/views/mail/terms-changed.php
        // BUG but not in email...

        $m->setTo($to);
        if (!is_null($bcc)) {
            $m->setBcc($bcc);
        }
        if (!is_null($attachment)) {
            $m->attach($attachment[0], $attachment[1]);
        }
        $m->setFrom(Yii::$app->params['adminEmail'])
            ->setSubject($subject)
            ->send();
    }
}

Mailer use:

// my Mailer class
$Mailer = new Mailer();
foreach ($users as $user) {
	$Mailer->sendMessage(
		$user->email,
		'the terms changed',
		'terms-changed',
		[
		      'name' => $user->username,
		]
	);
}

Hello and welcome Yanick,

so Yii::$app->mailer is the registered component of Swiftmailer and you made your own Mailer class that just executes on the mailer component?

Your commented line

// BUG Rendering view file: /home/yanick/public_html/dev_env_yii2-all/vendor/projects/yii2-helpers/views/mail/terms-changed.php

looks like you put your project in the vendor directory. Is there a specific reason for that?
Normaly, application directories resides outside of it.

Yes, I use switftmailer and my class executes it.

// BUG Rendering view file:

The word BUG shouldn’t be there… That is the output of the debug log which shows that the file is rendered, but missing in the eml file.

I have divided my project in subprojects which are extensions.

When I do the following then html and text is there in every email (but of course layout around missing)! But composing with (html… text…) does not work, but I need the layout…

// works:
$mailer = Yii::$app->mailer;              
$m = $mailer->compose();

$m->setTo($user->email)
    ->setFrom(Yii::$app->params['adminEmail'])
    ->setSubject($subject)
    ->setTextBody($subject)
    ->setHtmlBody('<p>def ghi</p>')
    ->send();

// fails: in first email only text, html missing; in second email content complete missing:

$mailer = Yii::$app->mailer;
$mailer->viewPath = '@project/helpers/views/mail';
$mailer->getView()->theme = Yii::$app->view->theme;

$view = 'terms-changed';
$m = $mailer->compose(['html' => $view, 'text' => 'text/'.$view],  ['name' => $user->username]);

$m->setTo($user->email)
    ->setFrom(Yii::$app->params['adminEmail'])
    ->setSubject($subject)
    ->send();

There seems to be a bug.
If I disable the send() part and only try to get the body of switftmessage I enter after compose:

echo 'content: '.$m->getSwiftMessage()->getBody().'<br>';

then the first is OK, but the second mail there is no string after ‘content’.

I would output the mail object/current module context (with Yii::debug() ) at certain places, where nothing happens like you expect. Then investigate further.

Thank you for your help!

The Yii::debug output gives the same: body is not empty for first email, but body is string length 0 for second email (first email: 'body";s:503:"HERE IS THE TEXT BODY OF THE EMAIL", second email 'body";s:0:"";)

In


compose(with template file) is only executed once, not several times consecutively. But I do call this several times in the same request.

Should there really be a bug, but then others would probably have already reported a problem?

@samdark Maybe you can help?

Okay, I found the problem.
I have email layout templates which I included in other email layout templates so that I only have to update one file.
But this causes the problem.
I copied my email template to all subprojects so I do not include any files inside the template.

Now it works!
Thank you very much for your help!