如何在Scrapy Spidermon中使用Mandrill发送邮件报告?能否替代SES?
Absolutely! Spidermon’s email notification system is built to be flexible and extensible. While it ships with an SES sender by default, you can easily build a custom email sender that integrates with Mandrill (now part of Mailchimp) to deliver your spider reports. This gives you full control over how notifications are sent, without being locked into SES.
Follow these practical steps to set up Mandrill with your Spidermon workflow:
1. Install the Mandrill Python Library
First, install the official Mandrill client (or use the newer Mailchimp Marketing API package if you prefer the latest integration):
pip install mandrill # Alternative for newer Mailchimp Marketing API (supports Mandrill) pip install mailchimp-marketing
2. Create a Custom Mandrill Email Sender
Make a new file (e.g., mandrill_sender.py) in your Scrapy project, then define a custom sender class that inherits from Spidermon’s base EmailSender. This class will handle the heavy lifting of communicating with Mandrill’s API:
from spidermon.contrib.actions.email.senders import EmailSender import mandrill class MandrillEmailSender(EmailSender): def __init__(self, api_key, **kwargs): super().__init__(**kwargs) self.mandrill_client = mandrill.Mandrill(api_key) def send_message(self, to, subject, body, cc=None, bcc=None, attachments=None): # Format recipient list for Mandrill's API requirements recipients = [{"email": email} for email in to] if cc: recipients.extend([{"email": email, "type": "cc"} for email in cc]) if bcc: recipients.extend([{"email": email, "type": "bcc"} for email in bcc]) # Build the message payload message = { "from_email": self.from_email, "from_name": self.from_name, "subject": subject, "html": body, # Swap with "text" if you want plain-text emails "to": recipients, "important": True, } # Add attachments if provided if attachments: message["attachments"] = [] for attachment in attachments: message["attachments"].append({ "type": attachment["mimetype"], "name": attachment["filename"], "content": attachment["content"], }) # Send the email via Mandrill try: self.mandrill_client.messages.send(message=message) self.logger.info(f"Successfully sent email via Mandrill to {to}") except mandrill.Error as e: self.logger.error(f"Mandrill API error occurred: {e.__str__()}") raise
3. Configure Spidermon in Your Scrapy Settings
Update your settings.py to enable Spidermon, link your custom Mandrill sender, and set up notification rules:
# Enable Spidermon SPIDERMON_ENABLED = True # Mandrill Sender Configuration SPIDERMON_EMAIL_SENDER = "your_project_name.mandrill_sender.MandrillEmailSender" SPIDERMON_EMAIL_SENDER_PARAMS = { "api_key": "YOUR_MANDRILL_API_KEY", # Grab this from your Mailchimp/Mandrill dashboard "from_email": "spider-alerts@yourdomain.com", "from_name": "Scrapy Spider Monitor", } # Email Notification Rules SPIDERMON_NOTIFICATIONS = { "MONITOR": { "recipients": ["your-team@yourdomain.com"], "send_when": "on_failure", # Options: on_success, on_failure, always }, "PERIODIC_MONITORS": { "recipients": ["your-team@yourdomain.com"], "send_when": "on_failure", }, } # Bind notifications to spider events SPIDERMON_ACTIONS = [ "spidermon.contrib.actions.email.EmailNotification", ]
4. Test the Setup
Run your spider and trigger a failure (or set send_when to "always") to confirm emails are being sent via Mandrill. If you run into issues, check your Mandrill dashboard for delivery logs or API error details.
Quick Notes:
- Ensure your Mandrill API key has permission to send emails from your configured
from_emailaddress. - If using the newer Mailchimp Marketing API, adjust the sender class to use the Mailchimp client instead of the legacy Mandrill library.
- Customize email content by overriding the
get_message_bodymethod in your sender class, or use Spidermon’s built-in template system for more dynamic reports.
内容的提问来源于stack exchange,提问作者Aminah Nuraini

