15.04.2013 Views

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

This script launches Outlook, creates a new message, "sends" it, and lets you view it by<br />

opening and displaying both the Outbox and the message itself.<br />

1 #!/usr/bin/env python<br />

2<br />

3 from Tkinter import Tk<br />

4 from time import sleep<br />

5 from tkMessageBox import showwarning<br />

6 import win32com.client as win32<br />

7<br />

8 warn = lambda app: showwarning(app, 'Exit?')<br />

9 RANGE = range(3, 8)<br />

10<br />

11 def outlook():<br />

12 app = 'Outlook'<br />

13 olook = win32.gencache.EnsureDispatch('%s.Application' % app)<br />

14<br />

15 mail = olook.CreateItem(win32.constants.olMailItem)<br />

16 recip = mail.Recipients.Add('you@127.0.0.1')<br />

17 subj = mail.Subject = '<strong>Python</strong>-to-%s Demo' % app<br />

18 body = ["Line %d" % i for i in RANGE]<br />

19 body.insert(0, '%s\r\n' % subj)<br />

20 body.append("\r\nTh-th-th-that's all folks!")<br />

21 mail.Body = '\r\n'.join(body)<br />

22 mail.Send()<br />

23<br />

24 ns = olook.GetNamespace("MAPI")<br />

25 obox = ns.GetDefaultFolder(win32.constants.olFolderOutbox)<br />

26 obox.Display()<br />

27 obox.Items.Item(1).Display()<br />

28<br />

29 warn(app)<br />

30 olook.Quit()<br />

31<br />

32 if __name__=='__main__':<br />

33 Tk().withdraw()<br />

34 outlook()<br />

In this example, we use Outlook to send an e-mail to ourselves. In order to make the demonstration<br />

work, you need to turn off your network access so that you do not really send the message, and thus<br />

are able to view it in your Outbox folder (and delete it if desired after viewing it). After launching<br />

Outlook, we create a new mail message and fill out the various fields such as recipient, subject, and<br />

body (lines 1521). We then call the send() method (line 22) to spool the message to the Outbox where it<br />

will be moved to "Sent Mail" once the message has actually been transmitted to the mail server.<br />

Like PowerPoint, there are many constants available ... olMailItem (with a constant value of 0) is the<br />

one used for e-mail messages. Other popular Outlook items include olAppointmentItem (1),<br />

olContactItem (2), and olTaskItem (3). Of course, there are more, so you will have to find a VB/Office<br />

programming book or search for the constants and their values online.<br />

In the next section (lines 2427), we use another constant,olFolderOutbox (4), to open the Outbox folder<br />

and bring it up for display. We find the most recent item (hopefully the one we just created) and display<br />

it as well. Other popular folders include: olFolderInbox (6), olFolderCalendar (9), olFolderContacts (10),

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!