Esempio n. 1
0
# Some utilities for mail
from email.Message import Message
from email import Header
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
from email.Charset import add_charset, QP, SHORTEST
from reStructuredText import HTML

# Add a charset for utf8 that should be encoded quopri,
# e.g. we assume mostly latin chars, if not the encoding will
# be very space inefficient
add_charset('utf8', SHORTEST, QP, 'utf8')


def decode_header(value):
    """Converts an encoded header into a unicode string::

        >>> decode_header(
        ...       'Je les =?utf-8?b?ZMODwql0w4PCqHN0ZQ==?= oui?')
        u'Je les d\\xc3\\xa9t\\xc3\\xa8ste oui?'

    """
    encoded_strings = Header.decode_header(value)
    header_val = Header.Header()
    for string, encoding in encoded_strings:
        header_val.append(string, encoding, errors='replace')
    return unicode(header_val)


def construct_simple_encoded_message(from_addr,
                                     to_addr,
Esempio n. 2
0
  def perform(self, req, summary):
    if summary is None:
      return False
    self.config = self.env.config
    self.encoding = 'utf-8'
    subject = self.subject

    if not self.config.getbool('notification', 'smtp_enabled'):
      return False
    smtp_server = self.config['notification'].get('smtp_server')
    smtp_port = self.config['notification'].getint('smtp_port')
    from_email = self.config['notification'].get('smtp_from')
    from_name = self.config['notification'].get('smtp_from_name')
    replyto_email = self.config['notification'].get('smtp_replyto')
    from_email = from_email or replyto_email
    if not from_email:
      return False
    
    # Authentication info (optional)
    user_name = self.config['notification'].get('smtp_user')
    password = self.config['notification'].get('smtp_password')
    
    # Thanks to the author of this recipe:
    # http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/473810
    
    from email.MIMEMultipart import MIMEMultipart
    from email.MIMEText import MIMEText
    from email.MIMEImage import MIMEImage
    from email.Charset import add_charset, SHORTEST
    add_charset( 'utf-8', SHORTEST, None, None )

    projname = self.config.get('project', 'name')
    
    # Create the root message and fill in the from, to, and subject headers
    msg_root = MIMEMultipart('alternative')
    msg_root['To'] = str(', ').join(self.emails)
    
    msg_root['X-Mailer'] = 'ClientsPlugin for Trac'
    msg_root['X-Trac-Version'] =  __version__
    msg_root['X-Trac-Project'] =  projname
    msg_root['Precedence'] = 'bulk'
    msg_root['Auto-Submitted'] = 'auto-generated'
    msg_root['Subject'] = subject
    msg_root['From'] = '%s <%s>' % (from_name or projname, from_email)
    msg_root['Reply-To'] = replyto_email
    msg_root.preamble = 'This is a multi-part message in MIME format.'
    
    view = 'plain'
    arg = "'%s'" % view
    result = self.transform(summary, view=arg)
    msg_text = MIMEText(str(result), view, self.encoding)
    msg_root.attach(msg_text)
    
    msg_related = MIMEMultipart('related')
    msg_root.attach(msg_related)
    
    view = 'html'
    arg = "'%s'" % view
    result = self.transform(summary, view=arg)
    #file = open('/tmp/send-client-email.html', 'w')
    #file.write(str(result))
    #file.close()

    msg_text = MIMEText(str(result), view, self.encoding)
    msg_related.attach(msg_text)
    
    # Handle image embedding...
    view = 'images'
    arg = "'%s'" % view
    result = self.transform(summary, view=arg)
    if result:
      images = result.getroot()
      if images is not None:
        for img in images:
          if 'img' != img.tag:
            continue
          if not img.get('id') or not img.get('src'):
            continue
          
          fp = open(img.get('src'), 'rb')
          if not fp:
            continue
          msg_img = MIMEImage(fp.read())
          fp.close()
          msg_img.add_header('Content-ID', '<%s>' % img.get('id'))
          msg_related.attach(msg_img)
    
    # Send the email
    import smtplib
    smtp = smtplib.SMTP() #smtp_server, smtp_port)
    if False and user_name:
        smtp.login(user_name, password)
    smtp.connect()
    smtp.sendmail(from_email, self.emails, msg_root.as_string())
    smtp.quit()
    return True
Esempio n. 3
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import smtplib
import traceback
from email.mime.text import MIMEText
from email.Charset import add_charset, QP
add_charset('utf-8', QP, QP, 'utf-8')
from time import time, sleep

def csv_reader(csv_data, **kwargs):
    if not isinstance(csv_data, str):
        m = "Please provide string data to csv_reader, not %s" % type(csv_data)
        raise ValueError(m)
    encodings = ['utf-8', 'iso8859-7', 'utf-16', 'utf-16le', 'utf-16be']
    encodings.reverse()
    rows = []
    append = rows.append
    while 1:
        if not encodings:
            m = "Cannot decode csv data!"
            raise ValueError(m)
        encoding = encodings[-1]
        try:
            data = csv_data.decode(encoding)
            data = data.strip(u'\ufeff')
            if data.count(u'\x00') > 0:
                m = "Wrong encoding detected (heuristic)"
                raise ValueError(m)
            if data.count(u'\u2000') > data.count(u'\u0020'):
                m = "Wrong endianess (heuristic)"
Esempio n. 4
0
# Some utilities for mail
from email.Message import Message
from email import Header
from email.MIMEText import MIMEText
from email.MIMEMultipart import MIMEMultipart
from email.Charset import add_charset, QP, SHORTEST
from reStructuredText import HTML

# Add a charset for utf8 that should be encoded quopri,
# e.g. we assume mostly latin chars, if not the encoding will
# be very space inefficient
add_charset('utf8', SHORTEST, QP, 'utf8')

def decode_header(value):
    """Converts an encoded header into a unicode string::

        >>> decode_header(
        ...       'Je les =?utf-8?b?ZMODwql0w4PCqHN0ZQ==?= oui?')
        u'Je les d\\xc3\\xa9t\\xc3\\xa8ste oui?'

    """
    encoded_strings = Header.decode_header(value)
    header_val = Header.Header()
    for string, encoding in encoded_strings:
        header_val.append(string, encoding, errors='replace')
    return unicode(header_val)

def construct_simple_encoded_message(from_addr, to_addr, subject, body,
                                  other_headers=None, encoding='utf8'):
    """All inputs to this method are expected to be unicode or ascii.
Esempio n. 5
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import smtplib
import traceback
from email.mime.text import MIMEText
from email.Charset import add_charset, QP
add_charset('utf-8', QP, QP, 'utf-8')
from time import time, sleep


def csv_reader(csv_data, **kwargs):
    if not isinstance(csv_data, str):
        m = "Please provide string data to csv_reader, not %s" % type(csv_data)
        raise ValueError(m)
    encodings = ['utf-8', 'iso8859-7', 'utf-16', 'utf-16le', 'utf-16be']
    encodings.reverse()
    rows = []
    append = rows.append
    while 1:
        if not encodings:
            m = "Cannot decode csv data!"
            raise ValueError(m)
        encoding = encodings[-1]
        try:
            data = csv_data.decode(encoding)
            data = data.strip(u'\ufeff')
            if data.count(u'\x00') > 0:
                m = "Wrong encoding detected (heuristic)"
                raise ValueError(m)
            if data.count(u'\u2000') > data.count(u'\u0020'):