summaryrefslogtreecommitdiffstats
path: root/utils/log_picker/sending/emailsender.py
blob: 03c94ca625522277b234cd971a416d05f744fdca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import os
import smtplib
import email
import email.encoders
import email.mime.multipart
from log_picker.sending.senderbaseclass import SenderBaseClass
from log_picker.sending.senderbaseclass import SenderError


class EmailSender(SenderBaseClass):
   
    _description = "Email from LogPicker"
    
    def __init__(self, sendby, addresses, smtp_server, *args, **kwargs):
        """Send file by email.
        @sendby - Sender email address. (string)
        @addresses - List of destination email addresses. (list)
        @smtp_server - SMTP server address. (string)"""
        
        SenderBaseClass.__init__(self, args, kwargs)
        self.smtp_server = smtp_server
        self.sendby = sendby
        self.addresses = addresses
        self.comment = ""
    
    def set_comment(self, comment):
        self.comment = comment
    
    def sendfile(self, filename, contenttype):
        # Create email message
        msg = email.mime.multipart.MIMEMultipart()
        msg['Subject'] = self._get_description(self._description)
        msg['From'] = self.sendby
        msg['To'] = ', '.join(self.addresses)
        msg.preamble = 'This is a multi-part message in MIME format.'
        
        # Add message text
        msgtext = email.mime.base.MIMEBase("text", "plain")
        msgtext.set_payload(self.comment)
        msg.attach(msgtext)
        
        # Add message attachment
        cont_type = contenttype.split('/', 1)
        if len(cont_type) == 1:
            cont_type.append("")
        elif not cont_type:
            cont_type = ["application", "octet-stream"]
        
        attach_data = open(filename, 'rb').read()
        
        msgattach = email.mime.base.MIMEBase(cont_type[0], cont_type[1])
        msgattach.set_payload(attach_data)
        email.encoders.encode_base64(msgattach)
        msgattach.add_header('Content-Disposition', 'attachment', 
                filename=os.path.basename(filename))
        msg.attach(msgattach)
        
        # Send message
        try:
            s = smtplib.SMTP(self.smtp_server)
        except(smtplib.socket.gaierror, smtplib.SMTPServerDisconnected):
            raise SenderError("Email cannot be send. " +\
                                "Error while connecting to smtp server.")
        
        try:
            s.sendmail(self.sendby, self.addresses, msg.as_string())
        except(smtplib.SMTPRecipientsRefused) as e:
            raise SenderError("Email cannot be send. Wrong destination " +\
                                "email address?\nErr message: %s" % e)
        s.quit()