# TODO: # ----- # Umlaute in Headern in US-ASCII umwandeln und dringend Verweis auf das Config-File. # nik@nerxs.com 2002-01-03 import smtplib import os import mx.DateTime import time from types import * class MyMail: "A simple Mail class." def __init__(self): self.myheaders = {} self.mybody = '' self.rcpts = [] def insertRCPTS(self, addr): """insertRCPTS(addr): Insert a SMTP-RCPT or a list of SMTP-RCPTS.""" if type(addr) is ListType: self.rcpts.extend(addr) else: self.rcpts.append(addr) def getRCPTS(self): """getRCPTS(): Get the list of SMTP-RCPTS. If this function is called all Rcpts from To, CC and Bcc are collected. Once the getRCPTS() fuction is called the Bcc:-Header is deleted.""" if self.myheaders.has_key("To"): self.rcpts.extend(self.myheaders["To"].split(",")) if self.myheaders.has_key("Cc"): self.rcpts.extend(self.myheaders["Cc"].split(",")) if self.myheaders.has_key("Bcc"): self.rcpts.extend(self.myheaders["Bcc"].split(",")) del self.myheaders["Bcc"] return self.rcpts def insertHeader(self, key, val): """insertHeader('header', 'value' or list): Inserts a Header. If Header is To, Cc or Bcc value or list of values are append to RCPT-List.""" key = self.normalizeHeader(key) if type(val) is ListType: valstr = "" for x in val: if valstr: valstr = valstr + ", " + x else: valstr = x else: valstr = val if key == "To": self.myheaders["To"] = valstr elif key == "Cc": self.myheaders["Cc"] = valstr elif key == "Bcc": self.myheaders["Bcc"] = valstr else: self.myheaders[key] = val def getHeaders(self, debug = "send"): """getHeaders([debug]): Gets all headers. If debug = 'nosend' Bcc:-Header is given too. Default is debug = 'send', so the Bcc:-Header is not seen in Mail. If your programm calls getRCPTS() the Bcc:-Header is deleted. Some headers if they are not given like Date or Message-ID are generated at the time your message is send. If you want to see _all_ headers call getMsg(debug='nosend').""" header = "" for x in self.myheaders.keys(): if x == "Bcc" and debug == "send": pass else: header = header + "%s: %s\r\n"%(x,self.myheaders[x]) return header def getFrom(self): """getFrom(): Tells you the From:-Header.""" if self.myheaders.has_key("From"): return self.myheaders["From"] def getMsg(self,debug="send"): """getMesg([debug]): Generates missing headers like Date, Message-ID and Content-Type. The default Content-Type is 'text/plain; charset=ISO-8859-1', 'MIME-Version: 1.0' and 'Content-Transfer-Encoding: 8bit'. If you need something else use the insertHeader('Header', 'Value') function. If debug = 'nosend' all headers including Bcc: are given. Default is 'send' without Bcc:.""" if not self.myheaders.has_key("Date"): date = mx.DateTime.ARPA.str(mx.DateTime.localtime()) self.myheaders["Date"] = date if not self.myheaders.has_key("Content-Type"): self.myheaders["Content-Type"] = "text/plain; charset=ISO-8859-1" self.myheaders["MIME-Version"] = "1.0" self.myheaders["Content-Transfer-Encoding"] = "8bit" if not self.myheaders.has_key("Message-ID"): timestamp = str(time.time()) pid = os.getpid() self.myheaders["Message-ID"] = "<" + str(pid) + "-" + timestamp + "@nyl.mail>" mymessage = self.getHeaders(debug) mymessage = mymessage + "\r\n" + self.mybody.replace('\n', '\r\n') + "\r\n\r\n" return mymessage def normalizeHeader(self, key): """normalizeHeader(key): This function capitalizes all words in the first colon and strips an ending ":" if exists.""" header = key.split("-") h = [] for x in header: x = x.capitalize() if x == "Mime": x = "MIME" h.append(x) header = "-".join(h) if header[-1] == ":": header = header[:-1] return header def insertBody(self, text): """insertBody(text): Puts the bodytext in your message.""" self.mybody = text def getBody(self): """returnBody(): Returns the bodytext of your message.""" return self.mybody def sendSMTP(self, host="localhost"): """sendSMTP(): Makes a SMTP-Connection using smtplib to a given host and sends your mail. By calling this function without hostname localhost will be used.""" try: self.setdiffHeaders() except: pass toaddrs = self.getRCPTS() sender = self.getFrom() msg = self.getMsg() #print "RCPT %s\nFROM %s\n\nBODY\n%s"%(toaddrs, sender, msg) server = smtplib.SMTP(host) server.set_debuglevel(0) server.sendmail(sender, toaddrs, msg) server.quit() print "\nnylmail: Mail send successfully."