The "Swift Mailer" ( swiftmailer.org ) API is integrated in the TYPO3 core with version 4.5. The advantages of this library are:
The best way to explain is to show a few examples
$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();
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();
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.
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');
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));
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.
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
No comments yet. Be the first to comment on this!