elif request["type"] == "send-email": try: message = MIMEText(request["message"]) for key, val in request["headers"].items(): message[key] = val message["From"] = formataddr( (request["from"]["name"], request["from"]["email"])) message["Message-Id"] = make_msgid() smtp = smtplib.SMTP(host=smtp_host, port=smtp_port) smtp.starttls() smtp.login(smtp_login, smtp_passwd) smtp.send_message(message) smtp.quit() imap_client.append("Sent", message.as_string()) contacts_file = open( os.path.dirname(sys.argv[0]) + "/.contacts", "a") contacts_file.write(request["headers"]["To"] + "\n") contacts_file.close() response = dict(success=True, type="send-email") except Exception as error: response = dict(success=False, type="send-email", error=str(error)) elif request["type"] == "extract-contacts": try: contacts = get_contacts() contacts_file = open( os.path.dirname(sys.argv[0]) + "/.contacts", "w+")
try: from_name = request['headers']['from-name'] from_email = request['headers']['from-email'] message = MIMEText(request['message']) message['From'] = formataddr((from_name, from_email)) message['To'] = request['headers']['to'] message['Subject'] = Header(request['headers']['subject']) message['Date'] = formatdate(localtime=True) message['Message-Id'] = make_msgid() if 'cc' in request: message['CC'] = request['headers']['cc'] if 'bcc' in request: message['BCC'] = request['headers']['bcc'] logging.info(message) smtp = smtplib.SMTP(host=_smtp_host, port=_smtp_port) smtp.starttls() smtp.login(_smtp_user, _smtp_pass) smtp.send_message(message) smtp.quit() _imap.append('Sent', message.as_string()) response = dict(success=True, type='send-email') except Exception as error: response = dict(success=False, type='send-email', error=str(error)) logging.info(json.dumps(response)) sys.stdout.write(json.dumps(response) + "\n") sys.stdout.flush()