Sending mails with TYPO3.SwiftMailer

Swift Mailer is a library. It is used within an application to send mails. It does not have a graphical user interface. It cannot be opened in your web browser directly. The library has a component based structure that we can implement in a number of ways.

System Requirements

The basic requirements to operate Swift Mailer are extremely minimal and easily achieved. Historically, Swift Mailer has supported both PHP 4 and PHP 5 by following a parallel development workflow. Now in it's fourth major version, and Swift Mailer operates on servers running PHP 5.2 or higher.

Implementation with TYPO3 Flow

To implement Swift Mailer in TYPO3 Flow, we need to include TYPO3.SwiftMailer package. To include it, specify it in your composer.json file in root directory.

"require": {
"typo3/swiftmailer": "5.3.0"
}


If you want to stand-alone view for your mail template then inject StandaloneView class from TYPO3.Fluid package . Its a API to generate template based emails.

/**
* A standalone template view
*
* @Flow\Inject
* @var \TYPO3\Fluid\View\StandaloneView
*/
protect $standaloneView;


Now in your method specify mail details and call send() method from \TYPO3\SwiftMailer\Message class.

/**
 * Sends your details to recipient
 *
 * @return void
 */
public function sendMail() {
        // set your template path
$templatepath =  'resource://Package.Name/Private/Templates/Emails/MyTemplate.ht ml';
$this->standaloneView->setFormat('html');
$this->standaloneView->setTemplatePathAndFilename($templatepath);
$emailBody = $this->standaloneView->render();

// create instance of \TYPO3\SwiftMailer\Message() and set mail details
$mail = new \TYPO3\SwiftMailer\Message();
$mail->setFrom(array('email' => 'dummy_sender@dummy.com', 'name' => 'dummy'))
->setTo(array('email' => 'dummy_recipient@dummy.com', 'name' => 'dummy'))
->setSubject('Your Subject')

// you can set supported formats like .html .txt .xml etc
->setBody($emailBody, 'text/html')

// you can add attchment for a document
->attach(\Swift_Attachment::fromPath(
'resource://Package.Name/Public/Images/my_image.jpg')
)
->addBcc('dummy_user@dummy.com', 'dummy_name')
->addCc('dummy_user@dummy.com', 'dummy_name')
->send();

}


    Attaching Dynamic Content

    Files that are generated at runtime, such as PDF documents or images can be attached directly to a message without writing them out to disk. Use the standard Swift_Attachment::newInstance() method.

    For example, we will generate pdf file attachment.

    1. Generate pdf data
    2. Create an attachment with Swift_Attachment::newInstance(), specifying the source data of your content along with a name and the content-type.
    3. Add the attachment to the message with attach().


        // Create your file contents in the normal way, but don't write them to disk
    $data = generateMyPdfData();

    // Create the attachment with your data
    $mail = Swift_Attachment::newInstance($data, 'my-file.pdf', 'application/pdf');

    // Attach it to the message
    $mail->attach($attachment);

    Copyright © 2019 Lelesys Informatik GmbH, Deutschland. All Rights Reserved.