def send_to_capsule(filename, exclude, send, send_from, send_to, smtp_host, smtp_port, smtp_username, smtp_password):
    archive = mbox(filename)
    messages = []
    for message in archive:
        should_exclude = False
        for excluded_to in exclude:
            if excluded_to in message['to']:
                should_exclude = True
                print 'excluding message to {0}'.format(message['to'])
                break
        if should_exclude:
            continue

        messages.append(message)

    if len(messages) > 0 and send and send_to:
        smtp = smtplib.SMTP(smtp_host, int(smtp_port))
        smtp.ehlo()
        smtp.starttls()
        smtp.ehlo()
        smtp.login(smtp_username, smtp_password)

        for message in reversed(messages):
            forward = MIMEBase("multipart", "mixed") 
            forward["Subject"] = "FW: %s" % message['subject']
            forward["From"] = send_from
            forward["To"] = send_to
            forward['bcc'] = None

            rfcmessage = MIMEBase("message", "rfc822") 
            rfcmessage.attach(message) 
            forward.attach(rfcmessage) 

            smtp.sendmail(message['from'], message['to'], forward.as_string(unixfrom=0))
    def test_seq_parts_in_a_multipart(self):
        outer = MIMEBase('multipart', 'mixed')
        outer['Subject'] = 'A subject'
        outer['To'] = '*****@*****.**'
        outer['From'] = '*****@*****.**'
        outer.preamble = ''
        outer.epilogue = ''
        msg = MIMEText('hello world')
        outer.attach([msg])
        outer.set_boundary('BOUNDARY')
        self.assertEqual(outer.as_string(), '''\
Content-Type: multipart/mixed; boundary="BOUNDARY"
MIME-Version: 1.0
Subject: A subject
To: [email protected]
From: [email protected]

--BOUNDARY
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

hello world

--BOUNDARY--
''')        
Beispiel #3
0
 def __sendMail(self, to, title, sMail):
     if self.m_SMTPServer == None:
         self.m_SMTPServer = SMTPServer
     if self.m_SMTPPort == None:
         self.m_SMTPPort = SMTPPort
     if self.m_SMTPUser == None:
         self.m_SMTPUser = SMTPUser
     if self.m_SMTPPass == None:
         self.m_SMTPPass = SMTPPass
     if self.m_mailFrom == None:
         self.m_mailFrom = SMTPFrom
     try:
         smtp = smtplib.SMTP()
         msgRoot = MIMEBase('multipart', 'mixed', boundary='BOUNDARY')
         msgRoot['Subject'] = title
         msgRoot['From'] = self.m_mailFrom
         msgRoot['To'] = to
         rcpts = to.split(";")
         msgAlternative = MIMEBase('multipart',
                                   'mixed',
                                   boundary='BOUNDARY')
         msgRoot.attach(msgAlternative)
         msgText = MIMEText(sMail, 'html')
         msgAlternative.attach(msgText)
         #            smtp.set_debuglevel(1)
         smtp.connect(self.m_SMTPServer)
         if self.m_SMTPUser != None and len(self.m_SMTPUser) > 0:
             smtp.login(self.m_SMTPUser, self.m_SMTPPass)
         smtp.sendmail(self.m_mailFrom, rcpts, msgRoot.as_string())
         return True
     except Exception, e:
         WBXTF.WBXTFOutput("Fail to send mail:%s" % (e), WBXTF.typeWarning)
         return False
Beispiel #4
0
class MIMEBase64(AbstractMessageHandler):
    def __init__(self, content):
        self.message = MIMEBase('text', 'plain')
        self.message.set_payload(content)
        Encoders.encode_base64(self.message)

    def add_header(self, key, value):
        self.message[key] = value

    def __str__(self):
        return self.message.as_string()
Beispiel #5
0
    def run(self):
        try:
            subject = 'Answering machine message'
            txt = 'See audio attachment'
            
            if self.cli:
                subject = 'Answering machine message from %s' % self.cli
                subject, txt = self.name_lookup(subject, txt)

            msg = MIMEBase('multipart', 'mixed')
            msg['Subject'] = subject
            msg['Date'] = email.Utils.formatdate()
            msg['From'] = smtp_from
            msg['To'] = ', '.join(smtp_to)
            # this is not normally visible
            msg.preamble = 'This is a message in MIME format.\r\n'
            # Guarantees the message ends in a newline
            msg.epilogue = ''

            # attach text comment
            msg.attach(MIMEText(txt))

            if self.file:
                d = self.file.read()
                att = MIMEAudio(wav_header(d, WAVE_FORMAT_ALAW) + d, 'x-wav',
                                name=time.strftime('%Y-%m-%d-%H-%M-%S') + '.wav')
            
                msg.attach(att)

                # close the file opened in the recording
                self.file.close()
                self.file = None

            if self.call:
                # disconnect the call
                self.call.disconnect()
                self.call = None
        
            smtp = smtplib.SMTP(smtp_server)
            smtp.sendmail(smtp_from, smtp_to, msg.as_string(unixfrom=0))
            smtp.close()

        except:
            log.warn('answering machine email failed', exc_info=1)
            return

        log.info('answering machine mail sent from %s' % self.cli)
    def test_no_parts_in_a_multipart(self):
        outer = MIMEBase('multipart', 'mixed')
        outer['Subject'] = 'A subject'
        outer['To'] = '*****@*****.**'
        outer['From'] = '*****@*****.**'
        outer.preamble = ''
        outer.epilogue = ''
        outer.set_boundary('BOUNDARY')
        msg = MIMEText('hello world')
        self.assertEqual(outer.as_string(), '''\
Content-Type: multipart/mixed; boundary="BOUNDARY"
MIME-Version: 1.0
Subject: A subject
To: [email protected]
From: [email protected]

--BOUNDARY


--BOUNDARY--
''')        
Beispiel #7
0
 def report(self, toMail, title = "Case Report"):
     """
     Send a report by mail
     
     @type toMail: string
     @param toMail: the mail address of the receivers
                 for example, "[email protected];[email protected]"
     @type title: string
     @param title: the mail title
     @see: WBXTFConfigMail
     """     
     if self.m_SMTPServer == "":
         self.m_SMTPServer = os.environ.get("WBXTF_MAIL_SERVER")
     if self.m_SMTPUser == "":
         self.m_SMTPUser = os.environ.get("WBXTF_MAIL_USER")
     if self.m_SMTPPass == "":
         self.m_SMTPPass = os.environ.get("WBXTF_MAIL_PASSWORD")
     if self.m_Sender == "":
         self.m_Sender = os.environ.get("WBXTF_MAIL_SENDER")
 
     if self.m_SMTPServer == None or len(self.m_SMTPServer) == 0:
         WBXTFOutput("Please indicate the mail server")
         return
     if self.m_SMTPServer == None or len(self.m_Sender) == 0:
         WBXTFOutput("Please indicate the mail sender")
         return     
        
     # Convert Text
     sMail = ""
     data = {}
     data["checkpoint_pass"] = 0
     data["checkpoint_fail"] = 0
     for log in self.m_log.getLog():
         type = log["type"]
         if not data.has_key(type):
             data[type] = 0
         data[type] = data[type] + 1                    
         if type == typeCheck:
             if log["more"]:
                 data["checkpoint_pass"] = data["checkpoint_pass"] + 1
             else:
                 data["checkpoint_fail"] = data["checkpoint_fail"] + 1 
    
     sMail = sMail + "<H3>Report</H3>"
     bPass = True
     if data.has_key(typeError) and data[typeError] > 0:
         bPass = False
     if data.has_key("checkpoint_fail") and data["checkpoint_fail"] > 0:
         bPass = False       
     sMail = sMail + '<table border="1" cellpadding="0" cellspacing="0">'
     sMail = sMail + "<tr><td>Total Logs</td><td width=50>%d</td></tr>" % len(self.m_log.getLog())
     if data.has_key(typeInfo) and data[typeInfo] > 0:
         sMail = sMail + "<tr><td>Information Logs</td><td>%d</td><tr>" % data[typeInfo]
     if data.has_key(typeWarning) and data[typeWarning] > 0:
         sMail = sMail + "<tr><td>Warning Logs</td><td>%d</td><tr>" % data[typeWarning]
     if data.has_key(typeError) and data[typeError] > 0:
         sMail = sMail + '<tr><td><font color="red">Error Logs</font></td><td><font color="red">%d</font></td><tr>' % data[typeError]
     if data.has_key(typeDebug) and data[typeDebug] > 0:
         sMail = sMail + "<tr><td>Debug Logs</td><td>%d</td><tr>" % data[typeDebug]        
     if data.has_key(typeCheck) and data[typeCheck] > 0:
         sMail = sMail + "<tr><td>Check Point Logs</td><td>%d</td><tr>" % data[typeCheck]           
     if data.has_key("checkpoint_pass") and data["checkpoint_pass"] > 0:
         sMail = sMail + '<tr><td><font color="green">Check Point Logs (Passed)</font></td><td><font color="green">%d</font></td><tr>' % data["checkpoint_pass"]
     if data.has_key("checkpoint_fail") and data["checkpoint_fail"] > 0:
         sMail = sMail + '<tr><td><font color="red">Check Point Logs (Failed)</font></td><td><font color="red">%d</font></td><tr>' % data["checkpoint_fail"]
 
     if bPass:
         sMail = sMail + '<tr><td><font color="green">Passed!</td><td></td></tr>'
     else:
         sMail = sMail + '<tr><td><font color="red">Failed!</td><td></td></tr>'      
     sMail = sMail + '</table>'          
     sMail = sMail + "<br><hr><p><H3>Logs</H3>"    
     for log in self.m_log.getLog():
         sHead = ""
         sEnd = ""
         sBody = ""
         color = ""
         type = log["type"]
         if not data.has_key(type):
             data[type] = 0
         data[type] = data[type] + 1                    
         if type == typeError:
             color = "red"
         elif type == typeWarning:
             color = "yellow"
         elif type == typeCheck:
             if log["more"]:
                 data["checkpoint_pass"] = data["checkpoint_pass"] + 1
                 color = "green"
             else:
                 data["checkpoint_fail"] = data["checkpoint_fail"] + 1
                 color = "red"
         elif type == typeDebug:
             color = "#666666"                                     
         if color != "":             
             sHead = '<font color = "%s">' % (color)
             sEnd = '</font>'                            
         sBody = self.m_log.formatLog(log)
         sMail = sMail + sHead
         sMail = sMail + sBody
         sMail = sMail + sEnd              
         sMail = sMail + "<br>"             
         
     strFrom = self.m_Sender
     strTo = toMail
     smtp = smtplib.SMTP()
     msgRoot = MIMEBase('multipart', 'mixed', boundary='BOUNDARY')
     msgRoot['Subject'] = title
     msgRoot['From'] = strFrom
     msgRoot['To'] = strTo
     msgAlternative = MIMEBase('multipart', 'mixed', boundary='BOUNDARY')
     msgRoot.attach(msgAlternative)    
     msgText = MIMEText(sMail, 'html')
     msgAlternative.attach(msgText)
     #smtp.set_debuglevel(1)
     smtp.connect(self.m_SMTPServer)
     if self.m_SMTPUser != None and len(self.m_SMTPUser) > 0:
         smtp.login(self.m_SMTPUser, self.m_SMTPPass)
     smtp.sendmail(strFrom, strTo, msgRoot.as_string())