~neobis/poweremail/poweremail61

« back to all changes in this revision

Viewing changes to core.py

  • Committer: Sharoon Thomas
  • Date: 2011-09-21 21:58:40 UTC
  • Revision ID: git-v1:afce9524bd29bd5c628597cfb723f991b17715ac
Refactored _get_outgoing_server method and fixes #1

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
import email
27
27
import time
28
28
import datetime
 
29
import warnings
29
30
from email import Encoders
30
31
from email.mime.base import MIMEBase
31
32
from email.mime.multipart import MIMEMultipart
215
216
                          }
216
217
                }
217
218
 
218
 
    def _get_outgoing_server(self, cursor, user, ids, context=None):
219
 
        """
220
 
        Returns the Out Going Connection (SMTP) object
221
 
        
222
 
        @attention: DO NOT USE except_osv IN THIS METHOD
223
 
        @param cursor: Database Cursor
224
 
        @param user: ID of current user
225
 
        @param ids: ID/list of ids of current object for
226
 
                    which connection is required
227
 
                    First ID will be chosen from lists
228
 
        @param context: Context
229
 
        
230
 
        @return: SMTP server object or Exception
231
 
        """
232
 
        #Type cast ids to integer
233
 
        if type(ids) == list:
234
 
            ids = ids[0]
235
 
        this_object = self.browse(cursor, user, ids, context)
236
 
        if this_object:
237
 
            if this_object.smtpserver and this_object.smtpport:
238
 
                try:
239
 
                    if this_object.smtpssl:
240
 
                        serv = smtplib.SMTP_SSL(this_object.smtpserver, this_object.smtpport)
241
 
                    else:
242
 
                        serv = smtplib.SMTP(this_object.smtpserver, this_object.smtpport)
243
 
                    if this_object.smtptls:
244
 
                        serv.ehlo()
245
 
                        serv.starttls()
246
 
                        serv.ehlo()
247
 
                except Exception, error:
248
 
                    raise error
249
 
                try:
250
 
                    if serv.has_extn('AUTH') or this_object.smtpuname or this_object.smtppass:
251
 
                        serv.login(this_object.smtpuname, this_object.smtppass)
252
 
                except Exception, error:
253
 
                    raise error
254
 
                return serv
 
219
    def _get_outgoing_server(self, cursor, user, account_id, context=None):
 
220
        """Returns the Out Going Connection (SMTP) object
 
221
        
 
222
        .. attention: 
 
223
            DO NOT USE except_osv IN THIS METHOD
 
224
 
 
225
        :param cursor: Database Cursor
 
226
        :param user: ID of current user
 
227
        :param account_id: ID of email account
 
228
        :param context: Context
 
229
        
 
230
        :return: SMTP server object
 
231
        """
 
232
        if type(account_id) == list:
 
233
            warnings.warn(
 
234
                "Support for passing list of ids to _get_outgoing_server "
 
235
                "will be deprecated in 0.8", DeprecationWarning
 
236
            )
 
237
            account_id = account_id[0]
 
238
 
 
239
        account = self.browse(cursor, user, account_id, context)
 
240
 
 
241
        if not account:
 
242
            raise Exception(_("Connection for the given ID does not exist"))
 
243
        if not (account.smtpserver and account.smtpport):
255
244
            raise Exception(_("SMTP SERVER or PORT not specified"))
256
 
        raise Exception(_("Core connection for the given ID does not exist"))
 
245
 
 
246
        if account.smtpssl:
 
247
            server = smtplib.SMTP_SSL(account.smtpserver, account.smtpport)
 
248
        else:
 
249
            server = smtplib.SMTP(account.smtpserver, account.smtpport)
 
250
 
 
251
        if account.smtptls:
 
252
            server.ehlo()
 
253
            server.starttls()
 
254
            server.ehlo()
 
255
 
 
256
        if server.has_extn('AUTH') or account.smtpuname or account.smtppass:
 
257
            server.login(
 
258
                account.smtpuname.encode('UTF-8'),
 
259
                account.smtppass.encode('UTF-8')
 
260
            )
 
261
 
 
262
        return server
257
263
 
258
264
    def check_outgoing_connection(self, cursor, user, ids, context=None):
259
265
        """