我創建了一個輔助類,它可以與任何模板任何分機來電!
/**
* 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();
}
}
請更改佔位符 「/無功/網絡/ TYPO3 /家/的/擴展/轉/」 將擴展程序的主目錄,如 「/無功/網絡/ TYPO3/typo3conf /轉/」!
現在,你可以從一個控制器調用這個類是這樣的:
Mailer::sendMail($this->getControllerContext(), '/Resources/Private/Templates/<<SubFolders>>/', '<<Name of your Template>>', array(
'name' => $value,
'name2' => $value2,
'name3' => $value3,
....
), '[email protected]', '[email protected]', 'Subject');
此示例爲TYPO3的7.6.x到8.x.x.隨意使用它!
您是否收到錯誤訊息? –
沒有錯誤。郵箱中沒有郵件。如果我嘗試從安裝工具發送測試郵件 - 它可以從powermail運行 - 起作用。如果我嘗試 –
發送測試郵件,如https://docs.typo3.org/typo3cms/CoreApiReference/ApiOverview/Mail/Index.html - 無。我嘗試安裝工具中的smtp和郵件設置 –