phptypo3extbasetypo3-6.2.x

Creating an attachment for MailMessage


From my extbase 6.2 extension I want to send different e-mails.
In a controller class I wrote a mail function that looks like this:
(notice no @param for $attachment - see my question at the end)

/**
 * 
 * @param string $to
 * @param string $subject
 * @param string $email_prefix
 * @param string $msg
 * @param string $email_suffix
 */
public function mailAction($to, $subject, $email_prefix, $msg, $email_suffix, $attachment = null) {
    try {
        $from = \TYPO3\CMS\Core\Utility\MailUtility::getSystemFrom();
        $body = $email_prefix
                . PHP_EOL . PHP_EOL
                . $msg
                . PHP_EOL . PHP_EOL
                . $email_suffix;
        $htmlBody = nl2br($body);
        $toEmail = $to;
        $mail = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Mail\\MailMessage');
        $mail->setFrom($from)
                ->setTo(array($toEmail))
                ->setSubject($subject)
                ->setBody($htmlBody, 'text/html');
        $mail->addPart($body, 'text/plain');
        if ($attachment) {
            $mail->attach($attachment);
        }
        if (empty($toEmail) || strpos($toEmail, '@') === FALSE) {
            $this->addFlashMessage('Die Mail konnte nicht verschickt werden! Keine Email-Adresse des Empfängers', '', \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
            );
        } else {
            if ($mail->send()) {
                $this->addFlashMessage('Die Mail für wurde verschickt!', '');
            } else {
                $this->addFlashMessage('Die Mail konnte nicht verschickt werden!', '', \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
                );
            }
        }
        $this->redirect('list');
    } catch (Exception $e) {
        echo 'Caught exception: ', $e->getMessage(), "\n";
    }
}

In a function that calls the mail function I tried creating an attachment like this but it failed saying: Fatal error: Class 'Swift_Attachment' not found in.../...Controller.php

$attachment = \Swift_Attachment::newInstance()
                ->setFilename('Termine.html')
                ->setContentType('text/html')
                ->setBody($emailView->render());

Then I call the mail function like this:

$this->redirect('mail', null, null, array(
            $to,
            $subject,
            $email_prefix,
            $msg,
            $email_suffix,
            $attachment));

My questions:

  1. How can I successfully create an object of type Swift_Attachment in a controller of my extbase extension (without creating a MailMessage object beforehand and creating the attachment inside of it)?
  2. What should I put after @param as the type of my $attachment variable in my mail function for this to work?

-- EDIT --

Ok, so apparently no one does that because it's not meant to be.
I now used Rene's approach combining it with Dimitri's scalable answer for multiple attachments. My @param is now array because I have to create the actual attachment after instantiating MailMessage - thanks!


Solution

  • In my extension for 6.2.25 ist works without any including:

    $email->attach(\Swift_Attachment::newInstance(
      $emailView->render(), 
      'Termine.html', 
      'text/html'
    ));
    

    So you should check why your autoload of classes don't work. Have you tried to clear the cache complete?

    To your second question: the correct param declaration should be:

    @param \Swift_Mime_Attachment $attachment
    

    But I wouldn't make an redirect, but an $this->forward. You don't need an redirection on client side for this. If this action is only called by an other action I also recommend to make it an protected function an call it directly from your action:

    $this->sendMail($to, $subject, $email_prefix, $msg, $email_suffix, $attachment)
    

    -- EDIT --

    I recommend to use bypass the attachment information to the function to create the attachment object after the SwitftMailer was initialized:

    /**
     * 
     * @param string $to
     * @param string $subject
     * @param string $email_prefix
     * @param string $msg
     * @param string $email_suffix
     * @param array  $attachment
     */
    public function mailAction($to, $subject, $email_prefix, $msg, $email_suffix, $attachment = null) {
      ...
      if (is_array($attachment) && array_key_exist('content', $attachment) && array_key_exist('filename', $attachment) && array_key_exist('mime', $attachment)) {
        $mail->attach(\Swift_Attachment::newInstance($attachment['content'], $attachment['filename'], $attachment['mime']));
      }
      ...
    }