Zionetrix::Git
Repositories
Help
Report an Issue
mantisbt-smtp-gateway
Code
Commits
Branches
Tags
Search
Tree:
a875b96
Branches
Tags
master
mantisbt-smtp-gateway
src
usr
share
mantis-smtp
MantisMail.py
Initial commit
Benjamin Renard
commited
a875b96
at 2011-11-24 16:55:40
MantisMail.py
Blame
History
Raw
#!/usr/bin/python import email import re import string from email.MIMEText import MIMEText import smtplib import logging class MantisMail(object): def __init__(self,mailfrom,mailto,data): self.mailfrom = mailfrom self.mailto = mailto self.msg = email.message_from_string(data) def get_issue_id_from_subject(self): res=re.search('\[[^\]]* ([0-9]*)\]',self.subject()) if res: issue_id=res.group(1) return string.atoi(issue_id) return None def subject(self): return self.msg['Subject'] def get_text(self): content='From : %s' % self.msg['From'] for part in self.msg.walk(): if part.get_content_maintype() == 'text': content='%s\n\n---------------------------\n\n%s' % (content,part.get_payload(decode=True)) return content def get_attachments(self): attachments=[] counter=1 for part in self.msg.walk(): part_type = part.get_content_maintype() if part_type == 'text' or part_type == 'multipart': continue filename = part.get_filename() if not filename: ext = mimetypes.guess_extension(part.get_content_type()) if not ext: ext = '.bin' filename = 'part-%03d%s' % (counter, ext) counter += 1 content=part.get_payload(decode=False) attachments.append({ 'filename': filename, 'type': part_type, 'content': content }) return attachments def send(mail_from,to,subject,content,smtp_host='127.0.0.1',smtp_port=25): msg = MIMEText(content) msg['To'] = to msg['From'] = mail_from msg['Subject'] = subject server = smtplib.SMTP(smtp_host,smtp_port) try: server.sendmail(mail_from, [to], msg.as_string()) except: logging.error('Error sending mail to %s - %s : %s' % (to,subject,sys.exc_info()[0])) return