使用python在Lotus Notes中发送邮件

时间:2014-09-26 17:32:16

标签: python email lotus-notes

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

from win32com.client import Dispatch
import smtplib

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

有什么建议吗?谢谢!

2 个答案:

答案 0 :(得分:7)

以下是我多年来为此目的使用的一些代码:

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)

答案 1 :(得分:0)

如果您的公司设置为主机IP地址和端口足以发送电子邮件,请使用以下命令:

import smtplib
from email.mime.base import MIMEBase
from email import encoders
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

def emailReport(attach_path,file_name,From,to,cc=None):
    msg = MIMEMultipart()
    msg['Subject'] = file_name
    msg['From'] = From
    msg['To'] = ", ".join(to) 
    msg['CC'] = ", ".join(cc)
    msg.attach(MIMEText("****Body of your email****\n"))

    #For multiple attachments repeat/loop  the following:
    part = MIMEBase('application', "octet-stream")
    part.set_payload(open(attach_path.format(file_name), "rb").read())
    encoders.encode_base64(part)
    part.add_header('Content-Disposition', 'attachment; filename=%s' % file_name)
    msg.attach(part)
    #Repeat stops here....

    s = smtplib.SMTP(host='xxx.xxx.xx.x',port=xx) #Enter your IP and port here
    s.sendmail(From,to+cc,msg.as_string())
    s.quit()



to=['Person1@email.com', 'Person2@email.com']
cc=['Person3@email.com', 'Person4@email.com']
From='Your@email.com'
attach_path=r'C:\Users\Desktop\Temp'
file_name='Test.xlsx'

emailReport(attach_path,file_name,From,to,cc)