You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在WooCommerce中实现商品售出时向卖家发送自定义邮件

Hey there! Let's walk through how to set up that post-sale email notification for your sellers—super straightforward once you break it down.

Core Implementation Steps

1. Nail Down the Trigger Timing

  • The email should only fire when an order's status gets updated to "sold" or "completed". This logic has to live on your backend—never rely on frontend triggers, since that's easy to exploit.
  • For example: Right after your backend confirms the buyer's payment and marks the order as finalized, that's where you'll hook in the email send function.

2. Craft a Clear Email Template

Sellers care about specific details, so make sure your email includes:

  • The sold product's name and ID (helps them quickly identify it)
  • Basic buyer info (e.g., nickname, contact—just make sure you're following privacy rules here)
  • Order amount and payment time
  • Next steps (like "ship soon" or a link to view the full order details)

Here's a simple HTML template you can tweak:

<div style="max-width:600px;margin:0 auto;padding:20px;border:1px solid #eee;border-radius:8px;">
  <h3 style="color:#2c3e50;">Hi there, your product has been sold!</h3>
  <p><strong>Product Name:</strong> {{product_name}}</p>
  <p><strong>Order ID:</strong> {{order_id}}</p>
  <p><strong>Buyer:</strong> {{buyer_nickname}}</p>
  <p><strong>Order Total:</strong> ¥{{order_amount}}</p>
  <p>Please process the shipment as soon as possible. View full order details: <a href="{{order_detail_url}}" style="color:#3498db;">Click here</a></p>
</div>

3. Build the Backend Email Logic

You'll need to use an email service (SMTP server or third-party tools like SendGrid/Aliyun Email Push). Below's a Python Flask example—adjust it to your backend language:

import smtplib
from email.mime.text import MIMEText
from email.header import Header

def send_sale_alert(seller_email, product_data, order_data):
    # Populate the template with real data
    email_html = f"""
    <div style="max-width:600px;margin:0 auto;padding:20px;border:1px solid #eee;border-radius:8px;">
      <h3 style="color:#2c3e50;">Hi there, your product has been sold!</h3>
      <p><strong>Product Name:</strong> {product_data['name']}</p>
      <p><strong>Order ID:</strong> {order_data['id']}</p>
      <p><strong>Buyer:</strong> {order_data['buyer_nick']}</p>
      <p><strong>Order Total:</strong> ¥{order_data['total_amount']}</p>
      <p>Please process the shipment as soon as possible. View full order details: /orders/{order_data['id']}</p>
    </div>
    """

    # SMTP configuration (replace with your own)
    smtp_host = 'smtp.yourdomain.com'
    smtp_port = 465
    sender_email = 'noreply@yourdomain.com'
    sender_password = 'your_email_auth_password'

    # Assemble the email
    msg = MIMEText(email_html, 'html', 'utf-8')
    msg['From'] = Header('Your Website Name', 'utf-8')
    msg['To'] = Header(seller_email, 'utf-8')
    msg['Subject'] = Header(f"Alert: Your product [{product_data['name']}] is sold!", 'utf-8')

    # Send the email with error handling
    try:
        with smtplib.SMTP_SSL(smtp_host, smtp_port) as server:
            server.login(sender_email, sender_password)
            server.sendmail(sender_email, seller_email, msg.as_string())
        print("Sale notification email sent successfully")
        return True
    except Exception as e:
        print(f"Failed to send email: {str(e)}")
        return False
  • When to call this function: After your backend updates the order status to "sold", fetch the seller's email from your database, pass in the product and order data, and run this function.

4. Critical Things to Remember

  • Security first: Never handle email sending on the frontend—exposing your email credentials there is a huge risk. All logic stays server-side.
  • Retry for failures: Emails can fail due to network issues. Add a retry mechanism, or use a message queue (like Redis/RabbitMQ) to queue failed sends and retry later.
  • Compliance: Make sure sellers opt in to these notifications (e.g., a checkbox during registration or product listing) to follow privacy laws like GDPR or CCPA.
  • Test thoroughly: Send test emails to your own address first to check formatting, timing, and content before rolling out to real sellers.

内容的提问来源于stack exchange,提问作者Tcmxc

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:31:45