| 1 | #!/usr/bin/python |
|---|
| 2 | # -*- coding: UTF-8 -*- |
|---|
| 3 | # |
|---|
| 4 | # Copyright (c) 2007 Tarek Ziadé |
|---|
| 5 | # |
|---|
| 6 | # Authors: |
|---|
| 7 | # Tarek Ziadé <tarek@ziade.org> |
|---|
| 8 | # |
|---|
| 9 | # This program is free software; you can redistribute it and/or |
|---|
| 10 | # modify it under the terms of the GNU General Public License |
|---|
| 11 | # as published by the Free Software Foundation; either version 2 |
|---|
| 12 | # of the License, or (at your option) any later version. |
|---|
| 13 | # |
|---|
| 14 | # This program is distributed in the hope that it will be useful, |
|---|
| 15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 17 | # GNU General Public License for more details. |
|---|
| 18 | # |
|---|
| 19 | # You should have received a copy of the GNU General Public License |
|---|
| 20 | # along with this program; if not, write to the Free Software |
|---|
| 21 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 22 | from datetime import datetime |
|---|
| 23 | from base64 import b64encode |
|---|
| 24 | |
|---|
| 25 | from model import mailed_data, mail_data |
|---|
| 26 | |
|---|
| 27 | def send_mail(sender, recipients, subject, msg): |
|---|
| 28 | """sends the mail by storing it into the DB""" |
|---|
| 29 | inserter = mail_data.insert() |
|---|
| 30 | if isinstance(msg, unicode): |
|---|
| 31 | msg = msg.encode('utf8') |
|---|
| 32 | if isinstance(subject, unicode): |
|---|
| 33 | subject = subject.encode('utf8') |
|---|
| 34 | |
|---|
| 35 | res = inserter.execute(subject=subject, sender=sender, |
|---|
| 36 | recipients=','.join(recipients), |
|---|
| 37 | data=b64encode(msg), date=datetime.now()) |
|---|
| 38 | |
|---|
| 39 | return res.last_inserted_ids()[0] |
|---|
| 40 | |
|---|
| 41 | def mail_status(mail_id=None): |
|---|
| 42 | """getting mail status""" |
|---|
| 43 | def _small(mail): |
|---|
| 44 | return {'subject': mail.subject, 'date': mail.date, |
|---|
| 45 | 'status': mail.status} |
|---|
| 46 | |
|---|
| 47 | if mail_id is None: |
|---|
| 48 | return [_small(mail) for mail in |
|---|
| 49 | mailed_data.select().execute()] |
|---|
| 50 | |
|---|
| 51 | res = mailed_data.select().execute(original_id=mail_id).fetchall() |
|---|
| 52 | if len(res) == 0: |
|---|
| 53 | # still in queue |
|---|
| 54 | return u'processing' |
|---|
| 55 | return res[0].status |
|---|
| 56 | |
|---|
| 57 | def mail_queue_size(): |
|---|
| 58 | """return the queue size""" |
|---|
| 59 | return len(mail_data.select().execute().fetchall()) |
|---|