SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict GeertDD's Avatar
    Join Date
    Feb 2005
    Location
    Belgium
    Posts
    334
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    A few questions about Swift Mailer

    I like Swift Mailer but I'm wondering whether my code couldn't be just a bit cleaner.

    Is there a way to set a default smtp server, or autoconnect? Or do I need to specify an smtp server every time I'm sending a mail, which means that I would have to edit multiple files if I want to change it, not very portable.

    Also, isn't there an auto close feature, just like auto flush?

    PHP Code:
    // current code
    $mail = new Swift(new Swift_Connection_SMTP('smtp.example.com'));
    $mail->send($recipient$sender$subject$body);
    $mail->close(); 
    PHP Code:
    // cleaner?
    $mail = new Swift;
    $mail->send($recipient$sender$subject$body); 

  2. #2
    Resident Code Monkey Chris Corbyn's Avatar
    Join Date
    Nov 2005
    Location
    Melbourne, Australia
    Posts
    713
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to set one every time but you can proxy the requests. For example, in a project at work we have it set up like this:

    PHP Code:
    <?php

    require_once 'sys/classes/3rd-party/swift/Swift.php';
    require_once 
    'sys/classes/3rd-party/swift/Swift/Connection/Sendmail.php';
    require_once 
    'sys/classes/3rd-party/swift/Swift/Connection/SMTP.php';

    /**
     * Just a stub/wrapper to allow Swift to be used like a SHANE class
     */
    class MailOut extends Swift
    {
        public function 
    __construct($type='sendmail')
        {
            switch (
    $type)
            {
                case 
    'smtp':
                
    parent::__construct(new Swift_Connection_SMTP(SMTP_HOSTSMTP_PORT));
                break;
                case 
    'sendmail':
                default:
                
    parent::__construct(new Swift_Connection_Sendmail(SENDMAIL_PATH.' -bs'));
            }
        }
        
        public function 
    __destruct()
        {
            
    $this->close();
        }
    }

    ?>
    So to send an email we just do:

    PHP Code:
    $mail = new MailOut;
    $mail->send( ... ); 
    No need to even call close().

    EDIT | If you're using PHP5, close() is called automatically from the destructor so there's no need to call it manually. PHP4 sucks in that respect.

  3. #3
    SitePoint Addict GeertDD's Avatar
    Join Date
    Feb 2005
    Location
    Belgium
    Posts
    334
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, Chris. Though I'm not a pro OOP programmer yet, I actually understand your workaround (except for the use of Sendmail.php and the word 'Shane').

    Swift Mailer seems like a very well coded class for me to start learning more about OOP, especially in php5.

  4. #4
    Resident Code Monkey Chris Corbyn's Avatar
    Join Date
    Nov 2005
    Location
    Melbourne, Australia
    Posts
    713
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by GeertDD
    Thanks, Chris. Though I'm not a pro OOP programmer yet, I actually understand your workaround (except for the use of Sendmail.php and the word 'Shane').

    Swift Mailer seems like a very well coded class for me to start learning more about OOP, especially in php5.
    Thanks Sendmail.php is just another of Swift's available connections (it supports SMTP, Sendmail or a combination of the two (multiple times over) either for redundancy or connection cycling).

    The word SHANE... that's my bad for leaving it in the comments. SHANE is an application manager we're building our project around. Nobody else will be using it since we're the ones writing it and tweaking it, although it will likely be released under GPL eventually.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •