the secure PHP framework


Using Swiftmailer with Composer and Autoload

Joe Schmoe
16 March 2017, 17:08
The server I'm developing on doesn't have access to the sendmail command so I need to send mail through SMTP. I would like to use the Swift Mailer library.

I downloaded composer and then ran the following command from the top level directory:

$ php composer.phar require swiftmailer/swiftmailer @stable

It updated the composer.json file and installed the library in the libraries/thirdparty directory.

I checked and it looks like Swift Mailer is included (lib/swift_required.php as shown in the example implementation page below) in two places:


Example implementation page:

All good so far! (as far as I can tell)

Now I am stuck on how to access the library in a model file (specifically I am modifying the models/contact.php to send an email).

Per the example above I reference the library as such:

$message = Swift_Message::newInstance();

However, I get an error message saying

Banshee exception
Class 'Swift_Message' not found
line 28 in /models/contact.php.

Sorry, I am pretty new to Composer so any help you can provide would be much appreciated.


Joe Schmoe
16 March 2017, 17:25
Just checked the available classes by using the following:


Looks like the Swift Mailer library did not get loaded.

Updated the code to explicitly include the library:


Success! So I guess Autoload doesn't work like I thought it did. Any insight you could provide would be helpful.

2 July 2017, 08:09
Thanks for posting this. Getting email working is a pain but with your help, I was able to install Composer and Swiftmailer. I actually want to use SMTP because I think I am too green to setup my own mailserver.

I was able to install Nullmailer as a drop-in Sendmail replacement and can receive emails on my email client. I think it is possible (hopefully not too hard) to use Swiftmail as the transport agent for Nullmailer. From what you can see, do you think I am all wet?
3 July 2017, 00:08
I think I was dreaming when I thought I could drop the following line in the right file and have it work. $transport = new Swift_SendmailTransport('/usr/sbin/exim -bs');

I was hoping it could be
Joe Schmoe
4 July 2017, 17:05
I think it should be that easy but I was using localhost and port 25 as the transport since I didn't have access to the sendmail command.