使用python在Lotus Notes中发送邮件 [英] Send mail in Lotus Notes using python

查看:1165
本文介绍了使用python在Lotus Notes中发送邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用python在Lotus Notes中发送邮件的帮助,看来win32com可以做到这一点,但是我没有找到任何完整的示例或教程。我的想法是一个简单的功能:

i need help for send a mail in the Lotus Notes using python, appear that the win32com can do it, but i don't found any complete example or tutorial. My idea is a simple function like it:

from win32com.client import Dispatch
import smtplib

def SendMail(subject, text, user):
    session = Dispatch('Lotus.NotesSession')
    session.Initialize('???')
    db = session.getDatabase("", "")
    db.OpenMail();

有些建议?谢谢!

推荐答案

以下是我为此目的使用了几年的代码:

Below is some code that I have used for this purpose for several years:

from __future__ import division, print_function

import os, uuid
import itertools as it

from win32com.client import DispatchEx
import pywintypes # for exception

def send_mail(subject,body_text,sendto,copyto=None,blindcopyto=None,
              attach=None):
    session = DispatchEx('Lotus.NotesSession')
    session.Initialize('your_password')

    server_name = 'your/server'
    db_name = 'your/database.nsf'

    db = session.getDatabase(server_name, db_name)
    if not db.IsOpen:
        try:
            db.Open()
        except pywintypes.com_error:
            print( 'could not open database: {}'.format(db_name) )

    doc = db.CreateDocument()
    doc.ReplaceItemValue("Form","Memo")
    doc.ReplaceItemValue("Subject",subject)

    # assign random uid because sometimes Lotus Notes tries to reuse the same one
    uid = str(uuid.uuid4().hex)
    doc.ReplaceItemValue('UNIVERSALID',uid)

    # "SendTo" MUST be populated otherwise you get this error: 
    # 'No recipient list for Send operation'
    doc.ReplaceItemValue("SendTo", sendto)

    if copyto is not None:
        doc.ReplaceItemValue("CopyTo", copyto)
    if blindcopyto is not None:
        doc.ReplaceItemValue("BlindCopyTo", blindcopyto)

    # body
    body = doc.CreateRichTextItem("Body")
    body.AppendText(body_text)

    # attachment 
    if attach is not None:
        attachment = doc.CreateRichTextItem("Attachment")
        for att in attach:
            attachment.EmbedObject(1454, "", att, "Attachment")

    # save in `Sent` view; default is False
    doc.SaveMessageOnSend = True
    doc.Send(False)

if __name__ == '__main__':
    subject = "test subject"
    body = "test body"
    sendto = ['abc@def.com',]
    files = ['/path/to/a/file.txt','/path/to/another/file.txt']
    attachment = it.takewhile(lambda x: os.path.exists(x), files)

    send_mail(subject, body, sendto, attach=attachment)

这篇关于使用python在Lotus Notes中发送邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆