Example #1
0
def mail(gmail_user, gmail_pwd, to, cc, subject, text, attach):
   msg = MIMEMultipart()

   msg['From'] = gmail_user
   msg['To'] = to
   #msg['Cc'] = "[email protected],[email protected],[email protected],[email protected],[email protected],[email protected]"
   msg['Subject'] = subject
   
   msg.add_header('Cc', cc)
   #msg.add_header('Cc', '[email protected],[email protected],[email protected],[email protected],[email protected],[email protected]')
   #msg.add_header('To', '[email protected],[email protected]')
   msg.attach(MIMEText(text))

   part = MIMEBase('application', 'octet-stream')
   part.set_payload(open(attach, 'rb').read())
   Encoders.encode_base64(part)
   part.add_header('Content-Disposition',
           'attachment; filename="%s"' % os.path.basename(attach))
   msg.attach(part)

   mailServer = smtplib.SMTP("smtp.gmail.com", 587)
   mailServer.ehlo()
   mailServer.starttls()
   mailServer.ehlo()
   mailServer.login(gmail_user, gmail_pwd)
   mailServer.sendmail(gmail_user, msg.get_all('To') + msg.get_all('Cc'), msg.as_string())
   
   #Specifically for this app - Cc to [email protected]
   # Should be mailServer.quit(), but that crashes...
   mailServer.close()
Example #2
0
    def mail(self):
        """
        Use the add header message and then the get_all message to add multiple recipients
        """
        msg = MIMEMultipart()
        msg.add_header('From', self.fromAddress)
        msg.add_header('Subject', self.subject)
        
        #Loop over csv string to add recipients
        for emailAddress in self.to.split(','):
            msg.add_header('To', emailAddress)
        
    
        msg.attach(MIMEText(self.text))
    
        if self.attach != None:
            part = MIMEBase('application', 'octet-stream')
            part.set_payload(open(self.attach, 'rb').read())
            Encoders.encode_base64(part)
            part.add_header('Content-Disposition',
                   'attachment; filename="%s"' % os.path.basename(self.attach))
            msg.attach(part)
    
        try:
            mailServer = smtplib.SMTP(self.server, self.port)
            mailServer.ehlo()
            mailServer.starttls()
            mailServer.ehlo()
            
            #Assumes the password is obfuscated
            
            tmp = deObfuscateString(self.passwd)
            mailServer.login(self.fromAddress, tmp)
            mailServer.sendmail(self.fromAddress, msg.get_all(('To')), msg.as_string())
            # Should be mailServer.quit(), but that crashes...
            
        except smtplib.SMTPAuthenticationError as inst:
            output = "ERROR GENERATED: EmailWrapper.Mail\n"
            output += "Exception Type: " + str(type(inst)) + "\n"
            output += "Exception: " + str(inst) + "\n"
            self.errString += output + '\n' 
            self.hasErrors = True
            print self.errString

            
        except Exception as inst:
            output = "ERROR GENERATED:\n"
            output += "Exception Type: " + str(type(inst)) + "\n"
            output += "Exception: " + str(inst) + "\n"
            self.errString += output + '\n' 
            self.hasErrors = True
            print self.errString
            
        finally:
            mailServer.close()