blog.Resource

Archive:

News-Feeds:


RSS 2.0
RSS 0.91
RDF
ATOM 0.3
February 23, 2011

New mail API for the core

Category: Jigal van Hemert, Core

TYPO3 4.5 comes with the Swift Mailer library as the new mail API. It's quite easy to use it in extensions.

The "Swift Mailer" ( swiftmailer.org ) API is integrated in the TYPO3 core with version 4.5. The advantages of this library are:

  • it has an object oriented API
  • it is used by FLOW3, so you get used to using the future API
  • it generates RFC compliant mails
  • encoding, character sets, etc. in body, attachments and headers are automatically handled
  • transport mechanisms such as PHP mail(), sendmail, SMTP (and our own local mbox file for testing) are available
  • the transport mechanism can be configured in the Install Tool and is used system wide

Practical samples

The best way to explain is to show a few examples

Regular syntax

$mail = t3lib_div::makeInstance('t3lib_mail_Message');
$mail->setFrom(array($email => $name));
$mail->setTo(array($email => $name));
$mail->setSubject($subject);
$mail->setBody($body);
$mail->send();

Concatenated syntax

Because most methods return the message object the calls can also be concatenated:

$mail = t3lib_div::makeInstance('t3lib_mail_Message');
$mail->setFrom(array($email => $name))
->setTo(array($email => $name))
->setSubject($subject)
->setBody($body)
->send();

Extra options

 

The class t3lib_mail_Message extends the class Swift_Message, so all the magic of Swift Mailer is available in the t3lib_mail_Message. One of the things added by t3lib_mail_Message is the send() method. This takes care of all the 'transport' and 'mailer' magic. It will use the settings from the Install Tool, so all mails from TYPO3 will use the same transport method.

 

HTML and plain text mails

It's easy to add alternative body parts. Swift Mailer will add the appropriate boundaries and MIME-part headers:

$mail->setBody($bodyHtml, 'text/html');
$mail->addPart($bodyText, 'text/plain');

Attachments

Again Swift Mailer will take care of the encoding, MIME part headers, etc.

$mail->attach(Swift_Attachment::fromPath($theFile));

Want to set the filename of the attachment?

$mail->attach(Swift_Attachment::fromPath($theFile)->setFilename($theName));

Inline images

If you want to embed images in HTML text Swift Mailer can help you too

$cid = $mail->embed(Swift_Image::fromPath($theFile);

The $cid contains the id you can use in the HTML content in the src attribute of the img tag.

Legacy code

Existing extensions will use various mail functions in the current TYPO3 API. To make sure that all mails sent from the TYPO3 installation use the new transport mechanisms an adapter is included which catches all calls to the 'old' functions and forwards them to the Swift Mailer functions. This adapter is not 100% perfect, so in case of problems you can disable this adapter:

$TYPO3_CONF_VARS['MAIL']['substituteOldMailAPI'] = 0

comments

No comments yet. Be the first to comment on this!

Sorry, comments are closed for this post.