Question

Should I not put the return from this method below in finally ? Pylint gives error for this saying: 3: return statement in finally block may swallow exception (lost-exception)

def sendMessage(self, subject, msgContent, files, mailto):
    """ Send the email message

        Args:
            subject(string): subject for the email
            msgContent(string): email message Content
            files(List): list of files to be attached
            mailto(string): email address to be sent to
    """

    msg = self.prepareMail(subject, msgContent, files, mailto)

    # connect to server and send email
    server=smtplib.SMTP(self.smtpserver, port=self.EMAIL_PORT)
    server.ehlo()
    # use encrypted SSL mode
    server.starttls()
    # to make starttls work
    server.ehlo()
    server.login(self.usrname, self.password)
    server.set_debuglevel(self.debug)
    try:
        failed = server.sendmail(self.mailFrom, mailto, msg.as_string())
    except Exception as er:
        print er
    finally:
        server.quit()
        if failed:
            return False
        return True
Was it helpful?

Solution

alright, I fixed the problem, @Nabla pointed the right!!

def sendMessage(self, subject, msgContent, files, mailto):
    """ Send the email message

        Args:
            subject(string): subject for the email
            msgContent(string): email message Content
            files(List): list of files to be attached
            mailto(string): email address to be sent to
    """

    msg = self.prepareMail(subject, msgContent, files, mailto)

    # connect to server and send email
    server = smtplib.SMTP(self.smtpserver, port=self.EMAIL_PORT)
    server.ehlo()
    # use encrypted SSL mode
    server.starttls()
    # to make starttls work
    server.ehlo()
    server.login(self.usrname, self.password)
    server.set_debuglevel(self.debug)
    try:
        server.sendmail(self.mailFrom, mailto, msg.as_string())
    except Exception as er:
        print er
        return False
    finally:
        server.quit()
    return True

OTHER TIPS

This is not a direct answer to your question, but if I may suggest a slightly-different implementation.

Put the connect-to-server and disconnect-from-server in two different methods:

class MyClass():

    serverDict = {
        "gmail"  :"smtp.gmail.com",
        "hotmail":"smtp.live.com",
        "yahoo"  :"smtp.mail.yahoo.com"
    }

    def __init__(self,username,password):
        self.username = username
        self.password = password
        self.serverName = MyClass.serverDict[username[username.index("@")+1:username.index(".")]]

    def sendMessage(self,subject,msgContent,files,mailto):
        server = Connect()
        if not server:
            return False
        failed = True
        try:
            server.login(self.username,self.password)
            if not server.sendmail(self.mailFrom,mailto,msg.as_string()):
                failed = False
        except Exception,error:
            print error
        Disconnect(server)
        return failed

    def Connect():
        try:
            server = smtplib.SMTP(self.serverName)
        except smtplib.SMTPException,error:
            print error
            return None
        try:
            server.ehlo()
            if server.has_extn("starttls"):
                server.starttls()
                server.ehlo()
        except (smtplib.SMTPException,ssl.SSLError),error:
            print error
            Disconnect(server)
            return None
        return server

    def Disconnect(server):
        try:
            server.quit()
        except smtplib.SMTPException,error:
            print error
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top