Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
SwiftMailer package
  • Hi all, In one of my project, I had (and wanted) to handle emails easily. I know that there is an Email class in fuel core but I read on the forum that it should have some licenses issues for the final release ( and on the same thread some of you talked about SwiftMailer. So I created a small package for it. You can find it here: (it's the first time I use Github so I hope I did it in the correct way) For the moment it's a really basic interface for SwiftMailer and you have to "hardcode" like it's written in the documentation. For example, to send an email:
    public function action_sendmail() {
     //Create the Transport
     $transport = Swift_MailTransport::newInstance();
     //Create the Mailer using your created Transport
     $mailer = Swift_Mailer::newInstance($transport);
     //Create a message
     $message = Swift_Message::newInstance('Wonderful Subject')
       ->setFrom(array('' => 'John Doe'))
       ->setBody('Here is the message itself');
     //Send the message
     $result = $mailer->send($message);

    For the moment I only tried to send basic emails using MailTransport and SmtpTransport and it worked. I still have to run some tests with attachment files to be sure that there is no issue with that. The next step will be to use the config file (config/swift.php) to create some shortcuts like:
    public function action_sendmail() {
     Swift::factory('default') // load the default config that set up protocol, encoding, recipient, etc.
      ->setSubject('Wonderful Subject')
      ->setBody('Here is the message itself')

    I hope it can help other people as well and if you try it, all comments are welcome. Thanks,
  • I've found this very useful - thanks. The "setBody" method will set just one body for an email, which will be a text format by default. Generally I like to send text and html multipart messages. The following code, used to create the message, will allow you to add both a html and a text part:
    $message = Swift_Message::newInstance('Wonderful Subject') 
        ->setFrom(array('' => 'System')) 
        ->setTo(array('' => 'John Doe')) 
        ->attach(new Swift_MimePart($email_body_text))
        ->attach(new Swift_MimePart($email_body_html, 'text/html')) ;

    The two parts can be generated easily from templates:
        $email_body_text = View::forge('path/to/emailtemplatetext');
        $email_body_html = View::forge('path/to/emailtemplatehtml');

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion