1# -*- coding: UTF-8 -*-
2
3__revision__ = '$Id$'
4
5# Copyright (c) 2005-2007 Vasco Nunes
6
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU Library General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software
19# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
20
21# You may use and distribute this software under the terms of the
22# GNU General Public License, version 2 or later
23
24import socket
25import smtplib
26import gutils
27
28socket.setdefaulttimeout(10)
29
30def mailto(self, tls, port, server, auth, user, password, sender, to, subject, msg):
31    """
32    sends an email
33    """
34    try:
35        session = smtplib.SMTP(server, port)
36        session.set_debuglevel(1)
37    except socket.timeout:
38        gutils.error(_("Connection timed out"), \
39            self.widgets['window'])
40        return()
41    if tls == True:
42        session.ehlo()
43        session.starttls()
44        session.ehlo()
45    if auth:
46        try:
47            session.login(user, password)
48        except:
49            gutils.error(_("Error sending e-mail: %s")%_("login failure"), \
50                self.widgets['window'])
51            return
52    headers = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" \
53        % (sender, to, subject)
54    try:
55        smtpresult = session.sendmail(sender, to, headers+msg)
56        gutils.info(_("E-mail sent successfully"), self.widgets['window'])
57        return
58    except:
59        gutils.error(_("Error sending e-mail: %s")%"", self.widgets['window'])
60
61    session.quit()
62
63def send_email(self):
64    if len(self.person_email):
65        if self.config.get('use_auth', False, section='mail') == True:
66            use_auth = 1
67        else:
68            use_auth = 0
69        try:
70            mailto(self, self.config.get('mail_use_tls', False, section='mail'), \
71                self.config.get('mail_smtp_port', '25', section='mail'), \
72                self.config.get('smtp_server', 'localhost', section='mail'), \
73                use_auth, self.config.get('username', '', section='mail'), \
74                self.config.get('password', '', section='mail'), \
75                self.config.get('email', 'griffith', section='mail'), self.person_email, \
76                _("Movie loan reminder"), _("Hi, %s!\n\nJust to remind you that I'm really needing the following movie I have loaned to you recently:\n\n%s (%s)\n\nLoaned on %s") \
77                %(self.person_name, self.widgets['movie']['o_title'].get_text(), \
78                self.widgets['movie']['title'].get_text(), self.loan_date[:10]))
79        except:
80            gutils.error(_("Mail could not be sent. Please check e-mail preferences."), self.widgets['window'])
81    else:
82        gutils.info(_("This person has no e-mail address defined."), \
83            self.widgets['window'])
84