Reputation: 5
Do you know or have an example of how to create and send emails with Jinja2 and Premailer as a Scrapy extension?
If I shouldn't use them with Scrapy, what other HTML templating solution do you recommend to send advanced emails?
Upvotes: 0
Views: 433
Reputation:
You definitely can send emails with Scrapy
and jinja2
. We do it all the time to be alerted from our scrapers. We use mandrill to send out our emails but you can use any other SMTP providers out there to send out your emails. Also you can extend this code to implement premailer
into the template.
import requests
from scrapy import signals
from jinja2 import Environment, PackageLoader
class EmailExt(object):
"""
Email extension for scrapy
"""
@classmethod
def from_crawler(cls, crawler):
"""
On `spider_closed` signal call `self.spider_closed()`
"""
ext = cls()
crawler.signals.connect(ext.spider_closed,
signal=signals.spider_closed)
return ext
def spider_closed(self, spider, reason):
#initialize `PackageLoader` with the directory to look for HTML templates
env = Environment(loader=PackageLoader('templates', 'emails'))
template = env.get_template("email-template.html")
# render template with the custom variables in your jinja2 template
html = template.render(title="Email from scraper", count=10)
# send the email using the mandrill API
requests.post('https://api.mailgun.net/v3/yourcompany/messages',
auth=('api', 'API-KEY-FOR-MANDRILL'),
data={'from': '[email protected]',
'to': '[email protected]',
'subject': 'Email from scraper',
'html': html})
Upvotes: 1