[0] | 1 | # -*- coding: utf-8 -*-
|
---|
| 2 |
|
---|
[11] | 3 | """
|
---|
[16] | 4 | The mailjam project - mta.py
|
---|
[11] | 5 |
|
---|
| 6 | This file is released under the BSD license, see LICENSE for
|
---|
| 7 | more information.
|
---|
| 8 |
|
---|
| 9 | Francisco de Borja Lopez Rio - <borja@codigo23.net>
|
---|
| 10 | Soluciones Informaticas Codigo23 S.L.U. - http://codigo23.net
|
---|
| 11 | """
|
---|
| 12 |
|
---|
[15] | 13 | import os, sys, email, smtplib, xmlrpclib
|
---|
[0] | 14 | from datetime import datetime
|
---|
[16] | 15 | from mailjam.models import MailingList
|
---|
| 16 | from mailjam.config import MTAClientConfig
|
---|
| 17 | from mailjam.tools import validate_email_address
|
---|
[0] | 18 |
|
---|
[15] | 19 | class MTAClient():
|
---|
[0] | 20 |
|
---|
[15] | 21 | def __init__(self, address=None, configfile=None):
|
---|
| 22 | if not validate_email_address(address):
|
---|
| 23 | raise ValueError(address, ' is not a valid email address')
|
---|
| 24 |
|
---|
[20] | 25 | mta_config = MTAClientConfig(configfile=configfile)
|
---|
[15] | 26 | mta_config.load()
|
---|
| 27 | self.config = mta_config.config
|
---|
| 28 | self.rpc = xmlrpclib.ServerProxy(self.config['server']['uri'])
|
---|
| 29 | self.address = self._validate_address(address)
|
---|
| 30 | self.suscriptors = self.rpc.members.list(address)
|
---|
| 31 | self.reply_to = self.address
|
---|
[2] | 32 | self.raw_email = None
|
---|
[0] | 33 | self.queue = []
|
---|
[15] | 34 |
|
---|
| 35 | def _validate_archive_path(self):
|
---|
| 36 | """
|
---|
| 37 | Validate that the archive path exists. If not, try to create it
|
---|
| 38 | """
|
---|
| 39 | if not os.path.exists(self.config['archive']['path']):
|
---|
| 40 | # FIXME: This will raise an IOError if the user has no
|
---|
| 41 | # privileges to create the directory, perhaps we should catch
|
---|
| 42 | # that and show an error instead
|
---|
| 43 | os.makedirs(self.config['archive']['path'])
|
---|
| 44 | return True
|
---|
[0] | 45 |
|
---|
[15] | 46 | def _validate_address(self, address=None):
|
---|
| 47 | """
|
---|
| 48 | The address provided by the user will be valid only if it is a
|
---|
| 49 | valid email address and if it is the email address of an already
|
---|
| 50 | existing mailing list
|
---|
| 51 | """
|
---|
| 52 | if not validate_email_address(address):
|
---|
| 53 | raise ValueError(address, ' is not a valid email address')
|
---|
| 54 | if not address in self.rpc.lists.addresses():
|
---|
| 55 | raise ValueError(address,
|
---|
| 56 | ' is not the address of any existing mailing list')
|
---|
| 57 | # If it is valid, return it
|
---|
| 58 | return address
|
---|
| 59 |
|
---|
| 60 | def get_raw_email(self, raw_email=None):
|
---|
| 61 | """
|
---|
| 62 | get the raw data containing email information. If raw_email is None,
|
---|
| 63 | try to get the data from stdin
|
---|
| 64 | """
|
---|
| 65 | if not raw_email:
|
---|
| 66 | try:
|
---|
| 67 | self.raw_email = sys.stdin.read()
|
---|
| 68 | except:
|
---|
| 69 | raise IOError('Can not get a valid email from stdin')
|
---|
| 70 | else:
|
---|
| 71 | self.raw_email = raw_email
|
---|
| 72 | # FIXME: We should perform some checks on the raw data, ensuring
|
---|
| 73 | # it is a valid email text
|
---|
[2] | 74 | return self.raw_email
|
---|
[0] | 75 |
|
---|
[2] | 76 | def save_raw_email(self):
|
---|
| 77 | if not self.raw_email:
|
---|
| 78 | # FIXME: perhaps a while loop here, with some maximum recursion
|
---|
| 79 | # check, would be nice here
|
---|
[15] | 80 | self.get_raw_email()
|
---|
| 81 | # Check the path to the archive exists
|
---|
| 82 | self._validate_archive_path()
|
---|
| 83 | filename = os.path.join(self.config['archive']['path'],
|
---|
[0] | 84 | datetime.today().strftime('%Y%d%m%H%M%S%f'))
|
---|
| 85 | tmpfile = file(filename, 'w')
|
---|
[2] | 86 | tmpfile.write(self.raw_email)
|
---|
[0] | 87 | tmpfile.close()
|
---|
| 88 | self.queue.append(filename)
|
---|
| 89 |
|
---|
[2] | 90 | def send_email(self):
|
---|
[15] | 91 | """
|
---|
| 92 | Send emails from the queue, if there is any
|
---|
| 93 | """
|
---|
[2] | 94 | if self.queue:
|
---|
| 95 | next_email = self.queue.pop()
|
---|
| 96 | email_file = file(next_email, 'r')
|
---|
| 97 | email_data = email.message_from_file(email_file)
|
---|
| 98 | email_file.close()
|
---|
| 99 | email_data['Reply-to'] = self.reply_to
|
---|
[26] | 100 | smtp_conn = smtplib.SMTP()
|
---|
| 101 | smtp_conn.connect()
|
---|
| 102 | smtp_conn.sendmail(email_data['From'], self.suscriptors,
|
---|
| 103 | email_data.as_string())
|
---|
| 104 | smtp_conn.close()
|
---|
[15] | 105 | # FIXME: enable tmp data removal here:
|
---|
| 106 | # if not self.config['archive']['persistent']:
|
---|
| 107 | # REMOVE THE FILE FROM DISK
|
---|
[0] | 108 |
|
---|
[2] | 109 | def run(self):
|
---|
| 110 | self.save_raw_email()
|
---|
| 111 | self.send_email()
|
---|