######################################################################## This is a rough description of the install procedure. Project is still in an experimental stage! Install script will be provided in the future. This description is just to give you an idea of how things work! Tested on a Raspberry Pi, could run on any Debian based Computer. Try it on: http://h1i1.de/mail2code/ ######################################################################## install fetchmail: sudo apt-get install fetchmail Create a file named .fetchmailrc: nano /home/pi/.fetchmailrc ---------------------- poll imap.1und1.de protocol IMAP #poll pop.1und1.de #protocol POP3 user "Your@Mail.Adress" with password "YourSecretPassword" mda "/home/pi/myfetchmailparser.sh" folder 'INBOX' fetchlimit 1 #keep ssl ------------------------ .fetchmailrc must have 0700 permissions: chmod 0700 .fetchmailrc mkdir /home/pi/mail mkdir /home/pi/mail/done Create a file named myfetchmailparser.sh: nano /home/pi/myfetchmailparser.sh ------------------------ FilenameUniqueId=$(date +"%Y%m%d_%H%M%S_%N") OutputFile="/home/pi/mail/mail"$FilenameUniqueId echo "" > $OutputFile while read x do #echo $x echo $x >> $OutputFile done ------------------------ myfetchmailparser.sh must be executable: chmod a+x myfetchmailparser.sh To make fetchmail run in background edit /etc/default/fetchmail sudo nano /etc/default/fetchmail # Declare here if we want to start fetchmail. 'yes' or 'no' START_DAEMON=yes Now, the fetchmail can be started from a terminal with: sudo /etc/init.d/fetchmail start ------------------------------------------- for file transfer via sftp install sshpass: sudo apt-get install sshpass Create file uploadSFTP.sh: nano uploadSFTP.sh -------------------------------- HOST='YourFtpHostName' USER='YourFtpServerUsername' REMOTEPATH='YourWebserverRootFolder/' file_name='/home/pi/readme.txt' sshpass -p YourSectretFtpPassword sftp $USER@$HOST < -1: is_mail_boundary = True this_is_mail_body = False mail_body_on = False next_empty_line = False print("is mail boundary:" + l) if l[0:3].upper() == "TO:": mail_to = l[3:].strip() print("mail_to: = ", mail_to) if l[0:5].upper() == "FROM:": if mail_from == "": mail_from = l[5:].strip() print("mail_from: = ", mail_from) # Subject contains info where to post the message if l[0:8].upper() == "SUBJECT:": if mail_subject == "": mail_subject = l[8:].strip() h = make_header(decode_header(mail_subject)) mail_subject = str(h) print("mail_subject: = ", mail_subject) if mail_subject.find("_pageId_")> -1: page_id = mail_subject[mail_subject.find("_pageId_")+8:] print("page_id: = ", page_id) page_id = page_id[:page_id.find("_")] print("page_id: = ", page_id) # get path and file configuration id_ok = 0; if page_id == "00000003": this_is_code_only = True print("This Is Code Only!") # Look in main configuration file if page ID of this post exists for p in config_lines: if p.upper().strip().find("PROJECT_FOLDER:") == 0: project_folder = p[p.upper().strip().find("PROJECT_FOLDER:") + 15:].strip() if not project_folder[:1] == "/": project_folder = project_folder + "/" print("project_folder=" + project_folder) # look for page ID if p.upper().strip().find("ID:") == 0: temp_id = p[p.upper().strip().find("ID:") + 3:].strip() print("temp_id: = ", temp_id) if temp_id == page_id: id_ok = 1 mail_is_ok = 1 # mail matches one of the IDs found in main config file else: id_ok = 0 # if subject page id matches an ID found in config file, load folders if id_ok == 1: if p.upper().strip().find("PAGE:") == 0: html_file = p[p.upper().strip().find("PAGE:") + 5:].strip() print("html_file=" + html_file) if p.upper().strip().find("MAIN_FOLDER:") == 0: main_folder = p[p.upper().strip().find("MAIN_FOLDER:") + 12:].strip() + "/" print("main_folder=" + main_folder) if p.upper().strip().find("HEADER_FILE:") == 0: header_file = p[p.upper().strip().find("HEADER_FILE:") + 12:].strip() print("header_file=" + header_file) if p.upper().strip().find("HEADER_FOLDER:") == 0: header_folder = p[p.upper().strip().find("HEADER_FOLDER:") + 14:].strip() + "/" print("header_folder=" + header_folder) if p.upper().strip().find("PICTURES_FOLDER:") == 0: pictures_folder = p[p.upper().strip().find("PICTURES_FOLDER:") + 16:].strip() + "/" print("pictures_folder=" + pictures_folder) if p.upper().strip().find("PREVIEW_FOLDER:") == 0: preview_folder = p[p.upper().strip().find("PREVIEW_FOLDER:") + 15:].strip() + "/" print("preview_folder=" + preview_folder) if p.upper().strip().find("ATTACHMENTS_FOLDER:") == 0: attachments_folder = p[p.upper().strip().find("ATTACHMENTS_FOLDER:") + 19:].strip() + "/" print("attachments_folder=" + attachments_folder) if p.upper().strip().find("STATS_FOLDER:") == 0: stats_folder = p[p.upper().strip().find("STATS_FOLDER:") + 13:].strip() + "/" print("stats_folder=" + stats_folder) if p.upper().strip().find("MAX_POST_NO:") == 0: max_post_no = int(p[p.upper().strip().find("MAX_POST_NO:") + 12:].strip()) print("max_post_no=" + str(max_post_no)) if l[0:5].upper() == "DATE:": if mail_date == "": mail_date = l[5:].strip() print("mail_date: = ", mail_date) if l[0:11].upper() == "MESSAGE-ID:": if mail_id == "": mail_id = l[11:].strip() print("mail_id: = ", mail_id) if ( mail_id.rindex("@") > -1 ): mail_id = mail_id[:mail_id.rindex("@")] # mail_id = mail_id.replace("@", "_at_") mail_id = mail_id + "_" + str(time.time()) + "_" for c in mail_id: if allowed_chars.find(c) == -1: mail_id = mail_id.replace(c, "") print("mail_id: = ", mail_id) if l.upper().find("BOUNDARY") > -1: first_empty_line = 2 temp_string = l[l.upper().find("BOUNDARY")+8:] temp_string = temp_string[temp_string.find("\"")+1:] temp_string = temp_string[0:temp_string.find("\""):].strip() temp_string = temp_string[2:] mail_boundaries.append(temp_string) print("append mail_boundary = ", temp_string) if mail_is_ok == 1: # get current post number if post_no_found == 0: if os.path.isfile(project_folder + main_folder + stats_folder + post_no_file): f = open(project_folder + main_folder + stats_folder + post_no_file, "r") post_no = f.readline() f.close() else: print("Stats file not found:" + project_folder + main_folder + stats_folder + post_no_file) post_no = "0" post_no = str(int(post_no) + 1).zfill(10) f = open(project_folder + main_folder + stats_folder + post_no_file, "w") f.write(post_no) f.close() print("post_no=" + post_no); post_no_found = 1 # Compute encoded mail block if l[0:13].upper() == "CONTENT-TYPE:": # Start with UNKNOWN next_empty_line = False mail_content_type = "UNKNOWN" mail_content_name = l[l.upper().find("NAME=\"")+6:] mail_content_name = mail_content_name[:mail_content_name.upper().find("\"")] if l[0:19].upper() == "CONTENT-TYPE: IMAGE": mail_content_type = "IMAGE" print("mail_content_type = ", mail_content_type) if l[0:19].upper() == "CONTENT-TYPE: TEXT/": if l.upper().find("NAME") > -1 and l.upper().find(".INO") > -1: no_mail_body = True mail_body = "" arduino_no += 1 mail_content_type = "ARDUINO" print("mail_content_type Arduino project file = ", mail_content_type) if l[0:24].upper() == "CONTENT-TYPE: TEXT/PLAIN": print("Content line: " + l) mail_content_type = "TEXT/PLAIN" print("mail_content_type = ", mail_content_type) if l.upper().find("NAME") == -1: this_is_mail_body = True print("This is mail body") if l.upper().find("FILENAME=") > -1: attachment_no += 1 mail_attachment_filename = l[l.upper().find("FILENAME")+8:] mail_attachment_filename = mail_attachment_filename[mail_attachment_filename.find("\"")+1:] mail_attachment_filename = mail_attachment_filename[0:mail_attachment_filename.find("\""):].strip() if no_mail_body and mail_attachment_filename.upper().find(".INO") > -1: mail_body = mail_attachment_filename + "
\n
\n" if( mail_attachment_filename.rindex(".") > -1): mail_attachment_filename = str(attachment_no) + mail_attachment_filename[mail_attachment_filename.rindex("."):] else: mail_attachment_filename = str(attachment_no) mail_attachment_filename = mail_attachment_filename.lower() print("mail_attachment_filename = ", mail_attachment_filename) if l[0:26].upper() == "CONTENT-TRANSFER-ENCODING:": mail_content_encoding = l[26:].strip() print("mail_content_encoding = ", mail_content_encoding) # Store mail text if mail_body_on: if not l.strip() == "": strip_mail_body = 0 if l.upper().find("MAIL2POST@H1I1.DE") > -1 and l.upper().find("TO:") > -1: # Strip that Forwarded header mail_body = "" strip_mail_body = 1 if not strip_mail_body == 1 and next_empty_line: mail_body = mail_body + l.rstrip('\n') + "
" + "\n" if this_is_mail_body and not mail_body_on and not no_mail_body: mail_body_on = True mail_body = "" print("mail_body_on is now True WITH boundary") if first_empty_line == 1 and not no_mail_body: mail_body_on = True print("mail_body_on is now True WITHOUT boundary!") # Store attachment(s) if mail_attachment_filename != "" and (mail_content_type == "IMAGE" or mail_content_type == "UNKNOWN" or mail_content_type == "ARDUINO"): if (is_mail_boundary or l.strip() == "" ) and encoding_on: # Encoded data ends here file_ending = picture_base_name[picture_base_name.rfind("."):].upper() print("file_ending=" + file_ending) attachment_file.close() if is_bitmap_types.find(file_ending.upper()) > -1: if not this_is_code_only: write_attachments.append("\n") else: if this_is_code_only: write_attachments.append("\n
Project file: \"" + mail_content_name + "\"
\n" + picture_base_name + "
\n
\n") else: write_attachments.append("\n
Atached file: \"" + mail_content_name + "\"
\n" + picture_base_name + "
\n
\n") print("...attachment link written") # Convert and upload attachments to server if is_bitmap_types.find(file_ending.upper()) > -1: if not this_is_code_only: command_line = "./convert-pictures.sh " + picture_file_name print(command_line) os.system(command_line) command_line = "./uploadSFTP.sh " + picture_file_name + " " + project_folder + main_folder + pictures_folder print(command_line) os.system(command_line) command_line = "./uploadSFTP.sh " + picture_file_name_preview + " " + project_folder + main_folder + preview_folder print(command_line) os.system(command_line) else: command_line = "./uploadSFTP.sh " + project_folder + main_folder + attachments_folder + picture_base_name + " " + project_folder + main_folder + attachments_folder print(command_line) os.system(command_line) if( mail_content_type == "ARDUINO" ) and arduino_no < 2: # Process Arduino project arduino_file_processed = True command_line = "cp " + project_folder + main_folder + attachments_folder + picture_base_name + " Arduino/Arduino.ino " print(command_line) os.system(command_line) command_line = "rm Arduino/Build/* -R" print(command_line) os.system(command_line) command_line = "arduino-cli compile -b arduino:avr:uno Arduino/Arduino.ino --build-path Arduino/Build/ 2>&1 | tee arduino-output.txt" print(command_line) os.system(command_line) f = open("arduino-output.txt", "r") temp_string = f.readlines() f.close() for temp_line in temp_string: mail_body = mail_body + temp_line + "
\n" if os.path.isfile("Arduino/Build/Arduino.ino.hex"): command_line = "./RecordVideo.sh&" print(command_line) os.system(command_line) command_line = "arduino-cli upload -b arduino:avr:uno -p /dev/ttyACM0 -i Arduino/Build/Arduino.ino.hex -v 2>&1 | tee arduino-output.txt" print(command_line) os.system(command_line) #f = open("arduino-output.txt", "r") #temp_string = f.readlines() #f.close() #for temp_line in temp_string: # mail_body = mail_body + temp_line + "
\n" time.sleep(80) # Load Empty sketch to Arduino command_line = "arduino-cli upload -b arduino:avr:uno -p /dev/ttyACM0 -i Arduino/Empty/Arduino.ino.hex -v 2>&1 | tee arduino-output.txt" print(command_line) os.system(command_line) command_line = "mv testvid.mp4 " + picture_base_name + ".mp4" print(command_line) os.system(command_line) command_line = "./uploadSFTP.sh " + picture_base_name + ".mp4" + " " + project_folder + main_folder + attachments_folder print(command_line) os.system(command_line) write_attachments.append("\n
Video file: \"" + mail_content_name + ".mp4\"
\n" + picture_base_name + ".mp4
\n
\n") encoding_on = False mail_attachment_filename = "" encoding_on = False mail_content_type = "" mail_content_encoding = "" encoding_on = False picture_file_name = "" picture_file_name_preview = "" # exit(0) # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! if encoding_on: #print("Writing attachment:", l) attachment_file.write(base64.b64decode(l)) if l.strip() == "" and not encoding_on and mail_attachment_filename != "": # Encoded data starts here print("Writing attachment...") encoding_on = True picture_base_name = mail_id + "_" + mail_attachment_filename file_ending = picture_base_name[picture_base_name.rfind("."):].upper() print("file_ending=" + file_ending) if is_bitmap_types.find(file_ending.upper()) > -1: picture_file_name = project_folder + main_folder + pictures_folder + picture_base_name picture_file_name_preview = project_folder + main_folder + preview_folder + picture_base_name print("Opening File:" + picture_file_name) attachment_file = open(picture_file_name, "wb") else: if mail_content_type == "ARDUINO": picture_base_name = post_no + "_" + str(time.time()) + ".txt" else: picture_base_name = post_no + "_" + str(time.time()) + file_ending print("Opening File:" + project_folder + main_folder + attachments_folder + picture_base_name) attachment_file = open(project_folder + main_folder + attachments_folder + picture_base_name, "wb") if page_id.upper().find("00000003") > -1 and not arduino_file_processed: mail_is_ok = 0 if mail_is_ok == 1: html_file_temp_handle = open("html.tmp", "w") html_file_handle = open(project_folder + main_folder + html_file, "r") # Write html header to index.tmp temp_string = "" f = open(project_folder + main_folder + header_folder + header_file) while temp_string.find("") == -1: temp_string = f.readline() html_file_temp_handle.write(temp_string) # print(temp_string) f.close() html_line = "" # In old html file discard all lines until end of header indicator line while html_line.find("") == -1: html_line = html_file_handle.readline() #print(html_line) html_file_temp_handle.write("" + mail_time_stamp + "
\n") mail_from = mail_from.replace("<", "<") mail_from = mail_from.replace(">", ">") html_file_temp_handle.write(mail_body) # Write image links for w in write_attachments: html_file_temp_handle.write(w) html_file_temp_handle.write("

") html_file_temp_handle.write(post_no + "

") if not this_is_code_only: html_file_temp_handle.write("Response
") html_file_temp_handle.write("
\n

\n\n\n\n\n") print("mail_body = ", mail_body) # Move mail to done folder in case nothing went wrong os.system("mv " + mail_file_name + " " + mail_path_done) # write all remaining lines from old htmp file current_post_no = 0 is_footer = False while html_line.find("") == -1: html_line = html_file_handle.readline() if (html_line.upper().find("
") > -1): is_footer = True html_file_temp_handle.write("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n") if (html_line.upper().find("
") > -1): current_post_no += 1 if (max_post_no > 0 and max_post_no > current_post_no) or is_footer or max_post_no == 0: html_file_temp_handle.write(html_line) html_file_temp_handle.close() html_file_handle.close() os.system("mv html.tmp" + " " + project_folder + main_folder + html_file) command_line = "./uploadSFTP.sh " + project_folder + main_folder + html_file + " " + project_folder + main_folder print(command_line) os.system(command_line) else: # mail_is_ok == 0 print("Mail not processed, move to folder 'error'!") os.system("mv " + mail_file_name + " " + mail_path_error)