
#
# spyne - Copyright (C) Spyne contributors.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
#

from __future__ import absolute_import

import logging
logger = logging.getLogger(__name__)

import getpass
import inspect
import traceback
import smtplib

from socket import gethostname
from subprocess import Popen, PIPE

from email.utils import COMMASPACE, formatdate
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication

from spyne.util import six


def email_exception(exception_address, message="", bcc=None):
    # http://stackoverflow.com/questions/1095601/find-module-name-of-the-originating-exception-in-python
    frm = inspect.trace()[-1]
    mod = inspect.getmodule(frm[0])
    module_name = mod.__name__ if mod else frm[1]

    sender = 'robot@spyne.io'
    recipients = [exception_address]
    if bcc is not None:
        recipients.extend(bcc)

    error_str = ("%s\n\n%s" % (message, traceback.format_exc()))
    msg = MIMEText(error_str.encode('utf8'), 'plain', 'utf8')
    msg['To'] = exception_address
    msg['From'] = 'Spyne <robot@spyne.io>'
    msg['Date'] = formatdate()
    msg['Subject'] = "(%s@%s) %s" % (getpass.getuser(), gethostname(), module_name)

    try:
        smtp_object = smtplib.SMTP('localhost')
        smtp_object.sendmail(sender, recipients, msg.as_string())
        logger.error("Error email sent")

    except Exception as e:
        logger.error("Error: unable to send email")
        logger.exception(e)


def email_text_smtp(addresses, sender=None, subject='', message="",
                                                     host='localhost', port=25):
    if sender is None:
        sender = 'Spyne <robot@spyne.io>'

    exc = traceback.format_exc()
    if exc is not None:
        message = (u"%s\n\n%s" % (message, exc))
    msg = MIMEText(message.encode('utf8'), 'plain', 'utf8')
    msg['To'] = COMMASPACE.join(addresses)
    msg['From'] = sender
    msg['Date'] = formatdate()
    msg['Subject'] = subject

    smtp_object = smtplib.SMTP(host, port)
    if six.PY2:
        smtp_object.sendmail(sender, addresses, msg.as_string())
    else:
        smtp_object.sendmail(sender, addresses, msg.as_bytes())
    logger.info("Text email sent to: %r.", addresses)


def email_text(addresses, sender=None, subject='', message="", bcc=None,
                                                                      att=None):
    if att is None:
        att = {}

    if sender is None:
        sender = 'Spyne <robot@spyne.io>'

    exc = traceback.format_exc()
    if exc is not None and exc != 'None\n':
        message = (u"%s\n\n%s" % (message, exc))
    msg = MIMEText(message.encode('utf8'), 'plain', 'utf8')
    if len(att) > 0:
        newmsg = MIMEMultipart()
        newmsg.attach(msg)
        for k, v in att.items():
            part = MIMEApplication(v)
            part.add_header('Content-Disposition', 'attachment', filename=k)
            newmsg.attach(part)

        msg = newmsg

    msg['To'] = COMMASPACE.join(addresses)
    msg['From'] = sender
    msg['Date'] = formatdate()
    msg['Subject'] = subject

    cmd = ["/usr/sbin/sendmail", "-oi", '--']
    cmd.extend(addresses)
    if bcc is not None:
        cmd.extend(bcc)

    p = Popen(cmd, stdin=PIPE)
    if six.PY2:
        p.communicate(msg.as_string())
    else:
        p.communicate(msg.as_bytes())

    logger.info("Text email sent to: %r.", addresses)

@@ -922,16 +922,33 @@
  smtplib
+%0Aimport mimetypes
 %0A%0Afrom s
@@ -1055,16 +1055,54 @@
 matdate%0A
+from email import message_from_string%0A
 from ema
@@ -1150,89 +1150,265 @@
 ime.
-multipart import MIMEMultipart%0Afrom email.mime.application import MIMEApplication
+image import MIMEImage%0Afrom email.mime.message import MIMEMessage%0Afrom email.mime.multipart import MIMEMultipart%0Afrom email.mime.application import MIMEApplication%0Afrom email.mime.nonmultipart import MIMENonMultipart%0Afrom email.encoders import encode_base64
 %0A%0Afr
@@ -3238,34 +3238,34 @@
 r=None, subject=
-''
+%22%22
 , message=%22%22, bc
@@ -3532,16 +3532,46 @@
 'None%5Cn'
+ and exc != 'NoneType: None%5Cn'
 :%0A      
@@ -3803,33 +3803,158 @@
     
-part = MIMEApplication
+mime_type, encoding = mimetypes.guess_type(k)%0A            if mime_type == %22message/rfc822%22:%0A                part = MIMEMessage(message_from_string
 (v)
+)
 %0A   
@@ -3958,24 +3958,487 @@
             
+elif mime_type.startswith(%22image/%22):%0A                part = MIMEImage(v, mime_type.rsplit('/', 1)%5B-1%5D)%0A            elif mime_type is not None:%0A                mime_type_main, mime_type_sub = mime_type.split('/', 1)%0A                part = MIMENonMultipart(mime_type_main, mime_type_sub)%0A                part.set_payload(v)%0A                encode_base64(part)%0A            else:%0A                part = MIMEApplication(v)%0A%0A            newmsg.attach(part)%0A            
 part.add_hea
@@ -4492,40 +4492,8 @@
 me=k
-)%0A            newmsg.attach(part
 )%0A%0A 

add mime type support to email_text