seSwiftMailerPlugin released

Posted by benni on 2009-01-28 in mail, php, plugins, symfony

After a while of developing plugins for internal use I finally got around to releasing one to the public. I have a few in stash that might be ready to be released in the future, so stay tuned.

This plugin helps speed up sending of mails from symfony. It should be compatible with all versions(1.0,1.1,1.2).I extended the swift library, which is an extremely powerful tool to sending mails in php, but for my taste not easy enough to use you have to deal with different classes and lots of configuration options, not necessarily a bad thing but for sending mails fast it can be done easier. My intention was not to wrap all of swifts features, but rather port those most useful for me at the moment. Nevertheless can it be easily extended, just take a look at the class files in the lib folder. You can use normal symfony templates as body, photos are automatically detected and attached to the message, mailing lists attachments are also supported.

Here are some examples of its usage:

We will use gmail as it is the most easy to set up, feel free to try out the other connection methods:

First you create the email message:

$mail=new GMail('Mail Title','senderEmail','password');

you can set the body text as txt or html or both(recommended)

 $mail->setBodyTxt('BodyText ....');
 $mail->setBodyHtml($this->getPartial('global/mail_body'),array('name'=>'Somename'));

note that the htmlBody can be a partial and that photos will automatically embedded. For sending we have the mail to one recipient we can simply

$mail->sendTo('mail@domain.com');

or if we wish to specify the name of the recipient

$mail->sendTo('mail@domain.com');

we can also add Ccs and Bccs by using the mail method

$mail->mail('toMail@domain.com','ccMail@domain.com','bccMail3@domain.com');

The sendTo and mail methods can also take arrays of email addresses or name address pairs to send to multiple recipients. Note that in that case all Tos and Ccs can obviously see each others addresses. In order to send a lot of mails to multiple recipients without showing all addresses, we could use a loop, but that is not a very performance wise. Luckily the swift library comes with batch mail functionality. In this plugin it has been incorporated in the mass mail function:

$mailingList=array(array('name1','mail1@domain.com'),'mail2@domain.com',array('name3','mail3@domain.com'));
$mail->massMail($mailingList);

Its as easy as that. Note that GMail has a limit of 50 mail per batch, so the GMail adaptor will through an exception if the mailingList is longer than that.

So thats it for a small overview. By the way this is the first version of the plugin so please let me know if there are any bugs or missing features etc.

Leave a reply