question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Scrapy mail.send error

See original GitHub issue

Hi, I’m new to scrapy and I want to send some emails after the spider closed. But I got some errors, anyone know ? I’m using python2.7 and scrapy 1.5.1. Here are my codes:

class AlertSpider(scrapy.Spider):
    name = "alert"
    start_urls = ['http://www.test.com']
    mails = []

    def parse(self, response):
        # Do something work

    @classmethod
    def from_crawler(cls, crawler):
        spider = cls()
        crawler.signals.connect(spider.spider_closed, signals.spider_closed)
        return spider

    def spider_closed(self, spider):
        settings = get_project_settings()
        mailer = MailSender.from_settings(settings)
       # first e-mail
        mailer.send(to=["xxxx@gmail.com"], subject='subject1', body='body1')
       # second e-mail
        return mailer.send(to=["xxxx@gmail.com"], subject='subject2', body='body2')

I want to send two e-mails after the spider close, but I get below errors: (By the way, there is no problem if I just send one e-mail)

File "C:\Software\Python27\lib\site-packages\twisted\internet\selectreactor.py", line 149, in _doReadOrWrite why = getattr(selectable, method)() File "C:\Software\Python27\lib\site-packages\twisted\internet\tcp.py", line 243, in doRead return self._dataReceived(data) File "C:\Software\Python27\lib\site-packages\twisted\internet\tcp.py", line 249, in _dataReceived rval = self.protocol.dataReceived(data) File "C:\Software\Python27\lib\site-packages\twisted\protocols\tls.py", line 330, in dataReceived self._flushReceiveBIO() File "C:\Software\Python27\lib\site-packages\twisted\protocols\tls.py", line 300, in _flushReceiveBIO self._flushSendBIO() File "C:\Software\Python27\lib\site-packages\twisted\protocols\tls.py", line 252, in _flushSendBIO bytes = self._tlsConnection.bio_read(2 ** 15) exceptions.AttributeError: 'NoneType' object has no attribute 'bio_read'

It seems to the twisted doesn’t close the io, but I don’t find any close method in MailSender class, so anyone have met this error?

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:14

github_iconTop GitHub Comments

5reactions
111qqzcommented, Nov 9, 2018

Same problem here. I try to send emails in the “close_spider” method of the pipeline class , because I have serveral spiders and I don’t want to add the “sending email” code serveral times. After I repalce “mailer.send(…)” with “return mailer.send(…)”,the problem disappered

4reactions
iveneycommented, Nov 22, 2020

I have an email pipeline that sends email during process_item and have the error 'NoneType' object has no attribute 'bio_read', e.g., something like:

def process_item(self, item, spider):
  if meets_criteria:
    mailer.send(...)
  return item

Changing the function to async and use await seems to solve it for me, as mailer.send returns the deferred object.

async def process_item(self, item, spider):
  if meets_criteria:
    await mailer.send(...)
  return item

Not sure if this is the right way to solve but it seems working for me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sending e-mail — Scrapy 2.7.1 documentation
MailSender is the preferred class to use for sending emails from Scrapy, as it uses Twisted non-blocking IO, like the rest of the...
Read more >
How to send error logs on email in Scrapy
I want to send emails with errors, if they occur while Scrapy is working. I've added email handler and connect it to spider.logger:...
Read more >
Unable to send mail with scrapy
I am new to Scrapy and i am trying to send an email. ... [scrapy.mail] ERROR: Unable to send mail: To=[myemail] Cc=[] Subject="This...
Read more >
Import Error--Scrapy. Basespider.
To post to this group, send email to scrapy...@googlegroups.com. To unsubscribe from this group, send email to scrapy-users...@googlegroups.com. For more ...
Read more >
Scrapy - Sending an E-mail
For this MailSender class needs to imported from scrapy and then a dedicated function with correct parameters needs to be called to successfully ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found