summaryrefslogtreecommitdiff
path: root/Mailman/Handlers/Sendmail.py
blob: af4db61421e667dd2e5e9a0c6fad90e9bb1381cc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# Copyright (C) 1998 by the Free Software Foundation, Inc.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software 
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

"""Deliver a message via `sendmail' drop-off.

This module delivers the message via the command line interface to the
sendmail program.  It should work for sendmail clones like Postfix.  It is
expected that sendmail handles final delivery, message queueing, etc.  The
recipient list is only trivially split so that the command line is less that
4k in size.

"""

import string
import os
import HandlerAPI
from Mailman import mm_cfg

class SendmailHandlerError(HandlerAPI.HandlerError):
    pass


MAX_CMDLINE = 3000



def process(mlist, msg):
    """Process the message object for the given list.

    The message object is an instance of Mailman.Message and must be fully
    prepared for delivery (i.e. all the appropriate headers must be set).  The
    message object can have the following attributes:

    recips - the list of recipients for the message (required)

    This function processes the message by handing off the delivery of the
    message to a sendmail (or sendmail clone) program.  It can raise a
    SendmailHandlerError if an error status was returned by the sendmail
    program.
    
    """
    # make sure the command line is of a manageable size
    recipchunks = []
    currentchunk = []
    chunklen = 0
    for r in msg.recips:
        currentchunk.append(r)
        chunklen = chunklen + len(r) + 1
        if chunklen > MAX_CMDLINE:
            recipchunks.append(string.join(currentchunk))
            currentchunk = []
            chunklen = 0
    # pick up the last one
    if chunklen:
        recipchunks.append(string.join(currentchunk))
    # get all the lines of the message, since we're going to do this over and
    # over again
    msgtext = str(msg)
    # cycle through all chunks
    for recips in recipchunks:
        fp = os.popen(mm_cfg.SENDMAIL_CMD + ' ' + recips, 'w')
        fp.write(msgtext)
        status = fp.close()
        if status:
            errcode = (status & 0xff00) >> 8
            mlist.LogMsg('post', 'post to %s from %s, size=%d, failure=%d' %
                         (mlist.internal_name(), msg.GetSender(),
                          len(msg.body), errcode))
            raise SendmailHandlerError(errcode)
        # Log the successful post
        mlist.LogMsg('post', 'post to %s from %s, size=%d, success' %
                     (mlist.internal_name(), msg.GetSender(), len(msg.body)))