source: sandbox/2.2.0.2/services/class.mail.php @ 4445

Revision 4445, 4.3 KB checked in by airton, 13 years ago (diff)

Ticket #1908 - Implementacao de melhorias na conta compartilhada - Adicao de arquivos e bibliotecas necessarias

  • Property svn:executable set to *
Line 
1<?php
2
3if( !function_exists( 'zend_include' ) )
4{
5    function zend_include( $path )
6    {
7        set_include_path( LIBRARY );
8        require_once( $path );
9        restore_include_path();
10    }
11}
12
13
14zend_include( 'Zend/Mail.php' );
15
16class MailService extends Zend_Mail /*implements ConfigurationService, HookableService*/
17{
18    static $configuration = array( 'host' => '', 'port' => '', 'username' => '', 'password' => '' );
19
20    public function __construct( $config = null)
21    {
22        if( !$config )
23        {
24            require_once ( ROOT.'/header.inc.php' );
25
26            $boemailadmin = CreateObject('emailadmin.bo');
27            $emailadmin = $boemailadmin->getProfileList();
28            $emailadmin = $boemailadmin->getProfile($emailadmin[0]['profileID']);
29               
30            self::$configuration['host'] = $emailadmin['smtpServer'];
31            self::$configuration['port'] = $emailadmin['smtpPort'];
32
33//          self::$configuration['username'] = $emailadmin['user'];
34//          self::$configuration['password'] = $emailadmin['pass'];
35//          self::$configuration['name'] = $emailadmin['name'];
36//          self::$configuration['type'] = 'SMTP';
37
38//          array_merge( $configuration, array( 'auth' => 'login' ) );
39
40            zend_include( 'Zend/Mail/Transport/Smtp.php' );
41            zend_include( 'Zend/Mail/Part.php' );
42            set_include_path(LIBRARY);
43            parent::setDefaultTransport( new Zend_Mail_Transport_Smtp( self::$configuration['host'], self::$configuration ) );
44            restore_include_path();
45        }
46
47        else
48            $this->configure( $config );
49    }
50
51    public function send( $to, $from, $subject, $body, $bcc = null, $cc = null, $isHTML = true )
52    {
53        set_include_path( LIBRARY );
54        if( $body )
55        {
56            if( $isHTML )
57                parent::setBodyHtml( $body );
58
59            else
60                parent::setBodyText( $body );
61        }
62
63        if( $subject )
64            parent::setSubject( $subject );
65
66        if( !$from )
67            $from = $GLOBALS['phpgw']->preferences->values['email'];
68
69        parent::setFrom( $from, self::getNameByMail( $from ) );
70
71//pra nao ter que fazer os ifs acima...
72        $destTypes = array( 'to', 'bcc', 'cc' );
73
74        foreach( $destTypes as $destination )
75        {
76            $dest = $$destination;
77       
78            if( $dest )
79            {
80                if( !is_array( $dest ) )
81                    $dest = array( $dest );
82
83                foreach( $dest as $d )
84                {
85                    $addDestination = 'add'.ucfirst($destination);
86
87                    parent::$addDestination( $d, self::getNameByMail( $d ) );
88                }
89            }
90        }
91       
92        parent::send();
93        restore_include_path();
94    }
95
96    public function configure( $config )
97    {
98        if( !$config ) return( false );
99
100        foreach( $config as $key => $value )
101        {
102            if( $value && isset( self::$configuration[$key] ) )
103            {
104                self::$configuration[$key] = $value;
105            }
106        }
107    }
108
109    //hook
110    public function getNameByMail( $mail )
111    {
112        return( null );
113    }
114
115    public function getAttachment( $attachment )
116    {
117        return( null );
118    }
119
120     /**
121     * Adds attachment to the mail message
122     *
123     * @param string $file  binary file
124     * @param string $filename file name
125     * @param string $type type example: image/gif
126     * @param Zend_Mime $disposition example: Zend_Mime::DISPOSITION_INLINE
127     * @param Zend_Mime $encoding example: Zend_Mime::ENCODING_BASE64
128     * @return bool
129     */
130    public function  addAttachment($file, $filename, $type, $encoding = null, $disposition = 'DISPOSITION_ATTACHMENT')
131    {
132        $part = new Zend_Mime_Part( $file );
133
134        $part->type = $type;
135        $part->filename = $filename;
136
137       switch ($disposition)
138       {
139            case 'DISPOSITION_INLINE':
140                $part->disposition = Zend_Mime::DISPOSITION_INLINE;
141                break;
142            case 'DISPOSITION_ATTACHMENT':
143                $part->disposition = Zend_Mime::DISPOSITION_ATTACHMENT;
144                break;
145            default: 
146                break;
147        }
148       
149        switch ($encoding)
150        {
151            case 'ENCODING_7BIT':
152                $part->encoding = Zend_Mime::ENCODING_7BIT;
153                break;
154            case 'ENCODING_8BIT:':
155                $part->encoding = Zend_Mime::ENCODING_8BIT;
156                break;
157            case 'ENCODING_QUOTEDPRINTABLE:':
158                $part->encoding = Zend_Mime::ENCODING_QUOTEDPRINTABLE;
159                break;
160            default:
161                $part->encoding = Zend_Mime::ENCODING_BASE64;
162                break;
163        }
164
165        parent::addAttachment( $part );
166
167        return;
168    }
169}
170
171ServiceLocator::register( 'mail', new MailService() );
172
Note: See TracBrowser for help on using the repository browser.