Files
cffInvoice/sendEmail.py

194 lines
7.5 KiB
Python

import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from datetime import datetime
import InvoiceDatabase
def send_email(test, record):
port = 25
smtp_server = "gll-com.mail.protection.outlook.com"
orders = InvoiceDatabase.just_work()
for each in orders:
info = InvoiceDatabase.record_lookup(each)
OrderNumber = info[0]
OrderDate = info[1]
UserLogon = info[2]
Name = info[3] + ' ' + info[4]
ChargeCode = info[5]
InvoiceNumber = info[6]
InvoiceEmailAddress = info[7]
ShippingCharges = info[16]
BalanceDue = info[17]
InvoiceDate = datetime.today().strftime('%m/%d/%Y')
#shipping information
ShippingCompany = info[8]
ShippingFirstName = info[9]
ShippingLastName = info[10]
ShippingAddress1 = info[11]
ShippingAddress2 = info[12]
ShippingCity = info[13]
ShippingState = info[14]
ShippingPostalCode = info[15]
# if ShippingCompany != 'None' and ShippingAddress2 == 'None':
block = '''
<br>{name}
<br>{ShippingAddress1}
<br>{ShippingCity}, {ShippingState} {ShippingZip}
<br>
</p>
</td>
'''
addressBlock = block.format(name=Name, ShippingAddress1=ShippingAddress1, ShippingCity=ShippingCity,
ShippingState=ShippingState, ShippingZip=ShippingPostalCode)
itemDetailBlock = '''
<TABLE style=""BORDER-TOP: #838282 1px solid; BORDER-RIGHT: #838282 1px solid; WIDTH: 600px;
BORDER-COLLAPSE: collapse; BORDER-BOTTOM: #838282 1px solid; COLOR: black;
BORDER-LEFT: #838282 1px solid; MARGIN-TOP: 10px"" borderColor=#838282 cellSpacing=0 cellPadding=5
rules=all border=1><TBODY>
<TR><TD style=""FONT-WEIGHT: 800""><B>Quantity</B></TD><TD style=""FONT-WEIGHT: 800""><B>Document ID</B></TD>
<TD style=""FONT-WEIGHT: 800""><B>Product Name</B</TD><TD style=""FONT-WEIGHT: 800""><B>Sales</B></TD></TR>
'''
items = InvoiceDatabase.all_items(OrderNumber)
for eachItem in items:
info = InvoiceDatabase.item_lookup(eachItem)
Quantity = info[0]
DocumentID = info[1]
ProductName = info[2]
ItemPrice = info[3]
addItem = '''
<tr>
<td> {Quantity} </td>
<td>{DocumentID}</td>
<td>{ProductName}</td>
<td align=" & Chr(34) & "right" & Chr(34)>
${ItemPrice:,.2f}</td>
</tr>
'''
itemDetailBlock = itemDetailBlock + addItem.format(Quantity=Quantity, DocumentID=DocumentID,
ProductName=ProductName, ItemPrice=ItemPrice)
addItem = '''
<TR><TD colSpan=9 align=right>Shipping:&nbsp; <SPAN contentEditable=false style=""BACKGROUND-COLOR: transparent"">
${ShippingCharges:,.2f} </SPAN></TD></TR><TR><TD colSpan=9 align=right>Invoice Total
<SPAN contentEditable=false style=""BACKGROUND-COLOR: transparent""> ${BalanceDue:,.2f}
</SPAN></TD></TR></TBODY></TABLE>
'''
itemDetailBlock = itemDetailBlock + addItem.format(ShippingCharges=ShippingCharges, BalanceDue=BalanceDue)
body = '''
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>CFF Order Invoice</TITLE>
<META content="text/html; charset=utf-8" http-equiv=Content-Type>
<META name=GENERATOR content="MSHTML 11.00.9600.18525">
<META name=Author content="">
<META name=Keywords content="">
<META name=Description content="">
<STYLE type=text/css>
p,
span,
a,
table,
td,
input,
textarea,
select,
option {{
font-family: Verdana, Tahoma, Geneva, Arial, Helvetica, sans-serif;
font-weight: normal;
font-size: 13px;
margin: 0;
}}
</STYLE>
</HEAD>
<BODY>
<TABLE style="BORDER-COLLAPSE: collapse" width="600">
<TR>
<TD style="BORDER-BOTTOM: #000000 1px solid" width="300"><IMG src="http://cff.gli.us.com/store/custom/images/GLIHeader.png"></TD>
<TD style="BORDER-BOTTOM: #000000 1px solid" width="300" align="right"><P style="FONT-SIZE: 18px; FONT-WEIGHT: normal; MARGIN-TOP: 10px"><I>INVOICE</I></P></TD>
</TR>
<TR vAlign="top">
<TD style="VERTICAL-ALIGN: top" vAlign="top" width="300"><BR><P><B>Shipment Address</B><BR>
{addressBlock}
</TD>
<TD style="VERTICAL-ALIGN: top" vAlign="top" width="300">&nbsp;<BR><P><B>Make Checks Payable To:</B> <BR>Great Lakes Integrated <BR>4246 Hudson Dr.<BR>Stow, Ohio 44224<BR>&nbsp;</P></TD>
</TR>
<TR>
<TD style="VERTICAL-ALIGN: top" vAlign="top" width="300"><BR></TD>
</TR>
<TR vAlign="top">
<TD style="VERTICAL-ALIGN: top" vAlign="top" width="300">
<P>
<B>Order Number:</B> {OrderNumber}&nbsp;
<BR><B>Order Date:</B> {OrderDate}&nbsp;
<BR><B>User Logon:</B> {UserLogon}&nbsp;
<BR><B>Name:</B> {Name}&nbsp;
<BR><B>Charge Code:</B> {ChargeCode}&nbsp;
</P>
</TD>
<TD style="VERTICAL-ALIGN: top" vAlign="top" width="300">
<P>
<B>Invoice Number:</B> {InvoiceNumber}&nbsp;
<BR><B>Invoice Date:</B> {InvoiceDate}&nbsp;
<BR><B>Customer Number:</B> 1925&nbsp;
<BR><B>Terms:</B>Due in 30 days&nbsp;
</P>
</TD>
</TR>
<TR>
<TD width="600" colSpan="2"><P style="MARGIN-TOP: 20px"><B>The order consists of the following items:</B> </P>
<SPAN contentEditable=false style="" PFVar="OrderDetail"></SPAN>
{itemDetailBlock}
</TD>
</TR>
</TBODY>
</TABLE>
</BODY>
</HTML>
'''
HTMLpart = body.format(addressBlock=addressBlock, OrderNumber=OrderNumber, OrderDate=OrderDate, UserLogon=UserLogon, Name=Name,
ChargeCode=ChargeCode, InvoiceDate=InvoiceDate, InvoiceNumber=InvoiceNumber, itemDetailBlock=itemDetailBlock)
bodyHTML = MIMEText(HTMLpart, "html")
#html output for testing
f = open("sample.html", 'w')
f.write(HTMLpart)
f.close()
msg = MIMEMultipart()
msg['Subject'] = 'INVOICE ' + InvoiceNumber
if test is True:
msg['To'] = 'ddembinski@gll.com'
msg['Cc'] = 'ddembinski@gll.com'
msg['Bcc'] = 'ddembinski@gll.com'
else:
msg['To'] = InvoiceEmailAddress
msg['From'] = 'ddembinski@gll.com'
msg.add_header('Content-Type', 'text/html')
msg.attach(bodyHTML)
try:
with smtplib.SMTP(smtp_server, port, timeout=120) as server:
server.sendmail(msg['From'], msg['To'], msg.as_string())
except Exception as e:
print(e)