def new_post(email):

	title = email['subject'].replace("Post: ", '')
	body = None
	if email.get_content_maintype() == 'multipart':
		for part in email.walk():
			if part.get_content_maintype() == 'multipart':
				pass
			elif part.get_content_maintype() == 'text':
				if body: continue
				body = part.get_payload(None, True)
			elif part.get_content_maintype() == 'image':
				filename = part.get_filename()
				with open(os.path.join('../content/images', filename), 'wb') as img:
					img.write(part.get_payload(decode=True))
				mdfilename = '![ ](images/%s)' % filename

	return update_pelican.post(title, body, mdfilename)
Example #2
0
def poll(verbose = True):
    global mail_password
    if mail_password == None:
        mail_password = get_password()

    # Read program config file for common variable values
    try:
        mailfetch_config = config.read_config()
        servername = mailfetch_config["Mailfetch"]["server"]
        portnumber = mailfetch_config["Mailfetch"]["port"]
        username = mailfetch_config["Mailfetch"]["username"]
        mailbox = mailfetch_config["Mailfetch"]["mailbox"]
        savedir = mailfetch_config["Mailfetch"]["savedir"]
        allowedtypes = mailfetch_config["Mailfetch"]["extensions"]
    except:
        mlogger.log("Mailfetch configuration error")
        print("Exiting.")
        return -1

    # Try to open a connection to the email server.
    # Using a method from the socket library, I am temporarily
    # imposing a timeout restriction to keep the program from
    # hanging on an invalid mailserver name.
    # After the connection, timeout must be reset to NONE to
    # place the socket back in blocking mode.
    socket.setdefaulttimeout(10)
    try:
        connection = open_connection(servername,portnumber)
    except:
        mlogger.log("Mailfetch failed to open connection to",servername,":",portnumber)
        print("Exiting.")
        return -2
    socket.setdefaulttimeout(None)

    # If a socket is opened successfully, try to login to the server
    try:
        login(connection,username,mail_password,mailbox)
    except:
        mlogger.log("Mailfetch failed to login to",servername,":",portnumber)
        print("Exiting.")
        return -3

    # Populate a list with parsed email info
    infolist = []
    messagelist = get_message_list(connection)
    for message in messagelist:
        email = get_message_contents(connection,message)

        # If the email is not multipart, don't process further.
        # Only multipart messages have attachments.
        if email.get_content_maintype() != 'multipart':
            continue

        # Iterate over the parts of an individual message
        for part in email.walk():
            # An attachment itself is NOT multipart
            if part.get_content_maintype() == 'multipart':
                continue

            # A condition where no attachment is to be found
            if part.get('Content-Disposition') is None:
                continue

            # Only proceed if the attachment extension matches those allowed
            filename = part.get_filename()
            extension = filename[-4:-1]
            if extension not in allowedtypes:
                continue

            # If we make it this far, extract and save the attachment
            path = extract_attachment(part,savedir)
            
            # put all the information here
            sendaddr = get_sender_addr(connection,message)
            jobinfo = pipeline.PrintJob(path,sendaddr)
            infolist.append(jobinfo)

    # Close connections and return list of job request info
    connection.close()
    connection.logout()
    return infolist
Example #3
-1
    def _get_content(self, email):
        """Get the body content of an email.

        :param: email (object) the email object to extract the content from.

        :return: (string) body of the message.

        """
        # get the body content of the email
        maintype = email.get_content_maintype()
        if maintype == 'multipart':
            for part in email.get_payload():
                if part.get_content_maintype() == 'text':
                    return part.get_payload()
        elif maintype == 'text':
            return email.get_payload()