Switch to side-by-side view

--- a/ForgeMail/forgemail/open_relay.py
+++ b/ForgeMail/forgemail/open_relay.py
@@ -13,19 +13,45 @@
     parser.add_option('-t', '--tls', dest='tls', action='store_true', default=False)
     parser.add_option('-l', '--login', dest='login', action='store_true', default=False)
     (options, args) = parser.parse_args()
-    if options.ssl:
-        smtp_client = smtplib.SMTP_SSL(options.host, int(options.port))
-    else:
-        smtp_client = smtplib.SMTP(options.host, int(options.port))
-    if options.tls:
-        smtp_client.starttls()
     if options.login:
         username = raw_input('Username:')
         password = getpass('Password:')
-        smtp_client.login(username, password)
+    else:
+        username = password = None
+    smtp_client = MailClient(options.host,
+                             int(options.port),
+                             options.ssl,
+                             options.tls,
+                             username, password)
     server = MailServer(('0.0.0.0', 8826), None,
                         smtp_client=smtp_client)
     asyncore.loop()
+
+class MailClient(object):
+
+    def __init__(self, host, port, ssl, tls, username, password):
+        self.host, self.port, self.ssl, self.tls, self.username, self.password = \
+            host, port, ssl, tls, username, password
+        self._client = None
+        self._connect()
+
+    def sendmail(self, mailfrom, rcpttos, data):
+        print 'Sending mail to %s' % rcpttos
+        try:
+            self._client.sendmail(mailfrom, rcpttos, data)
+        except:
+            self._connect()
+            self._client.sendmail(mailfrom, rcpttos, data)
+
+    def _connect(self):
+        if self.ssl:
+            self._client = smtplib.SMTP_SSL(self.host, int(self.port))
+        else:
+            self._client = smtplib.SMTP(self.host, int(self.port))
+        if self.tls:
+            self._client.starttls()
+        if self.username:
+            self._client.login(self.username, self.password)
 
 class MailServer(smtpd.SMTPServer):