Skip to content
Related Articles

Related Articles

Scrapy – Sending an E-mail
  • Last Updated : 16 Mar, 2021

Prerequisites: Scrapy

Scrapy provides its own facility for sending e-mails which is extremely easy to use, and it’s implemented using Twisted non-blocking IO, to avoid interfering with the non-blocking IO of the crawler. This article discusses how mail can be sent using scrapy. 

For this MailSender class needs to imported from scrapy and then a dedicated function with correct parameters needs to be called to successfully send a mail.

Functions used

  • MailSender() is used to setup the mailer.

Syntax:

classscrapy.mail.MailSender(smtphost=None, mailfrom=None, smtpuser=None, smtppass=None, smtpport=None)



Parameters

  • smtphost (str or bytes) – the SMTP host to use for sending the emails.
  • mailfrom (str) – the address used to send emails (in the From: header).
  • smtpuser – the SMTP user.  If omitted, the MAIL_USER setting will be used. If not given, no SMTP authentication is going to be performed..
  • smtppass (str or bytes) – the SMTP pass for authentication.
  • smtpport (int) – the SMTP port to connect to
  • smtptls (bool) – enforce using SMTP STARTTLS
  • smtpssl (bool) – enforce employing a secure SSL connection
  • classmethodfrom_settings() Instantiates a Scrapy settings object, which can respect these Scrapy settings.

Syntax:

classmethodfrom_settings(settings)

Parameters

  • settings (scrapy.settings.Settings object) – the e-mail recipients
  • send() sends email to the given recipients.

Syntax:

send(to, subject, body, cc=None, attachs=(), mimetype=’text/plain’, charset=None)

Parameters

  • to (str or list) –the e-mail recipients as a string or as an inventory of string
  • subject (str) – the subject of the e-mail
  • cc (str or list) – the e-mails to CC as a string or as an inventory of strings
  • body (str) – the e-mail body
  • attachs (collections.abc.Iterable) – an iterable of tuples (attach_name, mimetype, file_object) where attach_name is a string with the name that will appear on the e-mail’s attachment, mimetype is the mimetype of the attachment and file_object  may be a readable file object with the contents of the attachment
  • charset (str) – the character encoding to use for the e-mail contents

Approach

  • Import module
  • Setup mailer
  • Add subject and body of the mail
  • Supply with sender and receiver e-mail addresses
  • Send mail

Example:

Python3




# import module
from scrapy.mail import MailSender
  
# setup mailer
mailer = MailSender(mailfrom="Something@gmail.com",
                    smtphost="smtp.gmail.com", smtpport=465, smtppass="MySecretPassword")
  
# send mail
mailer.send(to=["abc@gmail.com"], subject="Scrapy Mail",
            body="Hi ! GeeksForGeeks", cc=["another@example.com"])

Output:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :