def send_mail(server, port, username, password, sender, recipient, subject, message): try: # Connect and login to SMTP server server = smtplib.SMTP(server, port) config = Config() if config.get("mailserver", "auth") != "0": server.login(username, password) # Build email sender = sender.strip() header = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % (sender, recipient, subject) email = header + message.strip() # Send email server.sendmail(sender, recipient, email) server.quit() return True except smtplib.SMTPException, e: print_exc()
def new(self, **kwargs): invoice_no = request.params["invoice_no"] amount = request.params["amount"] po_id = request.params["po_id"] vendor_id = request.params["vendor_id"] user_id = request.params["user_id"] po_amount = PurchaseOrder.get_amount(po_id) if int(amount) != int(po_amount): return ("error", "Invoice amount does not match PO amount.") invoice = Invoice.create(invoice_no, po_id, vendor_id, user_id, amount) warehouse = Warehouse.get(1) # Send email config = Config() server = config.get("mailserver", "server") port = config.get("mailserver", "port") username = config.get("mailserver", "username") password = config.get("mailserver", "password") sender = config.get("email", "sender") recipient = warehouse.manager_email subject = config.get("email", "subject2") message = ( "Hi " + User.get(warehouse.manager).firstname + " " + User.get(warehouse.manager).lastname + ",\r\n\r\n" ) message += "Invoice %s has been received for purchase order %s. %sRegards %s" % ( invoice_no, po_id, "\r\n\r\n", "\r\n\r\n", ) h.send_mail(server, port, username, password, sender, recipient, subject, message) return ("invoice_object", invoice)