[TYPO3-v4] Swift Mailer is coming for v4.5...

Ernesto Baschny [cron IT] ernst at cron-it.de
Fri Aug 27 15:27:52 CEST 2010


Reinhard Führicht schrieb am 27.08.2010 14:39:

>> here is my concept so far, I would like some comments on that before I
>> start implementing it:
>>
>> http://forge.typo3.org/projects/typo3v45-projects/wiki/SwiftMailer
>>
>> This will turn out to be a whole new API for sending mails using TYPO3
>> (starting at v4.5).
>>
>> There is still more functionality left in t3lib_htmlmail which should be
>> migrated to other utility classes later, but this is not the scope yet
>> of this new API.
>>
>> I would love to hear from extension developers that are using
>> t3lib_htmlmail currently, on what else we would need on that area of
>> generating and sending emails.
>>
>> Cheers,
>> Ernesto
> 
> Hi Ernesto!
> 
> Sounds promising.
> I tried to set up an interface for a Mailer in Formhandler.
> Here is the result. Maybe it helps you.
> 
> interface Tx_Formhandler_MailerInterface {
>     
>     public function send($recipient);
>     
>     public function setHTML($html);
>     public function setPlain($plain);
>     
>     public function setSubject($value);
>     public function setSender($email, $name);
>     public function setReplyTo($email, $name);
>     public function addCc($email, $name);
>     public function addBcc($email, $name);
>     public function setReturnPath($value);
>     
>     public function addHeader($value);
>     public function addAttachment($value);
>     
>     
>     public function getHTML();
>     public function getPlain();
>     
>     public function getSubject();
>     public function getSender();
>     public function getReplyTo();
>     public function getCc();
>     public function getBcc();
>     public function getReturnPath();
>     
> }
> 
> I currently use this as a wrapper for t3lib_htmlmail and once started
> writing a wrapper for SwiftMailer too. I you are interested, I could
> search for this wrapper in my archives.

Thanks for the feedback and help.

I would like to try to stick as close as possible to the SwiftMailer
interfaces, so that we don't have to change that much, instead of
orienting myself on the old t3lib_htmlmail. And also having the benefit
of providing a "well known API" that people from other projects might be
aware of, and also providing a smooth path to transition to FLOW3, which
will have a very similar Interface.

Here are the way-to-do-it in SwiftMail, as a comparison:


>     public function send($recipient);

public function send();

No parameters. $recipients are set beforehand in the $message with the
corresponding methods.


>     public function setHTML($html);
>     public function setPlain($plain);

$message->setBody('My <em>amazing</em> body', 'text/html');
$message->addPart('My amazing body in plain text', 'text/plain');
...

MIME-type's at will. See http://swiftmailer.org/docs/message-body


>     public function setSubject($value);

$message->setSubject($subject);


>     public function setSender($email, $name);
>     public function setReturnPath($value);

$message->setFrom(array('some at address.tld' => 'The Name'));
 => adds From: header

$message->setSender('your at address.tld');
 => adds Sender: xx and Return-Path: unless Return-Path explicitly set

$message->setReturnPath('bounces at address.tld');


>     public function setReplyTo($email, $name);

$message->addReplyTo($address, $name);


>     public function addCc($email, $name);
>     public function addBcc($email, $name);

$message->setTo(array('some at address.tld', 'other at address.tld'));
$message->setCc(array('some at address.tld', 'other at address.tld'));
$message->setBcc(array('some at address.tld', 'other at address.tld'));

The array allows multiple emails and also displayName information. See
http://swiftmailer.org/docs/recipients-to. You have setTo, setCc, setBcc
and corresponding addTo, addCc and addBcc.


>     public function addHeader($value);

This is very "low level" and requires the client of the API to handle
all the encoding of the header himself. There are several header types
possible, see http://swiftmailer.org/docs/header-types.

So either we have different methods by type:

$message->addTextHeader($name, $value);
$message->addDateHeader($name, $value);
$message->addMailboxHeader($name, array($email => $name));

etc.. I think a simple $message->addTextHeader($name, $value) helper
method is enough in our main interface, because this is the most common
use case. Other headers can be added in the SwiftMailer way:

$message->getHeaders()->addTextHeader($name, $value);


>     public function addAttachment($value);

Planned are:

$message->attachFromFile($filename, $mimeType);
$message->attachData($data, $filename, $mimeType);
$message->embedFromFile($filename, $mimeType); # returns a cid: url that
can be used inline
$message->embedData($data, $filename, $mimeType); # returns a cid: url
that can be used inline


>     public function getHTML();
>     public function getPlain();

Do you need to get that stuff back? You just set it and its set. :)
Anyway you have of course a $message->getBody(), but to get the
different mime parts, you need to transverse the MIME parts.

What you can do is

	$message->toString();

which will give you the whole email as a string (useful for debugging
before sending the email).


>     public function getSubject();
>     public function getSender();
>     public function getReplyTo();
>     public function getCc();
>     public function getBcc();
>     public function getReturnPath();

ditto in SwiftMailer.

Cheers,
Ernesto


More information about the TYPO3-project-v4 mailing list