def connect(self, argv): """connect [-s <bindto>] <host> [<port>] Where <host> is the mailhost to connect to, <port> is the TCP port to use (defaults to 25), and <bindto> is the local IP address to source from.""" bindto = None port = 25 optlist, longopts, args = self.getopt(argv, "s:hp:") for opt, val in optlist: if opt == "-s": bindto = val elif opt == "-p": port = int(val) if len(args) < 1: self._print(self.connect.__doc__) return host = args[0] if len(args) > 1: port = int(args[1]) if self._client: self._print("warning: closing existing connection.") self.quit() self._client = SMTP.get_mailer(logfile=self._logfile) try: code, msg = self._client.connect(host, port, bindto) self._print("%s %s" % (code, msg)) self._setprompt(argv[1]) except: self._client = None self._print("connect failed!") ex, val, tb = sys.exc_info() self._print(str(val))
def ezmail(obj, To=None, From=None, subject=None, cc=None, bcc=None, extra_headers=None, mailhost=None): """A generic mailer that sends a multipart-mixed message with attachments. The 'obj' parameter may be a MIME* message, or another type of object that will be converted to text. If it is a list, each element of the list will be attached. """ global CONFIG if isinstance(obj, (AutoMessage, MultipartMessage)): outer = obj else: if type(obj) is list: outer = MultipartMessage() for part in obj: _do_attach(outer, part) elif isinstance(obj, unicode): outer = AutoMessage(obj, charset="utf-8") else: outer = AutoMessage(unicode(obj), charset="utf-8") outer.From(From) if To: outer.To(To) if subject: outer.Subject(subject) if cc: outer.Cc(cc) if bcc: outer.Bcc(bcc) if extra_headers: # a dictionary of header names (keys), and values. for name, value in extra_headers.items(): outer[name] = value mhost = mailhost or CONFIG.get("mailhost", "localhost") if mhost == "localhost": smtp = LocalSender() status = outer.send(smtp) if not status: raise MailError(str(status)) else: from pycopia.inet import SMTP smtp = SMTP.SMTP(mhost, bindto=CONFIG.get("bindto")) errs = outer.send(smtp) smtp.quit() if errs: raise MailError(str(status)) return outer["Message-ID"]