Example #1
0
def make_message(email):
    """ makes a python email.message.Message from our Email object """

    if email.body:
        # yes, yes, it's very ugly. It plays nice with content types such as: generic_type/specific_type; other_data
        specific_type = email.headers.get('Content-Type').split('/',1)[1].split(';',1)[0]
        msg = MIMEText(email.body, specific_type, 'utf-8')
    else:
    # this needs to go into a separate function
        msg = MIMEMultipart('alternative')
        # looks to see if a HTML and plaintext is there
        attachments = []
        for attachment in email.attachments.all():
            if attachment.content_type in ['text/plain', 'text/html']:
                attachments.append(attachment)

        for attachment in attachments:
            try:
                gen_type, specific_type = attachment.content_type.split('/', 1)
            except ValueError:
                gen_type, specific_type = 'application', 'octet-stream'
            msg.attach(MIMEText(attachment.data, specific_type))

        # okay now deal with other attachments
        for attachment in email.attachments.all():
            if attachment in attachments:
                continue # we've already handled it
            # right now deal with it
            try:
                gen_type, specific_type = attachment.content_type.split('/', 1)
            except (AttributeError, IndexError):
                gen_type, specific_type = 'application', 'octet-stream' # generic
            if gen_type == 'audio':
                attach = MIMEAudio(attachment.data, specific_type)
            elif gen_type == 'image':
                attach = MIMEImage(attachment.data, specific_type)
            elif gen_type == 'text':
                attach = MIMEText(attachment.data, specific_type, 'utf-8')
            else:
                attach = MIMEBase(gen_type, specific_type)
                attach.set_payload(attachment.data)
                encoders.encode_base64(attach)

            attach.add_header('Content-Disposition', 'attachment', filename=attachment.content_disposition)
            msg.attach(attach)

    # now add the headers
    for header in email.headers.all():
        msg[header.name] = header.data

    return msg
Example #2
0
def send_mail_core(mail_to_list, directory):
    me = "SHARER" + "<" + mail_user + "@" + mail_postfix + ">"
    msg = MIMEMultipart()
    msg["Subject"] = "audio"
    msg["From"] = me
    msg["To"] = ";".join(mail_to_list)
    msg.preamble = 'You will not see this in a MIME-aware mail reader.\n'

    i = 0
    for filename in os.listdir(directory):
        i += 1
        path = os.path.join(directory, filename)
        if not os.path.isfile(path):
            continue

        ctype, encoding = mimetypes.guess_type(path)
        if ctype is None or encoding is not None:
            ctype = 'application/octet-stream'
        maintype, subtype = ctype.split('/', 1)
        if maintype == 'video':
            fp = open(path, 'rb')
            outer = MIMEAudio(fp.read(), _subtype=subtype)
            fp.close()
        else:
            fp = open(path, 'rb')
            outer = MIMEBase(maintype, subtype)
            outer.set_payload(fp.read())
            fp.close()
        outer.add_header('Content-Disposition',
                         'attachment',
                         filename=filename)
        msg.attach(outer)
        #删除发送过的文件
        cmd = "rm -f %s" % path
        os.system(cmd)
        if i >= count:
            break
    composed = msg.as_string()

    try:
        s = smtplib.SMTP_SSL()
        s.connect(mail_host + ':' + str(mail_host_port))
        s.login(mail_user, mail_pass)
        s.sendmail(me, mail_to_list, composed)
        s.close()
        return True
    except Exception as e:
        print(str(e))
        return False
Example #3
0
def send(event):

    #with open(path, 'r+') as password_list:
    sms_list = open(path_file_entry.get(), 'r+')
    email_list = sms_list.read().split('\n')
    try:
        msg = MIMEMultipart()
        msg['From'] = login_entry.get()
        msg['To'] = ", ".join(email_list)
        msg['Subject'] = "Test Message"
        msg.attach(MIMEText(text_message_entry.get(), 'plain'))
        fp = open(path_media, "rb")
        name = os.path.basename(path_media)
        to_attach = MIMEAudio("application", "octet-stream")
        to_attach.set_payload(fp.read())
        encode_base64(to_attach)
        to_attach.add_header("Content-Disposition",
                             "attachment",
                             filename=name)
        fp.close()
        msg.attach(to_attach)
        server = smtplib.SMTP(server_entry.get(), int(port_entry.get()))
        server.starttls()
        server.login(msg['From'], password_entry.get())
        server.sendmail(msg['From'], email_list, msg.as_string())
        server.quit()
    except:
        result_label['text'] = "Что-то пошло не так"
    else:
        result_label['text'] = "Все ок"
        output = open(pathDec, 'w')
        output.write(server_entry.get() + '\n' + port_entry.get() + '\n' +
                     login_entry.get() + '\n' + password_entry.get() + '\n' +
                     path_file_entry.get() + '\n' + msg['To'] + '\n' +
                     result_label['text'])
        output.close()