class NotesMail(object): def __init__(self, server, file): print('init mail client') self.session = DispatchEx('Notes.NotesSession') self.db = self.session.GetDatabase(server, file) if not self.db.IsOpen: print('open mail db') try: self.db.OPENMAIL except Exception as e: print(str(e)) print('could not open database: {}'.format(db_name)) def send_mail(self,sendto,copyto, blindcopyto,subject, body_text, attach): doc = self.db.CREATEDOCUMENT doc.sendto = sendto if copyto is not None: doc.copyto = copyto if blindcopyto is not None: doc.blindcopyto = blindcopyto doc.Subject = subject # body body = doc.CreateRichTextItem("Body") body.AppendText(body_text) # attachment if attach is not None: attachment = doc.CreateRichTextItem("Attachment") for att in attach: print(att) attachment.EmbedObject(1454, "", att, "Attachment") doc.SaveMessageOnSend = True doc.Send(False) print('send success')
def send_mail(subject, body_text, sendto, copyto=None, blindcopyto=None, attach=None): # Get credentials mailServer = 'Your Server' mailPath = 'Your database' mailPassword = '******' # Connect notesSession = DispatchEx('Lotus.NotesSession') try: notesSession.Initialize(mailPassword) notesDatabase = notesSession.GetDatabase(mailServer, mailPath) except pywintypes.com_error: raise Exception('Cannot access mail using %s on %s' % (mailPath, mailServer)) # print('Title:' + notesDatabase.Title) # Get a list of folders # for view in notesDatabase.Views: # if view.IsFolder: # print('view : ' + view.Name) document = notesDatabase.CreateDocument() document.ReplaceItemValue("Form", "Memo") document.ReplaceItemValue("Subject", subject) # assign random uid because sometimes Lotus Notes tries to reuse the same one uid = str(uuid.uuid4().hex) document.ReplaceItemValue('UNIVERSALID', uid) # "SendTo" MUST be populated otherwise you get this error: # 'No recipient list for Send operation' document.ReplaceItemValue("SendTo", sendto) if copyto is not None: document.ReplaceItemValue("CopyTo", copyto) if blindcopyto is not None: document.ReplaceItemValue("BlindCopyTo", blindcopyto) # body body = document.CreateRichTextItem("Body") body.AppendText(body_text) # attachment if attach is not None: attachment = document.CreateRichTextItem("Attachment") for att in attach: attachment.EmbedObject(1454, "", att, "Attachment") # save in `Sent` view; default is False document.SaveMessageOnSend = True document.Send(False)
def getDatabase(server, filePath, password): # Connect notesSession = DispatchEx('Lotus.NotesSession') try: notesSession.Initialize(password) notesDatabase = notesSession.GetDatabase(server, filePath) if not notesDatabase.IsOpen: try: notesDatabase.Open() except pywintypes.com_error: print('could not open database: {}'.format(db_name)) return notesDatabase except pywintypes.com_error: raise Exception('Cannot access database using %s on %s' % (filePath, server))
def send_mail(server, db_file, reciver_list, subject, body=None): pythoncom.CoInitialize() makepy.GenerateFromTypeLibSpec('Lotus Domino Objects') logger.info('init mail client') session = DispatchEx('Notes.NotesSession') db = session.GetDatabase(server, db_file) if not db.IsOpen: logger.info('open mail db') try: db.OPENMAIL except Exception as e: logger.info('could not open database') logger.info("Send email ing") doc = db.CREATEDOCUMENT doc.sendto = reciver_list doc.Subject = subject if body: doc.Body = body doc.SEND(0, reciver_list) logger.info('send success')
class NotesMail(object): def __init__(self, server, file): print('init mail client') self.session = DispatchEx('Notes.NotesSession') # self.server = self.session.GetEnvironmentString("MailServer", True) #self.server = 'MACEDD25N/SERVERS/CEDD/HKSARG' #self.file = 'C:\\Users\\Desktop\\ppcslo.nsf' self.db = self.session.GetDatabase(server, file) if not self.db.IsOpen: print('open mail db') try: self.db.OPENMAIL except Exception as e: print(str(e)) print('could not open database: {}'.format(self.db)) def send_mail(self, receiver_list, subject, body=None): doc = self.db.CREATEDOCUMENT doc.sendto = receiver_list doc.Subject = subject if body: doc.Body = body doc.SEND(0, receiver_list) print('send success')
# Yield it yield document # Get the next document document = folder.GetNextDocument(document) if __name__ == '__main__': # Get credentials mailServer = 'Your Server' mailPath = 'Your database' mailPassword = '******' # Connect notesSession = DispatchEx('Lotus.NotesSession') try: notesSession.Initialize(mailPassword) notesDatabase = notesSession.GetDatabase(mailServer, mailPath) except pywintypes.com_error: raise Exception('Cannot access mail using %s on %s' % (mailPath, mailServer)) # print('Title:' + notesDatabase.Title) # Get a list of folders # for view in notesDatabase.Views: # if view.IsFolder: # print('view : ' + view.Name) for document in makeDocumentGenerator('($Inbox)'): # Get fields subject = document.GetItemValue('Subject')[0].strip() date = datetime.datetime(
class NotesMail(): """ 发送读取邮件有关的操作 """ def __init__(self, server, file): """Initialize @param server Server's name of Notes @param file Your data file, usually ends with '.nsf' """ self.session = DispatchEx('Notes.NotesSession') self.server = self.session.GetEnvironmentString("MailServer", True) self.db = self.session.GetDatabase(server, file) self.db.OPENMAIL self.myviews = [] def send_mail(self, receiver, subject, body=None): """发送邮件 @param receiver: 收件人 @param subject: 主题 @param body: 内容 """ doc = self.db.CREATEDOCUMENT doc.sendto = receiver doc.Subject = subject if body: doc.Body = body doc.SEND(0, receiver) def get_views(self): for view in self.db.Views: if view.IsFolder: self.myviews.append(view.name) def get_documents(self, view_name): """ @return generator """ documents = [] folder = self.db.GetView(view_name) if not folder: raise Exception('Folder {0} not found. '.format(view_name)) document = folder.GetFirstDocument while document: documents.append(document) document = folder.GetNextDocument(document) return documents def read_mail(self, view, attachment=False): """Read the latest mail @param view The view(fold) to access @param attachment Boolean, whether get attachment @return, dict Info of a mail """ result = {} documents = self.get_documents(view) latest_document = documents[-1:][0] extra_obj = Extract(latest_document) result = extra_obj.extract() if attachment: extra_obj.get_attachment() return result