Banshee
the secure PHP framework

Forum

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:

composer/autoload_static.php
composer/autoload_files.php

Example implementation page:

http://swiftmailer.org/docs/messages.html

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.

Thanks!

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

print_r(get_declared_classes());

Looks like the Swift Mailer library did not get loaded.

Updated the code to explicitly include the library:

require_once('/libraries/thirdparty/swiftmailer/swiftmailer/lib/swift_required.php');

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

Thanks!
chapchap70
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?
chapchap70
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.

http://swiftmailer.org/docs/sending.html
Message preview

The following BB-codes are available in a message:

  • [b]Bold text[/b]
  • [center]Center text or imagen[/center]
  • [color=color name or #RGB code]Colored text[/color]
  • [i]Italic text[/i]
  • [img]Link to image[/img]
  • [right]Align text or image right[/right]
  • [s]Strike-through text[/s]
  • [size=pixelsize]Big or small text[/size]
  • [u]Underlined text[/u]
  • [url]Link to website[/url]
  • [url=link to website]Link text[/url]