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 (http://fuelphp.com/forums/topics/view/1880#1884) and on the same thread some of you talked about SwiftMailer. So I created a small package for it. You can find it here: https://github.com/sherblot/fuel-swiftmailer (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.com' => 'John Doe'))
       ->setTo(array('you@domain.com'))
       ->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.
      ->setTo(array('you@domain.com'))
      ->setSubject('Wonderful Subject')
      ->setBody('Here is the message itself')
      ->send();
     }
    }
    

    I hope it can help other people as well and if you try it, all comments are welcome. Thanks,
    Sebastien
  • 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@example.com' => 'System')) 
        ->setTo(array('john.doe@example.com' => '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