1

I've written a plugin that sends and email to a user group on new entry save in a particular section. Most entries will be made from the front end, and the plugin is working great in that regard. However, if a user attempts to create a new entry from the CP, the plugin cannot find my designated template path and throws an error. Any help is greatly appreciated.

if ($isNewEntry and $entrySection == 'Questions')
{
    //Get all users from UserGroup
    $user_criteria = craft()->elements->getCriteria(ElementType::User);
    $user_criteria->groupId = '9';
    $users = $user_criteria->find();
    $template = '/email/question-email.html';

    //Send to each User
    foreach ($users as $user)
    {
        //Send Mail
        $email = new EmailModel();
        $email->toEmail = $user->email;
        $email->subject = 'New Question';
        $email->htmlBody = "{% extends '{$template}' %}".
            "{% set body %}".
            '
            <p>A new question has been posted by '.$entry->author->name.':</p>

            <h2>Question: '.$entry->title.'</h2>

            <p>Description: '.$entry->questionDetails.'</p>

            <p>Attachments: '.$entry->questionAttachments->total().'</p>

            <p><a style="color: #09c;" href="'.$entry->url.'" title="'.$entry->title.'"><b>Answer Question</b></a></p>
                        '
            ."{% endset %}";

        craft()->email->sendEmail($email);
    }
}
1

You probably just need to call PathService::setTemplatesPath() and pass in the path to your plugin's template folder.

It would look something like:

// Save the current template path.
$originalPath = craft()->path->getTemplatesPath();

// Set your plugin's template path.
$path = craft()->path->getPluginsPath().'/mypluginfolder/templates/';
craft()->path->setTemplatesPath($path);

//Send to each User
foreach ($users as $user)
{
    //Send Mail
    $email = new EmailModel();
    $email->toEmail = $user->email;
    $email->subject = 'New Question';
    $email->htmlBody = "{% extends '{$template}' %}".
        "{% set body %}".
        '
        <p>A new question has been posted by '.$entry->author->name.':</p>

        <h2>Question: '.$entry->title.'</h2>

        <p>Description: '.$entry->questionDetails.'</p>

        <p>Attachments: '.$entry->questionAttachments->total().'</p>

        <p><a style="color: #09c;" href="'.$entry->url.'" title="'.$entry->title.'"><b>Answer Question</b></a></p>
                    '
        ."{% endset %}";

    craft()->email->sendEmail($email);
}

// Reset the original path.
craft()->path->setTemplatesPath($originalPath);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.