【问题标题】:Attaching a .png file to an email sent by satchmo将 .png 文件附加到 satchmo 发送的电子邮件
【发布时间】:2012-04-14 09:14:38
【问题描述】:

在 Satchmo 商店中,我需要在 django 在完成订单时发送的电子邮件中附加一个小的 .png(条形码)。使用调用 send_store_mail() 的 send_order_confirmation() 将电子邮件格式化为 HTML 格式(都是 satchmo 的一部分。)这些函数都没有提供附加文件的能力(我认为)所以我应该重写它们吗?我想知道是否有可能/更好地使用信号来做到这一点。也许 rendering_store_mail() ?

顺便说一句,条形码是动态生成的,因此无法在某处的服务器上找到文件的链接。

非常感谢, 托马斯

【问题讨论】:

    标签: django satchmo


    【解决方案1】:

    好吧,我也不得不在确认电子邮件中添加额外的信息,不过只有文本。因此,这将是使用信号向电子邮件添加额外内容的非常简单的方法,恕我直言,这是最好的方法。如果可以避免覆盖 satchmo-core,请始终使用信号 ;-)

    1. 定义你的监听器来为渲染添加一些上下文。在这种情况下,我将添加一个额外注释字段的内容,以及该订单的条形码,假设有一个名为 get_barcode_img(<order>) 的函数到上下文中。我在这里也假设get_barcode_img 函数不仅会返回一个 png,而且会返回类似于 MIMEImage(如from email.MIMEImage import MIMEImage)的东西,以便能够将其包含在内。此外,可能还需要更多信息,例如 img 的 MIME 标头。

      # localsite/payment/listeners.py
      
      def add_extra_context_to_store_mail(sender,
              send_mail_args={}, context={}, **kwargs):
          if not 'order' in context:
              return
          context['barcode_header'] = get_barcode_header(context['order'])
          context['barcode_img'] = get_barcode_img(context['order'])
          context['notes'] = context['order'].notes
      
    2. 将侦听器连接到肯定会“发现”代码的某个地方的信号,例如models.py

      # localsite/payment/models.py 
      
      from satchmo_store.shop.signals import rendering_store_mail, order_notice_sender
      
      rendering_store_mail.connect(payment_listeners.add_extra_context_to_store_mail, sender=order_notice_sender)
      
    3. 在本地覆盖模板(例如order_placed_notice.html)以添加新上下文。请注意您将模板放在哪里,因为路径对于 django 获取新模板而不是 satchmo 的模板至关重要。在这种情况下,从项目的根路径开始,可能有一个模板文件夹,其中的路径必须与 satchmo 文件夹中的路径完全相同。例如。 /templates/shop/email/order_placed_notice.html ...这可以应用于应用程序内的任何“有效”模板文件夹。由您决定模板的组织位置/方式。

      <!-- templates/shop/email/order_placed_notice.html -->
      <!DOCTYPE ...><html>
      <head>
          <!-- include the image-header here somewhere??? -->
          <title></title>
      </head>
      <body>        
      ...
      Your comments:
      {{ notes }}
      
      Barcode:
      {{ barcode_img }}"
      

    【讨论】:

      猜你喜欢
      • 2011-08-14
      • 2012-11-11
      • 2013-03-18
      • 1970-01-01
      • 1970-01-01
      • 2020-06-27
      • 2014-06-25
      • 2018-06-22
      • 1970-01-01
      相关资源
      最近更新 更多