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

<pre><code>/**
 * 
 * @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-&gt;setFrom($from)
                -&gt;setTo(array($toEmail))
                -&gt;setSubject($subject)
                -&gt;setBody($htmlBody, 'text/html');
        $mail-&gt;addPart($body, 'text/plain');
        if ($attachment) {
            $mail-&gt;attach($attachment);
        }
        if (empty($toEmail) || strpos($toEmail, '@') === FALSE) {
            $this-&gt;addFlashMessage('Die Mail konnte nicht verschickt werden! Keine Email-Adresse des Empfängers', '', \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
            );
        } else {
            if ($mail-&gt;send()) {
                $this-&gt;addFlashMessage('Die Mail für wurde verschickt!', '');
            } else {
                $this-&gt;addFlashMessage('Die Mail konnte nicht verschickt werden!', '', \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR
                );
            }
        }
        $this-&gt;redirect('list');
    } catch (Exception $e) {
        echo 'Caught exception: ', $e-&gt;getMessage(), "\n";
    }
}
</code></pre>

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

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

<p>Then I call the mail function like this:</p>

<pre><code>$this-&gt;redirect('mail', null, null, array(
            $to,
            $subject,
            $email_prefix,
            $msg,
            $email_suffix,
            $attachment));
</code></pre>

<p>My questions:  </p>

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

<p>-- EDIT --  </p>

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