'Test attaching a file against different mimetypes and make sure that
a file will be attached and sent properly even if an invalid mimetype
is specified.'

def test_attach_file(self):

    files = (('file.txt', 'text/plain'), ('file.png', 'image/png'), ('file_txt', None), ('file_png', None), ('file_txt.png', 'image/png'), ('file_png.txt', 'text/plain'), ('file.eml', 'message/rfc822'))
    test_mimetypes = ['text/plain', 'image/png', None]
    for (basename, real_mimetype) in files:
        for mimetype in test_mimetypes:
            email = EmailMessage('subject', 'body', 'from@example.com', ['to@example.com'])
            self.assertEqual(mimetypes.guess_type(basename)[0], real_mimetype)
            self.assertEqual(email.attachments, [])
            file_path = os.path.join(os.path.dirname(__file__), 'attachments', basename)
            email.attach_file(file_path, mimetype=mimetype)
            self.assertEqual(len(email.attachments), 1)
            self.assertIn(basename, email.attachments[0])
            msgs_sent_num = email.send()
            self.assertEqual(msgs_sent_num, 1)
