2015-12-15 45 views
0

在我的TYPO3 6.2.x擴展中,我通過函數發送了郵件。TYPO3中的MailMessage類7.2如何使用它?

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中它不起作用。如何使用它?

+0

您是否收到錯誤訊息? –

+0

沒有錯誤。郵箱中沒有郵件。如果我嘗試從安裝工具發送測試郵件 - 它可以從powermail運行 - 起作用。如果我嘗試 –

+0

發送測試郵件,如https://docs.typo3.org/typo3cms/CoreApiReference/ApiOverview/Mail/Index.html - 無。我嘗試安裝工具中的smtp和郵件設置 –

回答

4

試試這個,它適用於我[7.6.0]。可能會幫助你。從你的行動

/** 
* @param array $recipient recipient of the email in the format array('[email protected]' => 'Recipient Name') 
* @param array $sender sender of the email in the format array('[email protected]' => '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 
); 
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(); 
} 
} 

請更改佔位符 「/無功/網絡/ 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.隨意使用它!

+0

dirname(dirname(dirname(dirname(__ FILE__))))? – j4k3

+0

這將返回擴展文件夾的絕對路徑,如'/ var/www/typo3conf/ext'在我的擴展中,這個類位於** Classes/Util **之下。如果這個類在**類**下,你需要寫'dirname(dirname(dirname(FILE)))''。我會更新代碼! –