| 1 | # Copyright (C) 2005-2007 Insecure.Com LLC. |
|---|
| 2 | # |
|---|
| 3 | # Authors: Adriano Monteiro Marques <py.adriano@gmail.com> |
|---|
| 4 | # Frederico Silva Ribeiro <ribeiro.fsilva@gmail.com> |
|---|
| 5 | # |
|---|
| 6 | # This program is free software; you can redistribute it and/or modify |
|---|
| 7 | # it under the terms of the GNU General Public License as published by |
|---|
| 8 | # the Free Software Foundation; either version 2 of the License, or |
|---|
| 9 | # (at your option) any later version. |
|---|
| 10 | # |
|---|
| 11 | # This program is distributed in the hope that it will be useful, |
|---|
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | # GNU General Public License for more details. |
|---|
| 15 | # |
|---|
| 16 | # You should have received a copy of the GNU General Public License |
|---|
| 17 | # along with this program; if not, write to the Free Software |
|---|
| 18 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 19 | |
|---|
| 20 | import re |
|---|
| 21 | from smtplib import SMTP |
|---|
| 22 | from socket import sslerror |
|---|
| 23 | |
|---|
| 24 | class Email(object): |
|---|
| 25 | """This class is intended to provide a simple interface for sending |
|---|
| 26 | Umit emails. The mailing system is going to be used for mailing results |
|---|
| 27 | to users, automatically. The arguments you should care about setting are as follows: |
|---|
| 28 | |
|---|
| 29 | from_addr - The address from which the email should be sent (Required) |
|---|
| 30 | from_passwd - If the mail server doesn't do relay, you should authenticate on it |
|---|
| 31 | to_addr - The email address to where the scan result should be sent (Required) |
|---|
| 32 | server - The address of the SMTP server which will deliver the email (Required) |
|---|
| 33 | localdomain - If you want to send your email using your own domain, set it here. |
|---|
| 34 | msg - The scan result or msg you want to send (Required) |
|---|
| 35 | tls - True if the server require tls. False if not. |
|---|
| 36 | port - The port which the SMTP server is listening |
|---|
| 37 | subject - The subject of the mail (Required) |
|---|
| 38 | """ |
|---|
| 39 | |
|---|
| 40 | def __init__(self): |
|---|
| 41 | self.email_server = None |
|---|
| 42 | self.from_passwd = None |
|---|
| 43 | self.server = "" |
|---|
| 44 | self.localdomain = "" |
|---|
| 45 | self.subject = "" |
|---|
| 46 | self.re_email = re.compile("[a-z0-9_-]+(\.[a-z0-9_-]+)*@[a-z0-9_-]+(\.[a-z0-9_-]+)+") |
|---|
| 47 | |
|---|
| 48 | def sendmail(self): |
|---|
| 49 | self.email_server = SMTP(self.server, self.port, self.localdomain) |
|---|
| 50 | self.email_server.ehlo(self.from_addr) |
|---|
| 51 | if self.tls: |
|---|
| 52 | self.email_server.starttls() |
|---|
| 53 | self.email_server.ehlo(self.from_addr) |
|---|
| 54 | |
|---|
| 55 | if self.from_passwd: |
|---|
| 56 | self.email_server.login(self.from_addr, self.from_passwd) |
|---|
| 57 | |
|---|
| 58 | try: |
|---|
| 59 | self.email_server.sendmail(self.from_addr, self.to_addr, self.msg) |
|---|
| 60 | return True |
|---|
| 61 | except sslerror: |
|---|
| 62 | return True |
|---|
| 63 | except: |
|---|
| 64 | return False |
|---|
| 65 | |
|---|
| 66 | def quit(self): |
|---|
| 67 | self.email_server.quit() |
|---|
| 68 | |
|---|
| 69 | def __verify_email(self, email): |
|---|
| 70 | if self.re_email.match(email): |
|---|
| 71 | return True |
|---|
| 72 | return False |
|---|
| 73 | |
|---|
| 74 | def get_from_addr(self): |
|---|
| 75 | '''From Address: The email from which the message is going to be sent. |
|---|
| 76 | ''' |
|---|
| 77 | return self.__from_addr |
|---|
| 78 | |
|---|
| 79 | def set_from_addr(self, from_addr): |
|---|
| 80 | ''' |
|---|
| 81 | ''' |
|---|
| 82 | assert self.__verify_email(from_addr) |
|---|
| 83 | self.__from_addr = from_addr |
|---|
| 84 | |
|---|
| 85 | def get_to_addr(self): |
|---|
| 86 | ''' |
|---|
| 87 | ''' |
|---|
| 88 | return self.__to_addr |
|---|
| 89 | |
|---|
| 90 | def set_to_addr(self, to_addr): |
|---|
| 91 | ''' |
|---|
| 92 | ''' |
|---|
| 93 | assert self.__verify_email(to_addr) |
|---|
| 94 | self.__to_addr = to_addr |
|---|
| 95 | |
|---|
| 96 | def get_msg(self): |
|---|
| 97 | ''' |
|---|
| 98 | ''' |
|---|
| 99 | return """From: %s\r |
|---|
| 100 | To: %s\r |
|---|
| 101 | Subject: %s\r |
|---|
| 102 | \r |
|---|
| 103 | %s |
|---|
| 104 | \n.\n""" % (self.from_addr, self.to_addr, self.subject, self.__msg) |
|---|
| 105 | |
|---|
| 106 | def set_msg(self, msg): |
|---|
| 107 | ''' |
|---|
| 108 | ''' |
|---|
| 109 | self.__msg = msg |
|---|
| 110 | |
|---|
| 111 | def get_tls(self): |
|---|
| 112 | ''' |
|---|
| 113 | ''' |
|---|
| 114 | if type(self.__tls) == type(True): |
|---|
| 115 | return self.__tls |
|---|
| 116 | return False |
|---|
| 117 | |
|---|
| 118 | def set_tls(self, tls): |
|---|
| 119 | ''' |
|---|
| 120 | ''' |
|---|
| 121 | if type(tls) == type(True): |
|---|
| 122 | self.__tls = tls |
|---|
| 123 | else: |
|---|
| 124 | raise Exception("TLS should be True or False!") |
|---|
| 125 | |
|---|
| 126 | def get_port(self): |
|---|
| 127 | ''' |
|---|
| 128 | ''' |
|---|
| 129 | if self.__port != None: |
|---|
| 130 | return self.__port |
|---|
| 131 | return 0 |
|---|
| 132 | |
|---|
| 133 | def set_port(self, port): |
|---|
| 134 | ''' |
|---|
| 135 | ''' |
|---|
| 136 | self.__port = port |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | from_addr = property(get_from_addr, set_from_addr) |
|---|
| 140 | to_addr = property(get_to_addr, set_to_addr) |
|---|
| 141 | msg = property(get_msg, set_msg) |
|---|
| 142 | tls = property(get_tls, set_tls) |
|---|
| 143 | port = property(get_port, set_port) |
|---|
| 144 | |
|---|
| 145 | __from_addr = None |
|---|
| 146 | __to_addr = None |
|---|
| 147 | __msg = None |
|---|
| 148 | __tls = None |
|---|
| 149 | __port = None |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | if __name__ == "__main__": |
|---|
| 153 | msg = '''From: yourgmailhere@gmail.com\r |
|---|
| 154 | To: targetgmailhere@gmail.com\r |
|---|
| 155 | Subject: Farting a lot!\r |
|---|
| 156 | \r |
|---|
| 157 | |
|---|
| 158 | Gotcha! |
|---|
| 159 | |
|---|
| 160 | \n.\n''' |
|---|
| 161 | |
|---|
| 162 | email = SMTP("smtp.gmail.com") |
|---|
| 163 | email.ehlo("yourgmailhere@gmail.com") |
|---|
| 164 | email.starttls() |
|---|
| 165 | email.ehlo("yourgmailhere@gmail.com") |
|---|
| 166 | email.login("yourgmailhere@gmail.com", "yourpasswdhere") |
|---|
| 167 | email.sendmail("yourgmailhere@gmail.com", "targetgmailhere@gmail.com", msg) |
|---|
| 168 | email.quit() |
|---|
| 169 | |
|---|