【问题标题】:Trigger an email in Python if SQL Query returns results如果 SQL 查询返回结果,则在 Python 中触发电子邮件
【发布时间】:2023-01-09 19:47:25
【问题描述】:

我已将 SQL 查询连接到 Python,并尝试在 SQL 查询返回结果时自动发送带有附件的电子邮件。查询将显示我们的价格与客户销售我们产品的价格之间的差异。

此查询每天跟踪客户的数据。如果客户从我们这里购买,我想将查询结果的附件通过电子邮件发送给我的主管,但是,如果 客户在过去 24 小时内没有进行任何购买,那么我不希望发送空电子邮件。

我四处浏览寻找答案并尝试了不同的代码,但没有找到任何解决方案。对此的任何帮助将不胜感激。

sql_query = pd.read_sql_query('''
SELECT SaleH.[Order No], SaleH.[Reference], SaleL.[Product code], SaleL.[Description], SaleL.[Quantity], SaleL.[Unit Price], SaleP.[Unit Price] AS 'Selling Price'
FROM [Sales Header] SaleH
INNER JOIN [Sales Line] SaleL ON SaleH.[Order No] = SaleL.[Order No] 
LEFT JOIN [Sales Price] SaleP ON SaleP.[Product Code] = SaleL.[Product Code] AND SaleH.[Customer No] = SaleP.[Customer No]
WHERE SaleH.[Customer No] = 'Cust01' AND SaleH.[Date] >DATEADD(DD,-1,getdate()) AND SaleP.[Unit     Price] != SaleL.[Unit Price]
''' ,conn) 
df = pd.DataFrame(sql_query)
df.to_csv (r'G:\Customer Folder\Customer_Sales_Orders.csv', index = False) 
outlook = win32. Dispatch('outlook.application')
email = outlook.CreateItem(0)
mail_from = "Sender"
mail_to = "Recipient"
mail_subject = "Customer Sales Orders"            
mail_attachment = 'Customer_Sales_Orders.csv'
mail_attachment_name = "Customer_Sales_Orders" +'.csv'
f = open(r'G:\Customer Folder\Customer_Sales_Orders.csv')
content = f.read()
    #if Customer_Sales_Orders results > 0:
        #sendemail
    #else:
        #pass

【问题讨论】:

  • 你说你没有找到解决办法,但你没有说明实际问题是什么。您在发送带有 CSV 附件的电子邮件时遇到困难还是其他原因?

标签: python sql


【解决方案1】:

这将做:

results = pd.read_csv('Data.csv') # count no. of lines
if len(results))> 0:
    #sendemail
    from_address = "<from which email>>"
  to_address = "<<to which email>>"
 
 # Create message container - the correct MIME type is multipart/alternative.
  msg = MIMEMultipart('alternative')
  msg['Subject'] = "Customer ha made purchases in the last 24 hours for  
  {}".format(date.today())
  msg['From'] = from_address
  msg['To'] = to_address
  
  # Create the message (HTML).
  html = """
  This is an automated email.
  
  We are sending an email using Python and Gmail, how fun! We can fill this with html, and gmail supports a decent range of css style attributes too - https://developers.google.com/gmail/design/css#example.
   """
  
   # Record the MIME type - text/html.
  part1 = MIMEText(html, 'html')
  
   # Attach parts into message container
  msg.attach(part1)
  
   # Credentials
  username = '<your email id>'  
  password = '<google_app_password>'
  
   # Sending the email
   ## note - this smtp config worked for me, I found it googling around, you may have to tweak the # (587) to get yours to work
  server = smtplib.SMTP('smtp.gmail.com', 587) 
  server.ehlo()
  server.starttls()
  server.login(username,password)  
  server.sendmail(from_address, to_address, msg.as_string())  
  server.quit()

【讨论】:

    猜你喜欢
    • 2020-04-06
    • 1970-01-01
    • 2021-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多