TYPO3中的MailMessage类7.2如何使用它?

时间:2015-12-15 14:38:22

标签: typo3 extbase

在TYPO3 6.2.x的扩展程序中,我通过该功能发送了邮件。

protected function sendMail($senderEmail, $recipientEmail, $subject, $message) {
    $this->view->assign('settings', $this->settings);   
    $mail = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Mail\\MailMessage');
    $mail->setFrom(array($this->settings['fromEmail']));
    $mail->setTo(array($this->settings['toEmail']));
    $mail->setSubject($this->settings['subject']);
    $mail->setBody(htmlspecialchars_decode($message), 'text/html');
    if($this->settings['debugMail'] == 1) {
        $this->debug($message);
    } else {
        $mail->send();
    }
}

效果很好。但在TYPO3 7.2中,它无法正常工作。怎么用?

2 个答案:

答案 0 :(得分:4)

试试这个,它对我有用[7.6.0]。可能会帮助你。

/**
* @param array $recipient recipient of the email in the format array('recipient@domain.tld' => 'Recipient Name')
* @param array $sender sender of the email in the format array('sender@domain.tld' => 'Sender Name')
* @param string $subject subject of the email
* @param string $templateName template name (UpperCamelCase)
* @param array $variables variables to be passed to the Fluid view
* @return boolean TRUE on success, otherwise false
*/
protected function sendTemplateEmail(array $recipient, array $sender, $subject, $templateName, array $variables = array(), $attachments) {

    /** @var \TYPO3\CMS\Fluid\View\StandaloneView $emailView */
    $emailView = $this->objectManager->get('TYPO3\\CMS\\Fluid\\View\\StandaloneView');

    $extbaseFrameworkConfiguration = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
    if(empty($extbaseFrameworkConfiguration['view']['templateRootPath'])){
        $extbaseFrameworkConfiguration['view']['templateRootPath'] = $extbaseFrameworkConfiguration['view']['templateRootPaths'][0];    
    }
    $templateRootPath = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($extbaseFrameworkConfiguration['view']['templateRootPath']);
    //DebuggerUtility::var_dump($templateRootPath);exit;
    $templatePathAndFilename = $templateRootPath . 'EmailTemplate/' . $templateName . '.html';
    $emailView->setTemplatePathAndFilename($templatePathAndFilename);
    $emailView->assignMultiple($variables); // assign Fluid variable
    $emailBody = $emailView->render();

    /** @var $message \TYPO3\CMS\Core\Mail\MailMessage */
    $message = $this->objectManager->get('TYPO3\\CMS\\Core\\Mail\\MailMessage');
    $message->setTo($recipient)
          ->setFrom($sender)
          ->setSubject($subject);

    // Possible attachments here
    /*if (count($attachments)) {
        foreach ($attachments as $file => $name) {
            if (file_exists($file)) {
                if (trim($name)) {
                    $message->attach(\Swift_Attachment::fromPath($file)->setFilename($name));
                } else {
                    $message->attach(Swift_Attachment::fromPath($file));
                }
            }
        }
    }*/

    // Plain text example
    //$message->setBody($emailBody, 'text/plain');

    // HTML Email
    $message->setBody($emailBody, 'text/html');

    $message->send();
    return $message->isSent();
}

从您的行动致电:

$isSend = $this->sendTemplateEmail(
    array($email => $user['title'].$user['first_name'].' '.$user['last_name']), // email TO $value['email']
    array($fromEmail => $fromName), // email From
    $subject, // Subject
    'YourEmailTemplateName', // Template name
    array('user' => $user) // Fluid variable
);

答案 1 :(得分:0)

我创建了一个帮助器类,可以使用任何模板从任何扩展名调用!

/**
 * Container class for mails
 * @package 
 */
class Mailer
{

/**
 * @param ControllerContext $context the controller, which calls this method
 * @param string $templatePath the path for the mail template like '/Resources/Private/Templates/.../'
 * @param string $templateName the name of the mail template without .html
 * @param array $assignArray the array with assign values for the template
 * @param string $sendTo the receiver address
 * @param string $sendFrom the sender address
 * @param string $subject the mail subject
 */
public static function sendMail(ControllerContext $context, $templatePath, $templateName, $assignArray, $sendTo, $sendFrom, $subject){

    if($context->objectManager == null){
        $context->objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
    }

    $directoryName = '<</var/www/typo3/home/of/extensions/ext/>>' . strtolower($context->getRequest()->getControllerExtensionName());

    // Mail erstellen
    $templateName = $templateName;

    /** @var $emailView StandaloneView  */
    /** @noinspection PhpMethodParametersCountMismatchInspection */
    $emailView = $context->objectManager->get(\TYPO3\CMS\Fluid\View\StandaloneView::class, $context->contentObject);

    $emailView->setLayoutRootPaths(array($directoryName . '/Resources/Private/Layouts/'));

    $emailView->setPartialRootPaths(array($directoryName . '/Resources/Private/Partials/'));

    $templateRootPath = $directoryName . $templatePath;

    $templatePathAndFilename = $templateRootPath . $templateName . '.html';
    $emailView->getRequest()->setControllerExtensionName($context->getRequest()->getControllerExtensionName());
    $emailView->setTemplatePathAndFilename($templatePathAndFilename);

    $emailView->assignMultiple($assignArray);

    $emailBody = $emailView->render();

    /** @var $message MailMessage */
    $message = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Mail\\MailMessage');

    $message->setTo(
        $sendTo
    )->setFrom(
        $sendFrom
    )->setSubject(
        $subject
    );

    $message->setBody($emailBody, 'text/html');
    $message->send();
}
}

请将占位符“/ var / www / typo3 / home / of / extensions / ext /”更改为您的扩展主目录,例如“/ var / www / typo3 / typo3conf / ext /”!

现在你可以从这样的控制器中调用这个类:

Mailer::sendMail($this->getControllerContext(), '/Resources/Private/Templates/<<SubFolders>>/', '<<Name of your Template>>', array(
        'name' => $value,
        'name2' => $value2,
        'name3' => $value3,
        ....
    ), 'test@test.de', 'no-replay@test.de', 'Subject');

此示例适用于Typo3 7.6.x至8.x.x.随意使用它!